-
Notifications
You must be signed in to change notification settings - Fork 2k
/
gulpfile.js
274 lines (235 loc) · 8.76 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* @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
*/
/* eslint-env node */
'use strict';
const gulp = require('gulp');
const gulpif = require('gulp-if');
const del = require('del');
const eslint = require('gulp-eslint');
const fs = require('fs-extra');
const path = require('path');
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();
const minimalDocument = require('./util/minimalDocument.js');
const dom5 = require('dom5/lib/index-next');
const parse5 = require('parse5');
const replace = require('gulp-replace');
const DIST_DIR = 'dist';
const BUNDLED_DIR = path.join(DIST_DIR, 'bundled');
const COMPILED_DIR = path.join(DIST_DIR, 'compiled');
const POLYMER_LEGACY = 'polymer-legacy.js';
const {PolymerProject, HtmlSplitter} = require('polymer-build');
const {Transform} = require('stream');
class BackfillStream extends Transform {
constructor(fileList) {
super({objectMode: true});
this.fileList = fileList;
}
_transform(file, enc, cb) {
if (this.fileList) {
const origFile = this.fileList.shift();
// console.log(`rename ${file.path} -> ${origFile.path}`)
file.path = origFile.path;
}
cb(null, file);
}
_flush(cb) {
if (this.fileList && 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);
});
}
cb();
}
}
let firstImportFinder = dom5.predicates.AND(
dom5.predicates.hasTagName('link'), dom5.predicates.hasAttrValue('rel', 'import')
);
const header =
`/**
* @fileoverview Generated typings for Polymer mixins
* @externs
* @suppress {checkPrototypalTypes}
*
* @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
*/
/* eslint-disable */
`;
class AddClosureTypeImport extends Transform {
constructor(entryFileName, typeFileName) {
super({objectMode: true});
this.target = path.resolve(entryFileName);
this.importPath = path.resolve(typeFileName);
}
_transform(file, enc, cb) {
if (file.path === this.target) {
let contents = file.contents.toString();
let html = parse5.parse(contents, {locationInfo: true});
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);
dom5.removeFakeRootElements(html);
file.contents = Buffer(parse5.serialize(html));
}
}
cb(null, file);
}
}
gulp.task('clean', () => del([DIST_DIR, 'closure.log']));
gulp.task('generate-externs', gulp.series('clean', async () => {
let genClosure = require('@polymer/gen-closure-declarations').generateDeclarations;
const declarations = await genClosure();
await fs.writeFile('externs/closure-types.js', `${header}${declarations}`);
}));
const runClosureOnly = ({lintOnly}) => () => {
let entry, splitRx, joinRx, addClosureTypes;
function config(path) {
entry = path;
joinRx = new RegExp(path.split('/').join('\\/'));
splitRx = new RegExp(joinRx.source + '_script_\\d+\\.js$');
addClosureTypes = new AddClosureTypeImport(entry, 'externs/polymer-internal-types.html');
}
config('polymer-legacy.js');
const project = new PolymerProject({
shell: `./${entry}`,
fragments: [
'node_modules/@webcomponents/shadycss/entrypoints/apply-shim.js',
'node_modules/@webcomponents/shadycss/entrypoints/custom-style-interface.js'
],
extraDependencies: [
addClosureTypes.importPath,
'externs/polymer-internal-shared-types.js',
]
});
function closureLintLogger(log) {
let chalk = require('chalk');
// write out log to use with diffing tools later
fs.writeFileSync('closure.log', chalk.stripColor(log));
console.error(log);
process.exit(-1);
}
let closurePluginOptions;
if (lintOnly) {
closurePluginOptions = {
logger: closureLintLogger
};
}
const closureStream = closure({
compilation_level: 'ADVANCED',
language_in: 'ES6_STRICT',
language_out: 'ES5_STRICT',
warning_level: 'VERBOSE',
isolation_mode: 'IIFE',
assume_function_wrapper: true,
rewrite_polyfills: false,
new_type_inf: true,
checks_only: lintOnly,
polymer_version: 2,
externs: [
'bower_components/shadycss/externs/shadycss-externs.js',
'externs/webcomponents-externs.js',
'externs/closure-types.js',
'externs/polymer-externs.js',
'externs/polymer-dom-api-externs.js',
],
extra_annotation_name: [
'appliesMixin',
'mixinClass',
'mixinFunction',
'polymer',
'customElement'
]
}, closurePluginOptions);
const closurePipeline = lazypipe()
.pipe(() => closureStream)
.pipe(() => new BackfillStream(closureStream.fileList_));
// 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 splitter = new HtmlSplitter();
return mergedFiles
.pipe(addClosureTypes)
.pipe(project.bundler())
.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})))
.pipe(gulp.dest(COMPILED_DIR));
};
gulp.task('closure', gulp.series('generate-externs', runClosureOnly({
lintOnly: false
})));
gulp.task('lint-closure', runClosureOnly({
lintOnly: true
}));
gulp.task('estimate-size', gulp.series('clean', () => {
const babelPresets = {
presets: [['minify', {regexpConstructors: false, simplifyComparisons: false}]]
};
const project = new PolymerProject({
shell: POLYMER_LEGACY,
fragments: [
'node_modules/@webcomponents/shadycss/entrypoints/apply-shim.js',
'node_modules/@webcomponents/shadycss/entrypoints/custom-style-interface.js'
]
});
// 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 bundledSplitter = new HtmlSplitter();
const bundlePipe = lazypipe()
.pipe(() => bundledSplitter.split())
.pipe(() => gulpif(/\.js$/, babel(babelPresets)))
.pipe(() => bundledSplitter.rejoin())
.pipe(minimalDocument);
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
.pipe(gulp.dest(BUNDLED_DIR));
}));
gulp.task('lint-eslint', function() {
return gulp.src(['lib/**/*.js', 'test/unit/*.{html,js}', 'util/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
// TODO(timvdlippe): Add back `, 'lint-closure'` once closure lint works again
gulp.task('lint', gulp.series('lint-eslint'));
// TODO(timvdlippe): Add back `'generate-externs',` once we can generate externs again
gulp.task('update-version', () => {
return gulp.src('lib/mixins/element-mixin.js')
.pipe(replace(/(export const version = )'\d+\.\d+\.\d+'/, `$1'${require('./package.json').version}'`))
.pipe(gulp.dest('lib/mixins'));
});