Update gulp build to transpile to es5.

For now, builds to dist/bundled and dist/unbundled, and links polymer.html and polymer-element.html to dist/bundled/polymer.html.  Source can be restored with `gulp restore-src`.
This commit is contained in:
Kevin Schaaf 2016-09-30 17:04:22 -07:00
parent 426f2346bf
commit fb70b87065
2 changed files with 118 additions and 75 deletions

View File

@ -11,112 +11,146 @@
// jshint node: true
'use strict';
var gulp = require('gulp');
var audit = require('gulp-audit');
var replace = require('gulp-replace');
var rename = require('gulp-rename');
var vulcanize = require('gulp-vulcanize');
var runseq = require('run-sequence');
var lazypipe = require('lazypipe');
var polyclean = require('polyclean');
var del = require('del');
var eslint = require('gulp-eslint');
/* global require */
const gulp = require('gulp');
const gulpif = require('gulp-if');
const audit = require('gulp-audit');
const rename = require('gulp-rename');
const runseq = require('run-sequence');
const del = require('del');
const eslint = require('gulp-eslint');
const fs = require('fs');
const path = require('path');
var path = require('path');
const DIST_DIR = 'dist';
const BUNDLED_DIR = path.join(DIST_DIR, 'bundled');
const UNBUNDLED_DIR = path.join(DIST_DIR, 'unbundled');
const DEFAULT_BUILD_DIR = BUNDLED_DIR;
const POLYMER_LEGACY = 'polymer.html';
const POLYMER_ELEMENT = 'polymer-element.html';
const DEFAULT_BUILD_TARGET = POLYMER_LEGACY;
const ENTRY_POINTS = [POLYMER_LEGACY, POLYMER_ELEMENT];
var minimalDocument = require('./util/minimalDocument');
const polymer = require('polymer-build');
const PolymerProject = polymer.PolymerProject;
const project = new PolymerProject({ shell: DEFAULT_BUILD_TARGET });
const fork = polymer.forkStream;
var micro = "polymer-micro.html";
var mini = "polymer-mini.html";
var max = "polymer.html";
var workdir = 'dist';
var distMicro = path.join(workdir, micro);
var distMini = path.join(workdir, mini);
var distMax = path.join(workdir, max);
var pkg = require('./package.json');
var cleanupPipe = lazypipe()
// remove leading whitespace and comments
.pipe(polyclean.leftAlignJs)
// remove html wrapper
.pipe(minimalDocument)
// Add real version number
.pipe(replace, /(Polymer.version = )'master'/, '$1"' + pkg.version + '"')
;
function vulcanizeWithExcludes(target, excludes) {
if (excludes) {
excludes = excludes.map(function(ex) { return path.resolve(ex); });
}
return function() {
return gulp.src(target)
.pipe(vulcanize({
stripComments: true,
excludes: excludes
}))
.pipe(cleanupPipe())
.pipe(gulp.dest(workdir));
};
}
gulp.task('micro', vulcanizeWithExcludes(micro));
gulp.task('mini', vulcanizeWithExcludes(mini, [micro]));
gulp.task('max', vulcanizeWithExcludes(max, [mini, micro]));
const mergeStream = require('merge-stream');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const htmlmin = require('gulp-htmlmin');
const gzipSize = require('gzip-size');
const prettyBytes = require('pretty-bytes');
gulp.task('clean', function() {
return del(workdir);
return del(DIST_DIR);
});
gulp.task('build', ['clean'], () => {
// process source files in the project
const sources = project.sources()
.pipe(project.splitHtml())
// add compilers or optimizers here!
.pipe(gulpif(/\.js$/, babel({presets: ['es2015']/*, plugins: ['external-helpers']*/})))
.pipe(gulpif(/\.js$/, uglify()))
.pipe(project.rejoinHtml())
.pipe(htmlmin({removeComments: true}));
// process dependencies
const dependencies = project.dependencies()
.pipe(project.splitHtml())
// add compilers or optimizers here!
.pipe(gulpif(/\.js$/, babel({presets: ['es2015']/*, plugins: ['external-helpers']*/})))
.pipe(gulpif(/\.js$/, uglify()))
.pipe(project.rejoinHtml())
.pipe(htmlmin({removeComments: true}));
// merge the source and dependencies streams to we can analyze the project
const mergedFiles = mergeStream(sources, dependencies)
.pipe(project.analyzer);
return mergeStream(
fork(mergedFiles)
.pipe(project.bundler)
// write to the bundled folder
.pipe(gulp.dest(BUNDLED_DIR)),
fork(mergedFiles)
// write to the unbundled folder
.pipe(gulp.dest(UNBUNDLED_DIR))
);
});
// copy bower.json into dist folder
gulp.task('copy-bower-json', function() {
return gulp.src('bower.json').pipe(gulp.dest(workdir));
return gulp.src('bower.json').pipe(gulp.dest(DEFAULT_BUILD_DIR));
});
// Build
gulp.task('build-steps', function(cb) {
runseq('restore-src', 'build', 'print-size', cb);
});
// Bundled build
gulp.task('build-bundled', function(cb) {
runseq('build-steps', 'save-src', 'link-bundled', cb);
});
// Unbundled build
gulp.task('build-unbundled', function(cb) {
runseq('build-steps', 'save-src', 'link-unbundled', cb);
});
// Default Task
gulp.task('default', function(cb) {
runseq('clean', ['micro', 'mini', 'max'], cb);
});
gulp.task('default', ['build-bundled']);
// switch src and build for testing
gulp.task('save-src', function() {
return gulp.src([mini, micro, max])
return gulp.src(ENTRY_POINTS)
.pipe(rename(function(p) {
p.extname += '.bak';
p.extname += '.src';
}))
.pipe(gulp.dest('.'));
});
gulp.task('restore-src', function() {
return gulp.src([mini + '.bak', micro + '.bak', max + '.bak'])
gulp.task('restore-src', function(cb) {
const files = ENTRY_POINTS.map(f=>`${f}.src`);
gulp.src(files)
.pipe(rename(function(p) {
p.extname = '';
}))
.pipe(gulp.dest('.'));
.pipe(gulp.dest('.'))
.on('end', ()=>Promise.all(files.map(f=>del(f))).then(()=>cb()));
});
gulp.task('cleanup-switch', function() {
return del([mini + '.bak', micro + '.bak', max + '.bak']);
gulp.task('link-bundled', function(cb) {
ENTRY_POINTS.forEach(f=>{
fs.writeFileSync(f, `<link rel="import" href="${DEFAULT_BUILD_DIR}/${DEFAULT_BUILD_TARGET}">`);
});
cb();
});
gulp.task('switch-build', function() {
return gulp.src([distMini, distMicro, distMax])
.pipe(gulp.dest('.'));
gulp.task('link-unbundled', function(cb) {
ENTRY_POINTS.forEach(f=>{
fs.writeFileSync(f, `<link rel="import" href="${DEFAULT_BUILD_DIR}/${f}">`);
});
cb();
});
gulp.task('switch', function(cb) {
runseq('default', 'save-src', 'switch-build', cb);
});
gulp.task('restore', function(cb) {
runseq('restore-src', 'cleanup-switch', cb);
gulp.task('print-size', function(cb) {
fs.readFile(path.join(DEFAULT_BUILD_DIR, DEFAULT_BUILD_TARGET), function(err, contents) {
gzipSize(contents, function(err, size) {
console.log(`${DEFAULT_BUILD_TARGET} size: ${prettyBytes(size)}`);
cb();
});
});
});
gulp.task('audit', function() {
return gulp.src([distMini, distMicro, distMax])
return gulp.src(ENTRY_POINTS.map(f=>path.join(DEFAULT_BUILD_DIR, f)))
.pipe(audit('build.log', { repos: ['.'] }))
.pipe(gulp.dest(workdir));
.pipe(gulp.dest(DEFAULT_BUILD_DIR));
});
gulp.task('release', function(cb) {

View File

@ -8,17 +8,26 @@
"test": "test"
},
"devDependencies": {
"del": "^2.2.0",
"babel-plugin-external-helpers": "^6.8.0",
"babel-preset-es2015": "^6.13.2",
"del": "^2.2.1",
"dom5": "^1.3.1",
"eslint-plugin-html": "^1.3.0",
"gulp": "^3.8.11",
"gulp": "^3.9.1",
"gulp-audit": "^1.0.0",
"gulp-babel": "^6.1.2",
"gulp-eslint": "^3.0.1",
"gulp-htmlmin": "^2.0.0",
"gulp-if": "^2.0.1",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.3",
"gulp-size": "^2.1.0",
"gulp-uglify": "^2.0.0",
"gulp-vulcanize": "^6.0.1",
"lazypipe": "^1.0.1",
"merge-stream": "^1.0.0",
"polyclean": "^1.2.0",
"polymer-build": "^0.3.0",
"run-sequence": "^1.1.0",
"through2": "^2.0.0",
"web-component-tester": "^4"