我正在尝试将.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任务中,我先运行tsify再运行babelify:
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,一切都运行良好(包括源映射)。
我不知道出了什么问题。你有任何想法吗?提前感谢!