首页 新闻 会员 周边

TypeScript的tsconfig.json怎么配置,才能给输入的js文件自动添加扩展名

0
悬赏园豆:10 [已解决问题] 解决于 2024-04-21 16:24

在1个TS demo, 我想将.ts文件转换为可在浏览器执行的.js文件。
但tsc命令后,输出的js文件里import { Foo } from "./foo"; 没有自动添加扩展名(import { Foo } from "./foo.js";) 导致在浏览器中无法执行。

问题: 怎么配置tsconfig.json文件,可以将ts文件转换为可浏览器执行的js文件。

源代码: https://gitee.com/wuchm/my-ts-app

项目源文件:



转换后的js文件:

北在北方的主页 北在北方 | 初学一级 | 园豆:180
提问于:2024-04-07 20:03
< >
分享
最佳答案
0

index.js 与 foo.js 的内容根本不对, 是运行命令 tsc index.ts 生成的吗?

收获园豆:10
dudu | 高人七级 |园豆:30966 | 2024-04-07 20:29

ts 编译成 js 不是这样的输出

dudu | 园豆:30966 (高人七级) | 2024-04-07 20:33

在项目根目录执行的tsc

北在北方 | 园豆:180 (初学一级) | 2024-04-07 21:43

@北在北方: 建议在github上建一个重现这个问题的仓库

dudu | 园豆:30966 (高人七级) | 2024-04-07 23:10
其他回答(1)
0

文档: https://www.typescriptlang.org/docs/handbook/modules/theory.html#module-specifiers-are-not-transformed

While the module compiler option can transform imports and exports in input files to different module formats in output files, the module specifier (the string from which you import, or pass to require) is always emitted as-written. ...There is no compiler option that enables transforming, substituting, or rewriting module specifiers. Consequently, module specifiers must be written in a way that works for the code’s target runtime or bundler, and it’s TypeScript’s job to understand those output-relative specifiers.

北在北方 | 园豆:180 (初学一级) | 2024-04-14 19:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册