打字稿的编译取决于文件的顺序

Typescript compilation depends on order of files

本文关键字:文件 顺序 取决于 编译      更新时间:2023-09-26

我正在开发打字稿库,我想在我的Web应用程序中使用它。

我想将所有库文件编译成单个库.js文件。

我尝试将 ts 编译器与 tsconfig.json 文件一起使用,以及使用 gulp 任务进行编译,但在这两种情况下,输出文件中的类顺序都有问题。

生成的代码也有很多IIFE

var MyLibrary;
(function (MyLibrary) {
.....
})(MyLibrary || (MyLibrary = {}));

我想它应该只在那里一次,而不是每个班级,对吧?

My tsconfig.json:

{
  "compilerOptions": {
    "module": "amd",
    "noImplicitAny": false,
    "removeComments": true,
    "preserveConstEnums": true,
    "outFile": "dist/js/floor-map-designer.js",
    "sourceMap": true
  },
  "exclude": [
    "node_modules",
    "dist",
    "src/app.ts"
  ]
}

和 gulp 任务配置:

var tsProject = ts.createProject({
    declarationFiles: true,
    noExternalResolve: false,
    module: 'AMD',
    removeComments: true,
    outFile: "my-library.js",
    exclude: ["app.ts", "config.ts"]
});
var paths = {
    npm: './node_modules/',
    lib: "./lib/vendor/",
    tsSource: ['./src/MyLibrary/**/*.ts', "./lib/typings/tsd.d.ts"],
    tsOutput: "./dist/js/",
    tsDef: "./lib/typings/"
};
gulp.task('ts-compile', function () {
    var tsResult = gulp.src(paths.tsSource)
                    .pipe(ts(tsProject));
    return merge([
        tsResult.dts.pipe(gulp.dest(paths.tsDef)),
        tsResult.js.pipe(gulp.dest(paths.tsOutput))
    ]);
});

记得上次我使用 TypeScript 时,我用来创建自动生成的参考文件,其中包含大量

/// <reference path="...." 

并添加 /// <ts:autoRef="referencesFile.ts">到所有 .ts 文件。

但是我注意到 TS 编译器不应该再依赖于文件顺序,我的 IDE 不再需要它,所以如果可能的话,我想避免它。

我还注意到使用 --out 选项被认为是不好的(至少根据这个网站:https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md(,但如何替换它?

我正在使用打字稿 1.7。

我还注意到使用 --out 选项被认为是不好的(至少根据这个网站:https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md(,但如何替换它?

使用模块 : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

在您的情况下更改:

module: 'AMD',
removeComments: true,
outFile: "my-library.js",

module: 'amd',
removeComments: true,

并使用模块加载器(对于 amd,这将是 requirejs http://requirejs.org/(