我正在努力使用gulp获得聚合物的自定义构建 . 我的目标是使用es6编写的Polymer 1项目进行编译和捆绑 . 我按照本指南https://github.com/PolymerElements/generator-polymer-init-custom-build .
转换适用于单个文件,但任何捆绑的js代码都是未转换的(如es6中所述) . 这是我的gulp任务:
function build() {
return new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
// Okay, so first thing we do is clear the build directory
console.log(`Deleting ${buildDirectory} directory...`);
del([buildDirectory])
.then(() => {
// Okay, now let's get your source files
let sourcesStream = polymerProject.sources()
// Here's how splitHtml & gulpif work
.pipe(polymerProject.splitHtml())
// Transpile
.pipe($.sourcemaps.init())
.pipe($.if('*.js', $.babel({
presets: ['es2015']
})))
.pipe($.sourcemaps.write())
// Oh, well do you want to minify stuff? Go for it!
.pipe(gulpif(/\.js$/, uglify()))
.pipe(gulpif(/\.html$/, htmlMinifier()))
.pipe(gulpif(/\.(png|gif|jpg|svg)$/, imagemin()))
.pipe(polymerProject.rejoinHtml());
// Okay, now let's do the same to your dependencies
let dependenciesStream = polymerProject.dependencies()
// .pipe(polymerProject.bundler)
.pipe(polymerProject.splitHtml())
.pipe(gulpif(/\.js$/, uglify()))
.pipe(gulpif(/\.html$/, htmlMinifier()))
.pipe(polymerProject.rejoinHtml());
// Okay, now let's merge them into a single build stream
let buildStream = mergeStream(sourcesStream, dependenciesStream)
.once('data', () => {
console.log('Analyzing build dependencies...');
});
// #PROBLEM# -> All included sources won't be transpiled
buildStream = buildStream.pipe(polymerProject.bundler);
// Okay, time to pipe to the build directory
buildStream = buildStream.pipe(gulp.dest(buildDirectory));
// waitFor the buildStream to complete
return waitFor(buildStream);
})
.then(() => {
// Okay, now let's generate the Service Worker
console.log('Generating the Service Worker...');
return polymerBuild.addServiceWorker({
project: polymerProject,
buildRoot: buildDirectory,
bundled: true,
swPrecacheConfig: swPrecacheConfig
});
})
.then(() => {
// You did it!
console.log('Build complete!');
resolve();
});
});
}
gulp.task('build', build);
谢谢您的帮助 .