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.
|
2018-02-08 10:02:45 +01: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
|
2016-02-16 11:43:39 -08:00
|
|
|
* Code distributed by Google as part of the polymer project is also
|
2018-02-08 10:02:45 +01:00
|
|
|
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
|
2016-02-16 11:43:39 -08:00
|
|
|
*/
|
|
|
|
|
|
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 runseq = require('run-sequence');
|
|
|
|
|
const del = require('del');
|
|
|
|
|
const eslint = require('gulp-eslint');
|
2017-11-10 12:17:11 -08:00
|
|
|
const fs = require('fs-extra');
|
2016-09-30 17:04:22 -07:00
|
|
|
const path = require('path');
|
2017-01-18 14:24:27 -08:00
|
|
|
const mergeStream = require('merge-stream');
|
|
|
|
|
const babel = require('gulp-babel');
|
|
|
|
|
const size = require('gulp-size');
|
|
|
|
|
const lazypipe = require('lazypipe');
|
|
|
|
|
const closure = require('google-closure-compiler').gulp();
|
2017-10-09 12:42:16 -07:00
|
|
|
const minimalDocument = require('./util/minimalDocument.js');
|
2018-02-25 17:10:11 +01:00
|
|
|
const dom5 = require('dom5/lib/index-next');
|
2017-07-14 16:58:54 -07:00
|
|
|
const parse5 = require('parse5');
|
2017-10-18 11:57:05 -07:00
|
|
|
const replace = require('gulp-replace');
|
2016-09-30 17:04:22 -07:00
|
|
|
|
|
|
|
|
const DIST_DIR = 'dist';
|
|
|
|
|
const BUNDLED_DIR = path.join(DIST_DIR, 'bundled');
|
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 POLYMER_LEGACY = 'polymer.html';
|
|
|
|
|
const POLYMER_ELEMENT = 'polymer-element.html';
|
|
|
|
|
|
2017-10-09 12:42:16 -07:00
|
|
|
const {PolymerProject, HtmlSplitter} = require('polymer-build');
|
2016-09-30 17:04:22 -07:00
|
|
|
|
2017-01-09 15:56:20 -08:00
|
|
|
const {Transform} = require('stream');
|
|
|
|
|
|
2017-06-29 16:47:17 -07:00
|
|
|
class BackfillStream 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-04-18 11:29:33 -07:00
|
|
|
let CLOSURE_LINT_ONLY = false;
|
|
|
|
|
|
2017-10-09 12:42:16 -07:00
|
|
|
let firstImportFinder = dom5.predicates.AND(
|
|
|
|
|
dom5.predicates.hasTagName('link'), dom5.predicates.hasAttrValue('rel', 'import')
|
|
|
|
|
);
|
2017-06-27 18:39:39 -07:00
|
|
|
|
2017-10-09 12:42:16 -07:00
|
|
|
const header =
|
2017-08-11 14:49:31 -07:00
|
|
|
`/**
|
2017-10-09 12:42:16 -07:00
|
|
|
* @fileoverview Generated typings for Polymer mixins
|
|
|
|
|
* @externs
|
|
|
|
|
*
|
2017-08-11 14:49:31 -07:00
|
|
|
* @license
|
|
|
|
|
* Copyright (c) 2017 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-10-09 12:42:16 -07:00
|
|
|
*/
|
|
|
|
|
/* eslint-disable */
|
|
|
|
|
`;
|
2017-08-11 14:49:31 -07:00
|
|
|
|
2017-06-29 16:47:17 -07:00
|
|
|
class AddClosureTypeImport extends Transform {
|
|
|
|
|
constructor(entryFileName, typeFileName) {
|
2017-06-27 18:39:39 -07:00
|
|
|
super({objectMode: true});
|
2017-06-29 16:47:17 -07:00
|
|
|
this.target = path.resolve(entryFileName);
|
|
|
|
|
this.importPath = path.resolve(typeFileName);
|
2017-06-27 18:39:39 -07:00
|
|
|
}
|
|
|
|
|
_transform(file, enc, cb) {
|
|
|
|
|
if (file.path === this.target) {
|
|
|
|
|
let contents = file.contents.toString();
|
2017-07-14 16:58:54 -07:00
|
|
|
let html = parse5.parse(contents, {locationInfo: true});
|
2017-06-27 18:39:39 -07:00
|
|
|
let firstImport = dom5.query(html, firstImportFinder);
|
|
|
|
|
if (firstImport) {
|
|
|
|
|
let importPath = path.relative(path.dirname(this.target), this.importPath);
|
|
|
|
|
let importLink = dom5.constructors.element('link');
|
|
|
|
|
dom5.setAttribute(importLink, 'rel', 'import');
|
|
|
|
|
dom5.setAttribute(importLink, 'href', importPath);
|
|
|
|
|
dom5.insertBefore(firstImport.parentNode, firstImport, importLink);
|
2017-07-14 16:58:54 -07:00
|
|
|
dom5.removeFakeRootElements(html);
|
|
|
|
|
file.contents = Buffer(parse5.serialize(html));
|
2017-06-27 18:39:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cb(null, file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gulp.task('clean', () => del([DIST_DIR, 'closure.log']));
|
2017-04-25 12:22:43 -07:00
|
|
|
|
2017-12-18 23:03:02 +01:00
|
|
|
gulp.task('closure', ['generate-externs'], () => {
|
2017-01-18 14:24:27 -08:00
|
|
|
|
2017-06-29 16:47:17 -07:00
|
|
|
let entry, splitRx, joinRx, addClosureTypes;
|
2017-03-29 15:52:01 -07:00
|
|
|
|
2017-05-24 16:54:24 -07:00
|
|
|
function config(path) {
|
|
|
|
|
entry = path;
|
|
|
|
|
joinRx = new RegExp(path.split('/').join('\\/'));
|
|
|
|
|
splitRx = new RegExp(joinRx.source + '_script_\\d+\\.js$');
|
2017-10-09 12:42:16 -07:00
|
|
|
addClosureTypes = new AddClosureTypeImport(entry, 'externs/polymer-internal-types.html');
|
2017-03-29 15:52:01 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-20 17:28:36 +02:00
|
|
|
config('polymer-legacy.js');
|
2017-03-29 15:52:01 -07:00
|
|
|
|
2017-01-18 14:24:27 -08:00
|
|
|
const project = new PolymerProject({
|
2017-04-25 12:22:43 -07:00
|
|
|
shell: `./${entry}`,
|
|
|
|
|
fragments: [
|
2018-04-20 17:32:30 +02:00
|
|
|
'node_modules/@webcomponents/shadycss/entrypoints/apply-shim.js',
|
|
|
|
|
'node_modules/@webcomponents/shadycss/entrypoints/custom-style-interface.js'
|
2017-06-27 18:39:39 -07:00
|
|
|
],
|
|
|
|
|
extraDependencies: [
|
2017-06-29 16:47:17 -07:00
|
|
|
addClosureTypes.importPath,
|
2017-10-09 12:42:16 -07:00
|
|
|
'externs/polymer-internal-shared-types.js',
|
2017-04-25 12:22:43 -07:00
|
|
|
]
|
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) {
|
2017-04-19 11:51:21 -07:00
|
|
|
let chalk = require('chalk');
|
|
|
|
|
// write out log to use with diffing tools later
|
|
|
|
|
fs.writeFileSync('closure.log', chalk.stripColor(log));
|
2017-06-29 16:47:17 -07:00
|
|
|
console.error(log);
|
|
|
|
|
process.exit(-1);
|
2017-04-18 11:29:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let closurePluginOptions;
|
|
|
|
|
|
|
|
|
|
if (CLOSURE_LINT_ONLY) {
|
|
|
|
|
closurePluginOptions = {
|
|
|
|
|
logger: closureLintLogger
|
2017-10-09 12:42:16 -07:00
|
|
|
};
|
2017-04-18 11:29:33 -07:00
|
|
|
}
|
|
|
|
|
|
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-05-02 16:19:31 -07:00
|
|
|
isolation_mode: 'IIFE',
|
2017-03-29 15:52:01 -07:00
|
|
|
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-06-20 17:30:57 -07:00
|
|
|
polymer_version: 2,
|
2017-03-29 15:52:01 -07:00
|
|
|
externs: [
|
2017-04-18 12:55:06 -07:00
|
|
|
'bower_components/shadycss/externs/shadycss-externs.js',
|
2017-03-29 15:52:01 -07:00
|
|
|
'externs/webcomponents-externs.js',
|
2017-03-30 11:08:58 -07:00
|
|
|
'externs/closure-types.js',
|
2017-06-06 11:35:38 -07:00
|
|
|
'externs/polymer-externs.js',
|
2017-03-29 15:52:01 -07:00
|
|
|
],
|
|
|
|
|
extra_annotation_name: [
|
2017-06-20 17:30:57 -07:00
|
|
|
'appliesMixin',
|
|
|
|
|
'mixinClass',
|
|
|
|
|
'mixinFunction',
|
|
|
|
|
'polymer',
|
|
|
|
|
'customElement'
|
2017-03-29 15:52:01 -07:00
|
|
|
]
|
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-10-09 12:42:16 -07:00
|
|
|
.pipe(() => new BackfillStream(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();
|
|
|
|
|
|
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-10-09 12:42:16 -07:00
|
|
|
const splitter = new HtmlSplitter();
|
2017-01-09 15:56:20 -08:00
|
|
|
return mergedFiles
|
2017-06-29 16:47:17 -07:00
|
|
|
.pipe(addClosureTypes)
|
2017-04-17 11:17:14 -07:00
|
|
|
.pipe(project.bundler())
|
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-10-09 12:42:16 -07:00
|
|
|
.pipe(gulp.dest(COMPILED_DIR));
|
2017-01-18 14:24:27 -08:00
|
|
|
});
|
|
|
|
|
|
2017-04-18 11:29:33 -07:00
|
|
|
gulp.task('lint-closure', (done) => {
|
|
|
|
|
CLOSURE_LINT_ONLY = true;
|
|
|
|
|
runseq('closure', done);
|
2017-10-09 12:42:16 -07:00
|
|
|
});
|
2017-04-18 11:29:33 -07:00
|
|
|
|
2017-04-25 12:22:43 -07:00
|
|
|
gulp.task('estimate-size', ['clean'], () => {
|
2016-09-30 17:04:22 -07:00
|
|
|
|
2017-04-25 12:22:43 -07:00
|
|
|
const babelPresets = {
|
2017-10-09 12:42:16 -07:00
|
|
|
presets: [['minify', {regexpConstructors: false, simplifyComparisons: false}]]
|
2017-04-25 12:22:43 -07:00
|
|
|
};
|
2016-02-16 11:43:39 -08:00
|
|
|
|
2017-04-25 12:22:43 -07:00
|
|
|
const project = new PolymerProject({
|
|
|
|
|
shell: POLYMER_LEGACY,
|
|
|
|
|
fragments: [
|
2018-04-20 17:32:30 +02:00
|
|
|
'node_modules/@webcomponents/shadycss/entrypoints/apply-shim.js',
|
|
|
|
|
'node_modules/@webcomponents/shadycss/entrypoints/custom-style-interface.js'
|
2017-04-25 12:22:43 -07:00
|
|
|
]
|
|
|
|
|
});
|
2016-09-30 17:04:22 -07:00
|
|
|
|
2017-04-25 12:22:43 -07:00
|
|
|
// process source files in the project
|
|
|
|
|
const sources = project.sources();
|
2016-02-16 11:43:39 -08:00
|
|
|
|
2017-04-25 12:22:43 -07:00
|
|
|
// process dependencies
|
|
|
|
|
const dependencies = project.dependencies();
|
2016-02-16 11:43:39 -08:00
|
|
|
|
2017-04-25 12:22:43 -07:00
|
|
|
// merge the source and dependencies streams to we can analyze the project
|
|
|
|
|
const mergedFiles = mergeStream(sources, dependencies);
|
2016-02-16 11:43:39 -08:00
|
|
|
|
2017-10-09 12:42:16 -07:00
|
|
|
const bundledSplitter = new HtmlSplitter();
|
2016-02-16 11:43:39 -08:00
|
|
|
|
2017-04-25 12:22:43 -07:00
|
|
|
const bundlePipe = lazypipe()
|
|
|
|
|
.pipe(() => bundledSplitter.split())
|
|
|
|
|
.pipe(() => gulpif(/\.js$/, babel(babelPresets)))
|
|
|
|
|
.pipe(() => bundledSplitter.rejoin())
|
2017-10-09 12:42:16 -07:00
|
|
|
.pipe(minimalDocument);
|
2016-02-16 11:43:39 -08:00
|
|
|
|
2017-04-25 12:22:43 -07:00
|
|
|
return mergedFiles
|
|
|
|
|
.pipe(project.bundler())
|
|
|
|
|
.pipe(gulpif(/polymer\.html$/, bundlePipe()))
|
|
|
|
|
.pipe(gulpif(/polymer\.html$/, size({ title: 'bundled size', gzip: true, showTotal: false, showFiles: true })))
|
|
|
|
|
// write to the bundled folder
|
2017-10-09 12:42:16 -07:00
|
|
|
.pipe(gulp.dest(BUNDLED_DIR));
|
2016-02-16 11:43:39 -08:00
|
|
|
});
|
|
|
|
|
|
2017-09-07 15:07:12 -07:00
|
|
|
gulp.task('lint-eslint', function() {
|
2018-04-20 17:28:36 +02:00
|
|
|
return gulp.src(['lib/**/*.js', 'test/unit/*.{html,js}', 'util/*.js'])
|
2016-02-16 11:43:39 -08:00
|
|
|
.pipe(eslint())
|
|
|
|
|
.pipe(eslint.format())
|
|
|
|
|
.pipe(eslint.failAfterError());
|
|
|
|
|
});
|
2017-04-26 14:59:34 -07:00
|
|
|
|
2017-09-07 15:07:12 -07:00
|
|
|
gulp.task('lint', (done) => {
|
|
|
|
|
runseq('lint-eslint', 'lint-closure', done);
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-09 14:17:46 -08:00
|
|
|
gulp.task('update-types', (done) => {
|
|
|
|
|
runseq('generate-externs', 'generate-typescript', done);
|
|
|
|
|
});
|
|
|
|
|
|
2017-11-10 12:17:11 -08:00
|
|
|
gulp.task('generate-externs', ['clean'], async () => {
|
2017-04-26 14:59:34 -07:00
|
|
|
let genClosure = require('@polymer/gen-closure-declarations').generateDeclarations;
|
2017-11-10 12:17:11 -08:00
|
|
|
const declarations = await genClosure();
|
|
|
|
|
await fs.writeFile('externs/closure-types.js', `${header}${declarations}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
gulp.task('generate-typescript', async () => {
|
|
|
|
|
let genTs = require('@polymer/gen-typescript-declarations').generateDeclarations;
|
|
|
|
|
await del(['types/**/*.d.ts', '!types/extra-types.d.ts']);
|
|
|
|
|
const config = await fs.readJson('gen-tsd.json');
|
|
|
|
|
const files = await genTs('.', config);
|
|
|
|
|
for (const [filePath, contents] of files) {
|
|
|
|
|
await fs.outputFile(path.join('types', filePath), contents);
|
|
|
|
|
}
|
2017-10-18 11:57:05 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
gulp.task('update-version', () => {
|
2018-04-20 17:32:30 +02:00
|
|
|
return gulp.src('lib/utils/boot.js')
|
2017-10-18 11:57:05 -07:00
|
|
|
.pipe(replace(/(window.Polymer.version = )'\d+\.\d+\.\d+'/, `$1'${require('./package.json').version}'`))
|
|
|
|
|
.pipe(gulp.dest('lib/utils'));
|
2017-11-10 12:17:11 -08:00
|
|
|
});
|