我有一个带有多个插件的git repo(1个主插件和几个插件仅用于与主插件配合使用)。我正在使用this approach进行分发,因为bower并没有为每个git repo提供多个插件的方法。
所以,我需要缩小每个插件,为每个插件创建一个源图,然后将每个插件放到一个与git子模块对应的单独的分发文件夹中,按照惯例,我将其命名为与插件相同,以使其变得简单。我想出了以下Gulp脚本,它一步一步完成,主要是基于我找到的答案here。
return gulp.src(['./jquery.dirtyforms.js', './helpers/*.js', './dialogs/*.js'], { base: './' })
.pipe(jshint())
.pipe(jshint.reporter(stylish))
.pipe(rename(function (path) {
var baseName = path.basename;
var dirName = path.dirname;
if (dirName == 'helpers' || dirName == 'dialogs') {
path.basename = 'jquery.dirtyforms.' + dirName + '.' + baseName;
console.log(path.basename);
}
path.dirname = path.basename;
}))
.pipe(gulp.dest(distributionFolder))
.pipe(sourcemaps.init())
.pipe(rename(function (path) {
var baseName = path.basename;
var dirName = path.dirname;
if (dirName == 'helpers' || dirName == 'dialogs') {
path.basename = 'jquery.dirtyforms.' + dirName + '.' + baseName;
console.log(path.basename);
}
path.dirname = path.basename;
path.extname = '.min.js';
}))
.pipe(uglify({
outSourceMap: true,
sourceRoot: '/'
}))
.pipe(gulp.dest(distributionFolder))
.pipe(sourcemaps.write('.', {
includeContent: true,
sourceRoot: '/'
}))
.pipe(gulp.dest(distributionFolder));
除了一件事,它完全符合我的要求。为每个插件生成的源映射文件包括路径中的子目录。由于插件的最终目的地不会包含此路径,因此无效。
在jquery.dirtyforms.min.js.map
文件中:
{"version":3,"sources":["jquery.dirtyforms/jquery.dirtyforms.min.js"]...
应该是
{"version":3,"sources":["jquery.dirtyforms.min.js"]...
在jquery.dirtyforms.min.js
文件中:
//# sourceMappingURL=../jquery.dirtyforms/jquery.dirtyforms.min.js.map
应该是
//# sourceMappingURL=jquery.dirtyforms.min.js.map
我挖掘了gulp-sourcemaps的来源,试图找到一个覆盖文件名的选项,但似乎并不是一个。
我想出的两个可能的解决方案是:
distributionFolder
中的文件,然后在生成后将其移动到正确的子文件夹但这两件事看起来都很糟糕。如果流首先正确创建它们会更好。有没有办法做到这一点?
答案 0 :(得分:0)
我最后选择了我提到的第二个选项 - 也就是说,在distributionFolder
(现在为settings.dest
)中生成缩小的文件,然后使用单独的复制和删除任务移动它们。
var gulp = require('gulp'),
uglify = require('gulp-uglify'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish'),
rename = require('gulp-rename'),
sourcemaps = require('gulp-sourcemaps'),
del = require('del');
var settings = {
baseProject: 'jquery.dirtyforms',
src: ['./jquery.dirtyforms.js', './helpers/*.js', './dialogs/*.js'],
dest: './dist/'
};
// Moves the .js files to the distribution folders and creates a minified version and sourcemap
gulp.task('build', ['copy-minified'], function (cb) {
del([settings.dest + '*.js', settings.dest + '*.map'], cb);
});
gulp.task('copy-minified', ['uglify'], function () {
return gulp.src([settings.dest + '*.js', settings.dest + '*.map'], { base: './' })
.pipe(rename(function (path) {
console.log('moving: ' + path.basename)
path.dirname = path.basename.replace(/\.min(?:\.js)?/g, '');
}))
.pipe(gulp.dest(settings.dest));
});
gulp.task('uglify', ['clean', 'test'], function () {
return gulp.src(settings.src, { base: './' })
.pipe(rename(function (path) {
var baseName = path.basename;
var dirName = path.dirname;
if (dirName == 'helpers' || dirName == 'dialogs') {
path.basename = settings.baseProject + '.' + dirName + '.' + baseName;
}
path.dirname = path.basename;
}))
.pipe(gulp.dest(settings.dest))
.pipe(sourcemaps.init())
.pipe(rename(function (path) {
path.dirname = '';
path.extname = '.min.js';
}))
.pipe(uglify({
outSourceMap: true,
sourceRoot: '/'
}))
.pipe(gulp.dest(settings.dest))
.pipe(sourcemaps.write('.', {
includeContent: true,
sourceRoot: '/'
}))
.pipe(gulp.dest(settings.dest));
});
// Tests the source files (smoke test)
gulp.task('test', function () {
return gulp.src(settings.src, { base: './' })
.pipe(jshint())
.pipe(jshint.reporter(stylish));
});
也许有一个更好的选择,不是这样的黑客,但这对我有用。