◆
tsconfig module 编译属性详解
作用
用来指定要使用的模块化的规范
可选版本
none、commonjs、amd、system、umd、es6、es2015、es2020、es2022、esnext、node16、nodenext
AMD:不要使用它,它仅能在浏览器工作;SystemJS:这是一个好的实验,已经被ES模块替代;ES模块:ES规范,import导入,export导出COMMONJS模块:NodeJS规范,require导入,exports导出如果你使用了
module: commonjs选项,moduleResolution: node将会默认开启。
编译示例
原始 typescript 代码
import { resolve } from 'path';
console.log(resolve());
当 module 为 commonjs,编译后的代码如下,可以看出来 commonjs 的引入是采用 require 的
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const path_1 = require('path');
console.log((0, path_1.resolve)());
当 module 为 es6,编译后的代码如下,可以看出来 es6 本身就是支持这种规范的,在 target 编译选项也是 es6 及其以上时,代码编译后没有变化
import { resolve } from 'path';
console.log(resolve());

还没有留言,来发表第一条吧~