Recently, I've started using gulp and encountered a common issue. My goal is to compile TypeScript into JavaScript, create sourcemaps, and then run uglify. The challenge lies in having both sourcemaps for the minified and non-minified JS files.
The desired file structure looks like this:
framework.js
framework.js.map < old method
framework.min.js
framework.min.js.map
Below is my current gulp task:
var gulp = require('gulp'),
uglify = require('gulp-uglify')
ts = require("gulp-typescript")
sourcemaps = require('gulp-sourcemaps')
rename = require('gulp-rename');
var tsProject = ts.createProject("tsconfig.json");
gulp.task('typescript', function(){
tsProject.src()
.pipe(sourcemaps.init())
.pipe(tsProject()).js
.pipe(sourcemaps.write('.')) // Create sourcemap for non-minified version
.pipe(gulp.dest("dist"))
.pipe(uglify()) // Issues arise here
.pipe(rename({suffix:'.min'}))
.pipe(sourcemaps.write('.')) // Create sourcemap for minified version.
.pipe(gulp.dest("dist"));
});
gulp.task('default', ['typescript']);
I'm encountering an error due to running sourcemaps.write() twice to generate two different files.
tream.js:74
throw er; // Unhandled stream error in pipe.
^ GulpUglifyError: unable to minify JavaScript
at createError (C:\Users\alexa\Dropbox\code\Web\mhadmin\framework\node_modules\gulp-uglify\lib\create-error.js:6:14)
If the first sourcemap.write() call is omitted, the minified version has a correct sourcemap. Any suggestions on how to resolve this?
UPDATE: This isn't a duplicate of gulp: uglify and sourcemaps as I need multiple calls to gulp.dest() for writing multiple sourcemaps.