如何让 "export default" 编译为 "module.exports"
How do I make "export default" compile to "module.exports"
我正在用打字稿编写,并将其设置为编译为 commonjs。每当我输入 export default
时,它都会编译成 exports.default
而不是 module.exports
。我正在制作一个 NPM 包,所以我想修复这个问题。我该如何解决?如果需要,我可以添加我的 tsconfig.json
文件。
你可以在你的TS文件中用module.exports
简单地写
const myExportedObj = {
};
module.exports = {
foo: myExportedObj,
};
输出:
var myExportedObj = {};
module.exports = {
foo: myExportedObj,
};
或者,据我从您的问题中可以看出,您可以不 export default
而是 export
TS
export const myExportedObj = {
};
JS
exports.myExportedObj = {};
另一个好方法是制作一个 index.ts
文件并从那里导出模块
TS
export * as Context from './context';
JS
exports.Context = require("./context");
我正在用打字稿编写,并将其设置为编译为 commonjs。每当我输入 export default
时,它都会编译成 exports.default
而不是 module.exports
。我正在制作一个 NPM 包,所以我想修复这个问题。我该如何解决?如果需要,我可以添加我的 tsconfig.json
文件。
你可以在你的TS文件中用module.exports
简单地写
const myExportedObj = {
};
module.exports = {
foo: myExportedObj,
};
输出:
var myExportedObj = {};
module.exports = {
foo: myExportedObj,
};
或者,据我从您的问题中可以看出,您可以不 export default
而是 export
TS
export const myExportedObj = {
};
JS
exports.myExportedObj = {};
另一个好方法是制作一个 index.ts
文件并从那里导出模块
TS
export * as Context from './context';
JS
exports.Context = require("./context");