2016-02-16 11:43:39 -08:00
|
|
|
/**
|
|
|
|
|
* @license
|
2017-03-03 16:54:36 -08:00
|
|
|
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
|
2016-02-16 11:43:39 -08:00
|
|
|
* 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';
|
|
|
|
|
|
2016-09-30 17:04:22 -07:00
|
|
|
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');
|
2017-01-18 14:24:27 -08:00
|
|
|
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')
|
2016-09-30 17:04:22 -07:00
|
|
|
|
|
|
|
|
const DIST_DIR = 'dist';
|
|
|
|
|
const BUNDLED_DIR = path.join(DIST_DIR, 'bundled');
|
|
|
|
|
const UNBUNDLED_DIR = path.join(DIST_DIR, 'unbundled');
|
2017-01-18 14:24:27 -08:00
|
|
|
const COMPILED_DIR = path.join(DIST_DIR, 'compiled');
|
2016-09-30 17:04:22 -07:00
|
|
|
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-03-29 15:52:01 -07:00
|
|
|
const project = new PolymerProject({ entrypoint: DEFAULT_BUILD_TARGET });
|
2016-09-30 17:04:22 -07:00
|
|
|
const fork = polymer.forkStream;
|
|
|
|
|
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('clean', function() {
|
2016-09-30 17:04:22 -07:00
|
|
|
return del(DIST_DIR);
|
|
|
|
|
});
|
|
|
|
|
|
2017-01-09 15:56:20 -08:00
|
|
|
const {Transform} = require('stream');
|
|
|
|
|
|
2017-01-10 15:02:48 -08:00
|
|
|
class OldNameStream extends Transform {
|
2017-01-12 15:55:12 -08:00
|
|
|
constructor(fileList) {
|
2017-01-09 15:56:20 -08:00
|
|
|
super({objectMode: true});
|
2017-01-12 15:55:12 -08:00
|
|
|
this.fileList = fileList;
|
2017-01-09 15:56:20 -08:00
|
|
|
}
|
|
|
|
|
_transform(file, enc, cb) {
|
2017-01-12 15:55:12 -08:00
|
|
|
if (this.fileList) {
|
|
|
|
|
const origFile = this.fileList.shift();
|
|
|
|
|
// console.log(`rename ${file.path} -> ${origFile.path}`)
|
|
|
|
|
file.path = origFile.path;
|
|
|
|
|
}
|
2017-01-10 15:02:48 -08:00
|
|
|
cb(null, file);
|
|
|
|
|
}
|
|
|
|
|
_flush(cb) {
|
2017-01-12 15:55:12 -08:00
|
|
|
if (this.fileList && this.fileList.length > 0) {
|
2017-01-10 15:02:48 -08:00
|
|
|
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-12 15:55:12 -08:00
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-29 15:52:01 -07:00
|
|
|
class Log extends Transform {
|
|
|
|
|
constructor(prefix = '') {
|
|
|
|
|
super({objectMode: true});
|
|
|
|
|
this.prefix = prefix;
|
|
|
|
|
}
|
|
|
|
|
_transform(file, enc, cb) {
|
|
|
|
|
console.log(this.prefix, file.path);
|
|
|
|
|
cb(null, file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-18 11:29:33 -07:00
|
|
|
let CLOSURE_LINT_ONLY = false;
|
|
|
|
|
let EXPECTED_WARNING_COUNT = 503;
|
|
|
|
|
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('closure', ['clean'], () => {
|
|
|
|
|
|
2017-03-29 15:52:01 -07:00
|
|
|
let entry, splitRx, joinRx;
|
|
|
|
|
|
|
|
|
|
function full() {
|
|
|
|
|
entry = 'polymer.html';
|
|
|
|
|
splitRx = /polymer\.html_script_\d+\.js$/;
|
|
|
|
|
joinRx = /polymer\.html/;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function element() {
|
|
|
|
|
entry = 'polymer-element.html';
|
|
|
|
|
splitRx = /polymer-element\.html_script_\d+\.js$/;
|
|
|
|
|
joinRx = /polymer-element\.html/;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 11:08:58 -07:00
|
|
|
// element();
|
|
|
|
|
full();
|
2017-03-29 15:52:01 -07:00
|
|
|
|
2017-01-18 14:24:27 -08:00
|
|
|
const project = new PolymerProject({
|
2017-04-17 11:17:14 -07:00
|
|
|
shell: `./${entry}`
|
2017-01-18 14:24:27 -08:00
|
|
|
});
|
2017-01-09 15:56:20 -08:00
|
|
|
|
2017-04-18 11:29:33 -07:00
|
|
|
function closureLintLogger(log) {
|
|
|
|
|
let result = log.split(/\n/).slice(-2)[0];
|
|
|
|
|
let warnings = result.match(/(\d+) warning/);
|
|
|
|
|
let chalk = require('chalk');
|
|
|
|
|
if (warnings && Number(warnings[1]) > EXPECTED_WARNING_COUNT) {
|
|
|
|
|
console.log(log);
|
|
|
|
|
console.error(chalk.red(`closure linting: actual warning count ${warnings[1]} greater than expected warning count ${EXPECTED_WARNING_COUNT}`));
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let closurePluginOptions;
|
|
|
|
|
|
|
|
|
|
if (CLOSURE_LINT_ONLY) {
|
|
|
|
|
closurePluginOptions = {
|
|
|
|
|
logger: closureLintLogger
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-09 15:56:20 -08:00
|
|
|
const closureStream = closure({
|
|
|
|
|
compilation_level: 'ADVANCED',
|
|
|
|
|
language_in: 'ES6_STRICT',
|
|
|
|
|
language_out: 'ES5_STRICT',
|
|
|
|
|
warning_level: 'VERBOSE',
|
2017-03-29 15:52:01 -07:00
|
|
|
output_wrapper: '(function(){\n%output%\n}).call(self);',
|
|
|
|
|
assume_function_wrapper: true,
|
2017-01-09 15:56:20 -08:00
|
|
|
rewrite_polyfills: false,
|
2017-03-29 15:52:01 -07:00
|
|
|
new_type_inf: true,
|
2017-04-18 11:29:33 -07:00
|
|
|
checks_only: CLOSURE_LINT_ONLY,
|
2017-03-29 15:52:01 -07:00
|
|
|
externs: [
|
|
|
|
|
'externs/webcomponents-externs.js',
|
|
|
|
|
'externs/polymer-externs.js',
|
2017-03-30 11:08:58 -07:00
|
|
|
'externs/closure-types.js',
|
2017-03-29 15:52:01 -07:00
|
|
|
],
|
|
|
|
|
extra_annotation_name: [
|
|
|
|
|
'polymerMixin',
|
|
|
|
|
'polymerMixinClass',
|
|
|
|
|
'polymerElement'
|
|
|
|
|
]
|
2017-04-18 11:29:33 -07:00
|
|
|
}, closurePluginOptions);
|
2017-01-09 15:56:20 -08:00
|
|
|
|
2017-01-10 15:02:48 -08:00
|
|
|
const closurePipeline = lazypipe()
|
|
|
|
|
.pipe(() => closureStream)
|
2017-01-12 15:55:12 -08:00
|
|
|
.pipe(() => new OldNameStream(closureStream.fileList_))
|
2017-01-10 15:02:48 -08:00
|
|
|
|
2017-01-09 15:56:20 -08:00
|
|
|
// process source files in the project
|
2017-04-17 11:17:14 -07:00
|
|
|
const sources = project.sources();
|
2017-01-09 15:56:20 -08:00
|
|
|
|
|
|
|
|
// process dependencies
|
2017-04-17 11:17:14 -07:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-09 15:56:20 -08:00
|
|
|
|
|
|
|
|
// merge the source and dependencies streams to we can analyze the project
|
|
|
|
|
const mergedFiles = mergeStream(sources, dependencies);
|
|
|
|
|
|
2017-03-29 15:52:01 -07:00
|
|
|
const splitter = new polymer.HtmlSplitter();
|
2017-01-09 15:56:20 -08:00
|
|
|
return mergedFiles
|
2017-04-17 11:17:14 -07:00
|
|
|
.pipe(new NoDeps())
|
|
|
|
|
.pipe(project.bundler())
|
|
|
|
|
.pipe(new Uniq())
|
2017-03-29 15:52:01 -07:00
|
|
|
.pipe(splitter.split())
|
|
|
|
|
.pipe(gulpif(splitRx, closurePipeline()))
|
|
|
|
|
.pipe(splitter.rejoin())
|
|
|
|
|
.pipe(gulpif(joinRx, minimalDocument()))
|
|
|
|
|
.pipe(gulpif(joinRx, size({title: 'closure size', gzip: true, showTotal: false, showFiles: true})))
|
2017-01-18 14:24:27 -08:00
|
|
|
.pipe(gulp.dest(COMPILED_DIR))
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-18 11:29:33 -07:00
|
|
|
gulp.task('lint-closure', (done) => {
|
|
|
|
|
CLOSURE_LINT_ONLY = true;
|
|
|
|
|
runseq('closure', done);
|
|
|
|
|
})
|
|
|
|
|
|
2017-01-18 14:24:27 -08:00
|
|
|
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))
|
|
|
|
|
);
|
2016-02-16 11:43:39 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// copy bower.json into dist folder
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('copy-bower-json', function() {
|
2016-09-30 17:04:22 -07:00
|
|
|
return gulp.src('bower.json').pipe(gulp.dest(DEFAULT_BUILD_DIR));
|
2016-02-16 11:43:39 -08:00
|
|
|
});
|
|
|
|
|
|
2016-09-30 17:04:22 -07:00
|
|
|
// Build
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('build-steps', function(cb) {
|
2016-09-30 17:04:22 -07:00
|
|
|
runseq('restore-src', 'build', 'print-size', cb);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Bundled build
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('build-bundled', function(cb) {
|
2016-09-30 17:04:22 -07:00
|
|
|
runseq('build-steps', 'save-src', 'link-bundled', cb);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Unbundled build
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('build-unbundled', function(cb) {
|
2016-09-30 17:04:22 -07:00
|
|
|
runseq('build-steps', 'save-src', 'link-unbundled', cb);
|
2016-02-16 11:43:39 -08:00
|
|
|
});
|
|
|
|
|
|
2016-09-30 17:04:22 -07:00
|
|
|
// Default Task
|
|
|
|
|
gulp.task('default', ['build-bundled']);
|
|
|
|
|
|
2016-02-16 11:43:39 -08:00
|
|
|
// switch src and build for testing
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('save-src', function() {
|
2016-09-30 17:04:22 -07:00
|
|
|
return gulp.src(ENTRY_POINTS)
|
2017-01-18 14:24:27 -08:00
|
|
|
.pipe(rename(function(p) {
|
2016-09-30 17:04:22 -07:00
|
|
|
p.extname += '.src';
|
2016-02-16 11:43:39 -08:00
|
|
|
}))
|
|
|
|
|
.pipe(gulp.dest('.'));
|
|
|
|
|
});
|
|
|
|
|
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('restore-src', function(cb) {
|
2017-01-09 15:56:20 -08:00
|
|
|
const files = ENTRY_POINTS.map(f => `${f}.src`);
|
2016-09-30 17:04:22 -07:00
|
|
|
gulp.src(files)
|
2017-01-18 14:24:27 -08:00
|
|
|
.pipe(rename(function(p) {
|
2016-02-16 11:43:39 -08:00
|
|
|
p.extname = '';
|
|
|
|
|
}))
|
2016-09-30 17:04:22 -07:00
|
|
|
.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-18 14:24:27 -08:00
|
|
|
gulp.task('link-bundled', function(cb) {
|
2017-01-09 15:56:20 -08:00
|
|
|
ENTRY_POINTS.forEach(f => {
|
2016-09-30 17:04:22 -07:00
|
|
|
fs.writeFileSync(f, `<link rel="import" href="${DEFAULT_BUILD_DIR}/${DEFAULT_BUILD_TARGET}">`);
|
|
|
|
|
});
|
|
|
|
|
cb();
|
2016-02-16 11:43:39 -08:00
|
|
|
});
|
|
|
|
|
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('link-unbundled', function(cb) {
|
2017-01-09 15:56:20 -08:00
|
|
|
ENTRY_POINTS.forEach(f => {
|
2016-09-30 17:04:22 -07:00
|
|
|
fs.writeFileSync(f, `<link rel="import" href="${DEFAULT_BUILD_DIR}/${f}">`);
|
|
|
|
|
});
|
|
|
|
|
cb();
|
2016-02-16 11:43:39 -08:00
|
|
|
});
|
|
|
|
|
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('audit', function() {
|
2017-01-09 15:56:20 -08:00
|
|
|
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: ['.'] }))
|
2016-09-30 17:04:22 -07:00
|
|
|
.pipe(gulp.dest(DEFAULT_BUILD_DIR));
|
2016-02-16 11:43:39 -08:00
|
|
|
});
|
|
|
|
|
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('release', function(cb) {
|
2016-02-16 11:43:39 -08:00
|
|
|
runseq('default', ['copy-bower-json', 'audit'], cb);
|
|
|
|
|
});
|
|
|
|
|
|
2017-01-18 14:24:27 -08:00
|
|
|
gulp.task('lint', function() {
|
2017-03-03 15:58:52 -08:00
|
|
|
return gulp.src(['lib/**/*.html', 'test/unit/*.html', 'util/*.js'])
|
2016-02-16 11:43:39 -08:00
|
|
|
.pipe(eslint())
|
|
|
|
|
.pipe(eslint.format())
|
|
|
|
|
.pipe(eslint.failAfterError());
|
|
|
|
|
});
|