我在服务器上使用Typescript和NodeJS。
作为服务器逻辑的一部分,我需要处理.xlsx和.docx文件,但它们未包含在Typescript编译输出中。 .xlsx和.docx文件用作创建其他文件的模板。
这是我的项目的样子:
package.json
package-lock.json
ormconfig.json
tsconfig.json
/src
/reports
--other-.ts-files
Router.ts
Report.xlsx //File that I want to be included in the Typescript compilation output
Report.docx //File that I want to be included in the Typescript compilation output
如何在Typescript编译输出中包括.xlsx和.docx文件? 我需要添加什么到tsconfig.json文件?
答案 0 :(得分:0)
1)可以向nodejs添加其他扩展,但是这种方式已经被弃用了
var fs = require('fs');
require.extensions['.txt'] = function (module, filename) {
module.exports = fs.readFileSync(filename, 'utf8');
};
var words = require("./words.txt");
2)您始终可以使用节点的文件API来读取文件
const fs = require('fs')
const path = require('path')
const css = fs.readFileSync(path.resolve(__dirname, 'email.css'), 'utf8')
3)结合打字稿和webpack,可以加载自定义文件类型。例如,SVG文件以这种方式加载到webpack + ts中。例如,在这种情况下,Typescript需要其他类型的输入(对于前端应用程序,这是更常见的方式):
declare module "*.svg" {
const content: any;
export default content;
}
4)要阅读XLSX工作表,请使用三个不错的图书馆,名为xlsx