【发布时间】:2015-11-15 11:17:39
【问题描述】:
我正在尝试从.ts 编译为.min.js,如下所示:
TS --> ES6 ---> ES5 ---> .min.js + .map
在我只是执行以下操作并且一切正常之前:
TS ---> ES5 ---> .min.js + .map
我希望能够使用源地图。我的 tsconfig.json 如下:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"isolatedModules": false,
"jsx": "react",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true
}
}
由于我添加了"target": "es6",我收到了错误:
SyntaxError: 'import' and 'export' may appear only with 'sourceType: module'
tsify 文档说:
当 TypeScript 文件在通过 Browserify 捆绑器运行之前未编译为 JavaScript 时,会发生此错误。您可能会遇到几个已知的原因。
但在我的 Gulp 任务中,在 babelify 之前运行 tsify:
gulp.task("bundle", function() {
var mainTsFilePath = "src/main.ts";
var outputFolder = "bundle/src/";
var outputFileName = settings.projectName + ".min.js";
var pkg = require("./package.json");
var banner = [
"/**",
" * <%= pkg.name %> v.<%= pkg.version %> - <%= pkg.description %>",
" * Copyright (c) 2015 <%= pkg.author %>",
" * <%= pkg.license %>",
" */", ""
].join("\n");
var bundler = browserify({
debug: true,
standalone : settings.projectName
});
var babelifyConfig = { extensions: ['.js','.jsx','.ts','.tsx'] };
// TS compiler options are in tsconfig.json file
return bundler.plugin(tsify)
// Added this line and target es6
.transform(babelify.configure(babelifyConfig))
.add(mainTsFilePath)
.bundle()
.pipe(source(outputFileName))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.pipe(header(banner, { pkg : pkg } ))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(outputFolder));
});
我刚刚添加了 ES6 编译,在我将 TS 编译到 ES5 之前,一切正常(包括源映射)。
我不知道出了什么问题。你有什么想法?提前致谢!
【问题讨论】:
-
您首先使用 typescript 生成 ES6,然后使用 babelify 生成 ES5,是否有特定的原因?你的 babelifyConfig 看起来怎么样?
-
计划是让 babel 处理 polyfill,因为它比 TypeScript 做得更好,所以我从 TS 中获得了字符串类型的好处,而从 babel 中获得了 polyfills 的好处。我的 babelify 配置只设置了一些扩展: var babelifyConfig = { extensions: ['.js','.jsx','.ts','.tsx'] };
-
@RemoH.Jansen 你解决了吗?
-
解决了我自己的问题。我将奖励赏金以很好地解释 gulp、tsify 和 babelify 如何协同工作。
标签: typescript gulp babeljs uglifyjs source-maps