TSConfig文件详解11

编译器配置项-compilerOptions

模块相关(二)

基础目录 - baseUrl

设置一个基础目录,基于该目录去解析模块名称。例如在以下目录结构中:

project
├── ex.ts
├── hello
│   └── world.ts
└── tsconfig.json

配置"baseUrl": "./",TypeScript 将查找与 tsconfig.json 位于同一文件夹中的文件。

import { helloWorld } from "hello/world";
console.log(helloWorld);

这个解析方案比从node_modules中查找具有更高的优先级。

这个功能设计目的是与浏览器中的 AMD 模块加载器结合使用,不建议在其他情况下使用。从 TypeScript 4.1 开始,使用paths配置后不再需要设置baseUrl

自定义引用-customConditions

customConditions配置一个引用列表,当TypeScript解析的模块的package.jsonexports / imports字段包含自定义引用列表中的条件时,应当解析成功。这些条件将添加到解析器默认使用条件列表中。

例如,当此字段在 tsconfig.json 中设置如下:

{
  "compilerOptions": {
    "target": "es2022",
    "moduleResolution": "bundler",
    "customConditions": ["my-condition"]
  }
}

每当在 package.json 中引用exportsimports字段时,TypeScript将优先使用my-condition指向的文件。

因此,当导入的包具有以下 package.json 时:

{
  // ...
  "exports": {
    ".": {
      "my-condition": "./foo.mjs",
      "node": "./bar.mjs",
      "import": "./baz.mjs",
      "require": "./biz.mjs"
    }
  }
}

TypeScript 将尝试查找与foo.mjs 对应的文件。

该字段仅在moduleResolution的值为node16nodenextbundler时有效。

模块-module

默认值:如果target为ES3/ES5是,为commonjs,其它情况为es6/es2015

设置程序的模块系统。有关更多信息,请参阅 TypeScript 模块选项背后的理论
及其参考页。在现代的nodejs工程中你可能设置为nodenext

更改module会影响moduleResolution,它也有一个参考页面

以下是index.tsmodule为不同值时的一些输出示例:

// @filename: index.ts
import { valueOfPi } from "./constants";
 
export const twoPi = valueOfPi * 2;

CommonJS

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.twoPi = void 0;
const constants_1 = require("./constants");
exports.twoPi = constants_1.valueOfPi * 2;

UMD

(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "./constants"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.twoPi = void 0;
    const constants_1 = require("./constants");
    exports.twoPi = constants_1.valueOfPi * 2;
});

AMD

define(["require", "exports", "./constants"], function (require, exports, constants_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.twoPi = void 0;
    exports.twoPi = constants_1.valueOfPi * 2;
});

System

System.register(["./constants"], function (exports_1, context_1) {
    "use strict";
    var constants_1, twoPi;
    var __moduleName = context_1 && context_1.id;
    return {
        setters: [
            function (constants_1_1) {
                constants_1 = constants_1_1;
            }
        ],
        execute: function () {
            exports_1("twoPi", twoPi = constants_1.valueOfPi * 2);
        }
    };
});

ESNext

import { valueOfPi } from "./constants";
export const twoPi = valueOfPi * 2;

ES2015/ES6/ES2020/ES2022

import { valueOfPi } from "./constants";
export const twoPi = valueOfPi * 2;

除了 ES2015/ES6 的基本功能外,ES2020 还添加了对动态导入和 import.meta 的支持,而 ES2022 进一步添加了对顶级的await的支持。

node16/nodenext (nightly builds)

从 4.7+ 开始,node16nodenex则集成了nodejs本地ECMAScript 模块的支持。至于是输出CommonJS还是ES2020类型的JavaScript,由文件的扩展名和最近的package.jsontype设置的值决定。模块解析的工作方式也不同。您可以在手册k
模块参考.
中了解更多信息。

preserve

preserve这个值是在TypeScript 5.4中新增的,输入文件中的imports /exports语句将在输入中保留,CommonJs风格的import x = require("...")export = ...语句将输出为CommonJS的 requiremodule.exports。换句话说,每个文件的import/export都会被保留,而不是被强制编译为单一格式。

import { valueOfPi } from "./constants";
const constants = require("./constants");
export const piSquared = valueOfPi * constants.valueOfPi;

虽然很少需要在一个文件中混合使用importrequire语句,但这种module模式最好地反映了现代大多数的打包器,就下昂Bun 运行时的功能。

为什么要关心使用捆绑器或 Bun 发出的 TypeScript 模块,您可能还设置了 noEmit?TypeScript设置的module的类型影响类型检查和模块解析。设置module可以为TypeScript提供一些打包器或二者运行时如何处理import / exports的信息,这确保导入的类型准确反映运行时或打包后的情况。

None

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.twoPi = void 0;
const constants_1 = require("./constants");
exports.twoPi = constants_1.valueOfPi * 2;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容