我试图修改一个gulp文件以适应我的目的,但我遇到了问题。我只关心一项任务:
gulp.task('js:browser', function () {
return mergeStream.apply(null,
Object.keys(jsBundles).map(function(key) {
return bundle(jsBundles[key], key);
})
);
});它使用browserify将我的包压缩成一个可用的单个文件。它使用这两个方法和这个对象:
function createBundle(src) {
//if the source is not an array, make it one
if (!src.push) {
src = [src];
}
var customOpts = {
entries: src,
debug: true
};
var opts = assign({}, watchify.args, customOpts);
var b = watchify(browserify(opts));
b.transform(babelify.configure({
stage: 1
}));
b.transform(hbsfy);
b.on('log', plugins.util.log);
return b;
}
function bundle(b, outputPath) {
var splitPath = outputPath.split('/');
var outputFile = splitPath[splitPath.length - 1];
var outputDir = splitPath.slice(0, -1).join('/');
console.log(outputFile);
console.log(plugins);
return b.bundle()
// log errors if they happen
.on('error', plugins.util.log.bind(plugins.util, 'Browserify Error'))
.pipe(source(outputFile))
// optional, remove if you don't need to buffer file contents
.pipe(buffer())
// optional, remove if you dont want sourcemaps
.pipe(plugins.sourcemaps.init({loadMaps: true})) // loads map from browserify file
// Add transformation tasks to the pipeline here.
.pipe(plugins.sourcemaps.write('./')) // writes .map file
.pipe(gulp.dest('build/public/' + outputDir));
}
var jsBundles = {
'js/polyfills/promise.js': createBundle('./public/js/polyfills/promise.js'),
'js/polyfills/url.js': createBundle('./public/js/polyfills/url.js'),
'js/settings.js': createBundle('./public/js/settings/index.js'),
'js/main.js': createBundle('./public/js/main/index.js'),
'js/remote-executor.js': createBundle('./public/js/remote-executor/index.js'),
'js/idb-test.js': createBundle('./public/js/idb-test/index.js'),
'sw.js': createBundle(['./public/js/sw/index.js', './public/js/sw/preroll/index.js'])
};当我运行gulp任务js:bower时,我从.pipe(plugins.sourcemaps.init({loadMaps: true}))表达式中得到以下错误:
TypeError: Cannot read property 'init' of undefined我知道这些行是可选的,我可以注释掉它们,但我确实想要它们。当我运行示例文件中的代码时,它工作正常,当我在我的gulp文件中运行它时,它给我错误。对我可能遗漏的内容有什么建议吗?谢谢!
发布于 2016-04-22 13:49:38
gulp-load-plugins会分析您的package.json文件的内容,以找出您安装了哪些Gulp插件。确保gulp-sourcemaps在那里定义的"devDependencies"中。如果未运行
npm install --save-dev gulp-sourcemaps您的问题可能与延迟加载源地图插件有关。如果上述方法都不能帮助您,请尝试如下所示的gulp-load-plugins:
var plugins = require('gulp-load-plugins')({lazy:false});https://stackoverflow.com/questions/36778738
复制相似问题