【问题标题】:Have typescript compiler add file extensions (.js) when building [duplicate]构建时让打字稿编译器添加文件扩展名(.js)[重复]
【发布时间】:2020-12-30 11:59:09
【问题描述】:

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

import {foo, bar} from 'threeLetterWords';

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

我的`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 compiler is forgetting to add file extensions to ES6 module imports? ,但这个问题是 3 年前提出的,并且 vscode 自动导入是无扩展的,所以我必须手动更改很多(100+)。

有任何进展吗?如果有,如何解决这个问题?

【问题讨论】:

标签: javascript typescript module typescript-compiler-api


【解决方案1】:

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

如果你想使用模块 threeLetterWords,你必须告诉 TypeScript 使用模块 threeLetterWords。如果你想使用模块 threeLetterWords.js,你必须告诉 TypeScript 使用模块 threeLetterWords.js

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

【讨论】:

    猜你喜欢
    • 2020-10-12
    • 1970-01-01
    • 2014-03-07
    • 2018-05-30
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    相关资源
    最近更新 更多