◆

tsconfig module 编译属性详解

2023-02-02 01:370

作用

用来指定要使用的模块化的规范

可选版本

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());
💬留言板0

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

PAGE LOAD / LIVEtsconfig module 编译属性详解加载中