Skip to content

Commit

Permalink
Clean up gulpfile
Browse files Browse the repository at this point in the history
Remove cruft from 1.x
Remove default task, rename "build" to "estimate-size" to reflect the
actual task

Related to #4564
  • Loading branch information
dfreedm committed Apr 25, 2017
1 parent 13fbc4c commit 764448c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 140 deletions.
192 changes: 55 additions & 137 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,26 @@

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');
const mergeStream = require('merge-stream');
const babel = require('gulp-babel');
const htmlmin = require('gulp-htmlmin');
const size = require('gulp-size');
const lazypipe = require('lazypipe');
const closure = require('google-closure-compiler').gulp();
const minimalDocument = require('./util/minimalDocument.js')

const DIST_DIR = 'dist';
const BUNDLED_DIR = path.join(DIST_DIR, 'bundled');
const UNBUNDLED_DIR = path.join(DIST_DIR, 'unbundled');
const COMPILED_DIR = path.join(DIST_DIR, 'compiled');
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];

const polymer = require('polymer-build');
const PolymerProject = polymer.PolymerProject;
const project = new PolymerProject({ entrypoint: DEFAULT_BUILD_TARGET });
const fork = polymer.forkStream;

gulp.task('clean', function() {
return del(DIST_DIR);
});

const {Transform} = require('stream');

Expand Down Expand Up @@ -86,9 +73,29 @@ class Log extends Transform {
}
}

class Uniq extends Transform {
constructor() {
super({ objectMode: true });
this.map = {};
}
_transform(file, enc, cb) {
this.map[file.path] = file;
cb();
}
_flush(done) {
for (let filePath in this.map) {
let file = this.map[filePath];
this.push(file);
}
done();
}
}

let CLOSURE_LINT_ONLY = false;
let EXPECTED_WARNING_COUNT = 498;

gulp.task('clean', () => del(DIST_DIR));

