【问题标题】:Is it possible to have TS Vite use ESM and have TS Gulp use Common JS in the same project?是否可以让 TS Vite 使用 ESM 并让 TS Gulp 在同一个项目中使用 Common JS?
【发布时间】:2021-10-24 05:32:57
【问题描述】:
这是我的具体场景:
- 我有一个使用 TypeScript 的 Vite 项目
- 我添加了Gulp 来处理一些自动化任务
- 我希望 Gulp 文件用 TypeScript 编写,所以我关注了these steps
- 我遇到了this issue
Gulp 的创建者这样回应:
您正在使用 esm,因此这是 gulpjs/rechoir#43 的副本...您可以切换回 CJS。
我想知道的是,TypeScript Gulp(即gulpfile.ts 文件)是否可以在 Common JS 上运行,而 Vite 仍然可以在 ESM 上运行并使用 TypeScript 文件。
【问题讨论】:
标签:
typescript
gulp
commonjs
vite
es6-modules
【解决方案1】:
这个问题可以通过这个其他答案来回答:
https://stackoverflow.com/a/49849088/1611058
(请注意,在发布此问题后,它已更新为我需要的答案)
将此应用于您的 tsconfig.json 文件:
{
// these options are overrides used only by ts-node
"ts-node": {
"compilerOptions": {
"module": "commonjs"
}
}
}
这将允许 ts-node 使用 CommonJS 编译你的 gulp 文件,并让 Vite 使用 ESM 编译你的其余代码。