tsconfig模块选项-“系统"是否指SystemJS? [英] tsconfig module options - does 'System' refer to SystemJS?
问题描述
在tsconfig.json文件中,可以将以下选项指定为CompilerOptions'module'属性的值:
In a tsconfig.json file, the following option can be specified as the value of the compilerOptions 'module' property:
System
这样我们就可以得到:
{
"compilerOptions": {
"module": "System",
...
系统是否引用SystemJS(即,如果将SystemJS用作模块加载器,那么在创建AngularJS或Angular应用程序时,我在tsconfig.json中是否始终需要"System")?该文档似乎没有对此进行解释:
Does System refer to SystemJS (i.e. if SystemJS is being used as the module loader do I always need 'System' in my tsconfig.json when I'm creating an AngularJS or Angular app)? The documentation doesn't appear to explain this:
https://www.typescriptlang.org/docs/handbook/compiler- options.html
在TypeScript的Visual Studio项目配置中,"TypeScript Build> Module system"下还有一个"System"选项,这显然与tsconfig.json中的"System"相同.
In Visual Studio project configuration for TypeScript there's also a 'System' option under 'TypeScript Build > Module system', which will obviously be referring to the same thing as the 'System' in the tsconfig.json.
推荐答案
是的,它引用SystemJS,并且如果希望TypeScript编译器按预期方式运行并生成所需的代码,则明确包含它很重要.如果未指定,TypeScript将还原为根据当前target
ES版本(默认为ES3
,触发commonjs
模块)为基础生成(并期望)代码.如编译器文档中所述,它将对其他默认编译器选项(如moduleResolution
和allowSyntheticDefaultImports
)产生影响.
Yes it Refers to SystemJS, and including it explicitly is important if you want the TypeScript compiler to behave as you expect and generate the code you expect. If you don't specify, TypeScript will revert to generating (and expecting) code based off of the current target
ES version (by default ES3
, triggering commonjs
modules). As noted in the compiler docs it will have an affect on other default compiler options like moduleResolution
and allowSyntheticDefaultImports
.
例如,如果您有一个如下的打字稿文件
For example, if you have a typescript file as follows
import { functionA } from './moduleA';
functionA();
如果将module
指定为system
,则生成的代码将使用系统调用:
If you specify module
as system
your generated code will used System calls:
System.register(["./moduleA"], function (exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var moduleA_1;
return {
setters: [
function (moduleA_1_1) {
moduleA_1 = moduleA_1_1;
}
],
execute: function () {
moduleA_1.functionA('Stuff');
}
};
});
但是,如果允许编译器默认为commonjs
,它将生成:
However, if you allow the compiler to default to commonjs
, it will generate:
"use strict";
var moduleA_1 = require('./moduleA');
moduleA_1.functionA('Stuff');
请注意,生成的代码可能会根据TS版本或其他标志而有所不同.
From testing/experience and the module resolution and compiler docs you linked.
这篇关于tsconfig模块选项-“系统"是否指SystemJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!