gulp.task('closure', ['clean'], () => {

let entry, splitRx, joinRx;
Expand All @@ -109,7 +116,11 @@ gulp.task('closure', ['clean'], () => {
full();

const project = new PolymerProject({
shell: `./${entry}`
shell: `./${entry}`,
fragments: [
'bower_components/shadycss/apply-shim.html',
'bower_components/shadycss/custom-style-interface.html'
]
});

function closureLintLogger(log) {
Expand Down Expand Up @@ -165,42 +176,11 @@ gulp.task('closure', ['clean'], () => {
// process dependencies
const dependencies = project.dependencies();

class Uniq extends Transform {
constructor() {
super({ objectMode: true });
this.map = {};
}
_transform(file, enc, cb) {
this.map[file.path] = file;
cb();
}
_flush(done) {
for (let filePath in this.map) {
let file = this.map[filePath];
this.push(file);
}
done();
}
}

class NoDeps extends Transform {
constructor() {
super({objectMode: true});
}
_transform(file, enc, cb) {
if (file.path.match(/shadycss/)) {
file.contents = new Buffer('');
}
cb(null, file);
}
}

// merge the source and dependencies streams to we can analyze the project
const mergedFiles = mergeStream(sources, dependencies);

const splitter = new polymer.HtmlSplitter();
return mergedFiles
.pipe(new NoDeps())
.pipe(project.bundler())
.pipe(new Uniq())
.pipe(splitter.split())
Expand All @@ -216,106 +196,44 @@ gulp.task('lint-closure', (done) => {
runseq('closure', done);
})

gulp.task('build', ['clean'], () => {
// process source files in the project
const sources = project.sources();

// process dependencies
const dependencies = project.dependencies();

// merge the source and dependencies streams to we can analyze the project
const mergedFiles = mergeStream(sources, dependencies);

const bundlePipe = lazypipe()
.pipe(() => project.splitHtml())
.pipe(() => gulpif(/\.js$/, babel({presets: ['babili']})))
.pipe(() => project.rejoinHtml())
.pipe(htmlmin, {removeComments: true})
.pipe(minimalDocument)
.pipe(size, {title: 'bundled size', gzip: true, showTotal: false, showFiles: true})

return mergeStream(
fork(mergedFiles)
.pipe(project.bundler)
.pipe(gulpif(/polymer\.html/, bundlePipe()))
// write to the bundled folder
.pipe(gulp.dest(BUNDLED_DIR)),

fork(mergedFiles)
.pipe(project.splitHtml())
// add compilers or optimizers here!
.pipe(gulpif(/\.js$/, babel({presets: ['babili']})))
.pipe(project.rejoinHtml())
.pipe(htmlmin({removeComments: true}))
// 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(DEFAULT_BUILD_DIR));
});

// Build
gulp.task('build-steps', function(cb) {
runseq('restore-src', 'build', 'print-size', cb);
});
gulp.task('estimate-size', ['clean'], () => {

// 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);
});
const babelPresets = {
presets: [['babili', {regexpConstructors: false}]]
};

// Default Task
gulp.task('default', ['build-bundled']);
const project = new PolymerProject({
shell: POLYMER_LEGACY,
fragments: [
'bower_components/shadycss/apply-shim.html',
'bower_components/shadycss/custom-style-interface.html'
]
});

// switch src and build for testing
gulp.task('save-src', function() {
return gulp.src(ENTRY_POINTS)
.pipe(rename(function(p) {
p.extname += '.src';
}))
.pipe(gulp.dest('.'));
});
// process source files in the project
const sources = project.sources();

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('.'))
.on('end', () => Promise.all(files.map(f => del(f))).then(() => cb()));
});
// process dependencies
const dependencies = project.dependencies();

gulp.task('link-bundled', function(cb) {
ENTRY_POINTS.forEach(f => {
fs.writeFileSync(f, `<link rel="import" href="${DEFAULT_BUILD_DIR}/${DEFAULT_BUILD_TARGET}">`);
});
cb();
});
// merge the source and dependencies streams to we can analyze the project
const mergedFiles = mergeStream(sources, dependencies);

gulp.task('link-unbundled', function(cb) {
ENTRY_POINTS.forEach(f => {
fs.writeFileSync(f, `<link rel="import" href="${DEFAULT_BUILD_DIR}/${f}">`);
});
cb();
});
const bundledSplitter = new polymer.HtmlSplitter();

gulp.task('audit', function() {
return gulp.src(ENTRY_POINTS.map(f => path.join(DEFAULT_BUILD_DIR, f)))
.pipe(audit('build.log', { repos: ['.'] }))
.pipe(gulp.dest(DEFAULT_BUILD_DIR));
});
const bundlePipe = lazypipe()
.pipe(() => bundledSplitter.split())
.pipe(() => gulpif(/\.js$/, babel(babelPresets)))
.pipe(() => bundledSplitter.rejoin())
.pipe(minimalDocument)

gulp.task('release', function(cb) {
runseq('default', ['copy-bower-json', 'audit'], cb);
return mergedFiles
.pipe(project.bundler())
.pipe(gulpif(/polymer\.html$/, bundlePipe()))
.pipe(new Uniq())
.pipe(gulpif(/polymer\.html$/, size({ title: 'bundled size', gzip: true, showTotal: false, showFiles: true })))
// write to the bundled folder
.pipe(gulp.dest(BUNDLED_DIR))
});

gulp.task('lint', function() {
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
"eslint-plugin-html": "^2.0.1",
"google-closure-compiler": "^20170409.0.0",
"gulp": "^3.9.1",
"gulp-audit": "^1.0.0",
"gulp-babel": "^6.1.2",
"gulp-eslint": "^3.0.1",
"gulp-htmlmin": "^3.0.0",
"gulp-if": "^2.0.1",
"gulp-rename": "^1.2.2",
"gulp-size": "^2.1.0",
"gulp-vulcanize": "^6.0.1",
"lazypipe": "^1.0.1",
Expand Down

0 comments on commit 764448c

Please sign in to comment.