使用 Gulp 在不同的文件夹中保存文件时保留源映射
Keep source maps when saving files in a different folder with Gulp
我正在尝试创建一个 Gulp 任务,它获取 JS 文件,对其运行 uglify,为文件名添加 .min
后缀,然后将它们保存在不同的目标文件夹中比源文件夹。棘手的部分是保持源映射工作...
gulp.task('uglify-js', function () {
// Fetch CSS files
return gulp.src(['assets/js/*.js', '!assets/js/*.min.js'])
// Start source map
.pipe(sourcemaps.init({loadMaps: true}))
// Uglify JS
.pipe(uglify())
// Add file suffix
.pipe(rename({suffix: '.min'}))
// Save source map
.pipe(sourcemaps.write())
// Save output to destination folder
.pipe(gulp.dest('public/js/'));
});
我尝试了很多不同的选项,但源映射似乎不起作用...
有谁知道这样做的正确方法吗?
我的包裹:
"devDependencies": {
"gulp": "^3.8.10",
"gulp-rename": "^1.2.0",
"gulp-sourcemaps": "^1.3.0",
"gulp-uglify": "^1.0.2"
}
非常感谢! :)
问题是 gulp-rename 当前不支持 gulp-sourcemaps,如 their latest issue.
中所述
一种替代方法是使用支持源映射的不同库,例如 gulp-concat, which supports renaming the files. If you don't want to concat your files, however, you could always open a pull request against gulp-rename to add sourcemaps support, as documented here。
更新: 截至 2015-03-14 gulp-现在重命名 supports sourcemaps
我正在尝试创建一个 Gulp 任务,它获取 JS 文件,对其运行 uglify,为文件名添加 .min
后缀,然后将它们保存在不同的目标文件夹中比源文件夹。棘手的部分是保持源映射工作...
gulp.task('uglify-js', function () {
// Fetch CSS files
return gulp.src(['assets/js/*.js', '!assets/js/*.min.js'])
// Start source map
.pipe(sourcemaps.init({loadMaps: true}))
// Uglify JS
.pipe(uglify())
// Add file suffix
.pipe(rename({suffix: '.min'}))
// Save source map
.pipe(sourcemaps.write())
// Save output to destination folder
.pipe(gulp.dest('public/js/'));
});
我尝试了很多不同的选项,但源映射似乎不起作用... 有谁知道这样做的正确方法吗?
我的包裹:
"devDependencies": {
"gulp": "^3.8.10",
"gulp-rename": "^1.2.0",
"gulp-sourcemaps": "^1.3.0",
"gulp-uglify": "^1.0.2"
}
非常感谢! :)
问题是 gulp-rename 当前不支持 gulp-sourcemaps,如 their latest issue.
中所述一种替代方法是使用支持源映射的不同库,例如 gulp-concat, which supports renaming the files. If you don't want to concat your files, however, you could always open a pull request against gulp-rename to add sourcemaps support, as documented here。
更新: 截至 2015-03-14 gulp-现在重命名 supports sourcemaps