让打字稿编译器在构建时添加文件扩展名 (.js)



在我正在处理的一个打字稿项目中,该项目在节点和浏览器中运行(使用 html 和 package.json 中的type = 'module)。但是,它不想在最后加载没有.js扩展名的文件,并且打字稿正在生成如下所示的文件:

import {foo, bar} from 'threeLetterWords';

此导入在节点和浏览器中都失败,因为它需要找到"threeLetterWords.js"。如何使 TSC 自动将这些.js扩展添加到导入末尾?

My 'tsconfig.json':

{
"compilerOptions": {
"outDir": "./Build",
"target": "ES6",

}
}

还有我的文件结构:

root  
|___Build  
|     |__(the root directory except for the build folder)  
|  
|__Public  
|    |__scripts
|    |     |__main.js  
|    |     |__threeLetterWords.js  
|    |__index.html
|
|__Server Logic
|     |__threeLetterWordsServer.js
|
|__app.js
|__package.json
|__tsconfig.json

我见过 Typescript 编译器忘记在 ES6 模块导入中添加文件扩展名?,但这个问题是在 3 年前提出的,并且 vscode 自动导入是无扩展名的,所以我必须手动更改很多 (100+)。

是否取得了任何进展,如果是,该怎么做才能解决这个问题?

threeLetterWords.jsthreeLetterWords是两个不同的模块。字符串.js在模块 URI 中绝对没有任何魔术意义。

如果要使用模块threeLetterWords则必须告诉TypeScript使用模块threeLetterWords。如果要使用模块threeLetterWords.js则必须告诉TypeScript使用模块threeLetterWords.js

TypeScript 永远不会更改您告诉它使用的模块名称。我有一个依赖于TypeScript 而不更改模块名称的项目,因为我正在使用 Node 模块加载器将模块解析为本机扩展、CommonJS 模块或 JSON 模块。

最新更新