Files
polymer/gulpfile.js

210 lines
5.9 KiB
JavaScript
Raw Normal View History

2016-02-16 11:43:39 -08:00
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http:polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http:polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http:polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http:polymer.github.io/PATENTS.txt
*/
2017-01-10 15:02:48 -08:00
/* eslint-env node */
2016-02-16 11:43:39 -08:00
'use strict';
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 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];
const polymer = require('polymer-build');
const PolymerProject = polymer.PolymerProject;
2017-01-09 15:56:20 -08:00
const project = new PolymerProject({
sources: ['./polymer.html'],
shell: './polymer.html'
});
const fork = polymer.forkStream;
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');
2016-02-16 11:43:39 -08:00
2017-01-10 15:02:48 -08:00
const lazypipe = require('lazypipe');
2017-01-09 15:56:20 -08:00
const closure = require('google-closure-compiler').gulp();
gulp.task('clean', function () {
return del(DIST_DIR);
});
2017-01-09 15:56:20 -08:00
const {Transform} = require('stream');
class LogStream extends Transform {
constructor(prefix) {
super({objectMode: true});
2017-01-10 15:02:48 -08:00
this.prefix = `${prefix || ''}:`;
2017-01-09 15:56:20 -08:00
}
_transform(file, enc, cb) {
2017-01-10 15:02:48 -08:00
console.log(this.prefix, file.path);
2017-01-09 15:56:20 -08:00
cb(null, file);
}
}
2017-01-10 15:02:48 -08:00
class OldNameStream extends Transform {
constructor(closureStream) {
2017-01-09 15:56:20 -08:00
super({objectMode: true});
2017-01-10 15:02:48 -08:00
this.fileList = closureStream.fileList_;
2017-01-09 15:56:20 -08:00
}
_transform(file, enc, cb) {
2017-01-10 15:02:48 -08:00
const origFile = this.fileList.shift();
// console.log(`rename ${file.path} -> ${origFile.path}`)
file.path = origFile.path;
cb(null, file);
}
_flush(cb) {
if (this.fileList.length > 0) {
this.fileList.forEach((oldFile) => {
// console.log(`pumping fake file ${oldFile.path}`)
let newFile = oldFile.clone({deep: true, contents: false});
newFile.contents = new Buffer('');
this.push(newFile);
})
2017-01-09 15:56:20 -08:00
}
2017-01-10 15:02:48 -08:00
cb();
2017-01-09 15:56:20 -08:00
}
}
gulp.task('build', ['clean'], () => {
2017-01-09 15:56:20 -08:00
const closureStream = closure({
// new_type_inf: true,
compilation_level: 'ADVANCED',
// compilation_level: 'SIMPLE',
language_in: 'ES6_STRICT',
language_out: 'ES5_STRICT',
warning_level: 'VERBOSE',
2017-01-10 15:02:48 -08:00
output_wrapper: '(function(){\n%output%\n}).call(self)',
2017-01-09 15:56:20 -08:00
rewrite_polyfills: false,
formatting: 'PRETTY_PRINT',
externs: ['externs/externs.js']
});
2017-01-10 15:02:48 -08:00
const closurePipeline = lazypipe()
.pipe(() => closureStream)
.pipe(() => new OldNameStream(closureStream))
2017-01-09 15:56:20 -08:00
// 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);
return mergedFiles
.pipe(project.bundler)
.pipe(project.splitHtml())
2017-01-10 15:02:48 -08:00
.pipe(gulpif(/polymer\.html_script_\d+\.js$/, closurePipeline()))
.pipe(project.rejoinHtml())
2017-01-09 15:56:20 -08:00
.pipe(gulp.dest(BUNDLED_DIR))
2016-02-16 11:43:39 -08:00
});
// copy bower.json into dist folder
2017-01-09 15:56:20 -08:00
gulp.task('copy-bower-json', function () {
return gulp.src('bower.json').pipe(gulp.dest(DEFAULT_BUILD_DIR));
2016-02-16 11:43:39 -08:00
});
// Build
2017-01-09 15:56:20 -08:00
gulp.task('build-steps', function (cb) {
runseq('restore-src', 'build', 'print-size', cb);
});
// Bundled build
2017-01-09 15:56:20 -08:00
gulp.task('build-bundled', function (cb) {
runseq('build-steps', 'save-src', 'link-bundled', cb);
});
// Unbundled build
2017-01-09 15:56:20 -08:00
gulp.task('build-unbundled', function (cb) {
runseq('build-steps', 'save-src', 'link-unbundled', cb);
2016-02-16 11:43:39 -08:00
});
// Default Task
gulp.task('default', ['build-bundled']);
2016-02-16 11:43:39 -08:00
// switch src and build for testing
2017-01-09 15:56:20 -08:00
gulp.task('save-src', function () {
return gulp.src(ENTRY_POINTS)
2017-01-09 15:56:20 -08:00
.pipe(rename(function (p) {
p.extname += '.src';
2016-02-16 11:43:39 -08:00
}))
.pipe(gulp.dest('.'));
});
2017-01-09 15:56:20 -08:00
gulp.task('restore-src', function (cb) {
const files = ENTRY_POINTS.map(f => `${f}.src`);
gulp.src(files)
2017-01-09 15:56:20 -08:00
.pipe(rename(function (p) {
2016-02-16 11:43:39 -08:00
p.extname = '';
}))
.pipe(gulp.dest('.'))
2017-01-09 15:56:20 -08:00
.on('end', () => Promise.all(files.map(f => del(f))).then(() => cb()));
2016-02-16 11:43:39 -08:00
});
2017-01-09 15:56:20 -08:00
gulp.task('link-bundled', function (cb) {
ENTRY_POINTS.forEach(f => {
fs.writeFileSync(f, `<link rel="import" href="${DEFAULT_BUILD_DIR}/${DEFAULT_BUILD_TARGET}">`);
});
cb();
2016-02-16 11:43:39 -08:00
});
2017-01-09 15:56:20 -08:00
gulp.task('link-unbundled', function (cb) {
ENTRY_POINTS.forEach(f => {
fs.writeFileSync(f, `<link rel="import" href="${DEFAULT_BUILD_DIR}/${f}">`);
});
cb();
2016-02-16 11:43:39 -08:00
});
2017-01-09 15:56:20 -08:00
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();
});
});
2016-02-16 11:43:39 -08:00
});
2017-01-09 15:56:20 -08:00
gulp.task('audit', function () {
return gulp.src(ENTRY_POINTS.map(f => path.join(DEFAULT_BUILD_DIR, f)))
2016-02-16 11:43:39 -08:00
.pipe(audit('build.log', { repos: ['.'] }))
.pipe(gulp.dest(DEFAULT_BUILD_DIR));
2016-02-16 11:43:39 -08:00
});
2017-01-09 15:56:20 -08:00
gulp.task('release', function (cb) {
2016-02-16 11:43:39 -08:00
runseq('default', ['copy-bower-json', 'audit'], cb);
});
2017-01-09 15:56:20 -08:00
gulp.task('lint', function () {
2016-09-08 10:58:06 -07:00
return gulp.src(['src/**/*.html', 'test/unit/*.html', 'util/*.js'])
2016-02-16 11:43:39 -08:00
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});