1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | import * as cssSlam from 'css-slam';
|
16 | import * as gulpif from 'gulp-if';
|
17 | import * as logging from 'plylog';
|
18 | import {JsCompileTarget, ModuleResolutionStrategy} from 'polymer-project-config';
|
19 | import {Transform} from 'stream';
|
20 | import * as vinyl from 'vinyl';
|
21 |
|
22 | import matcher = require('matcher');
|
23 |
|
24 | import {jsTransform} from './js-transform';
|
25 | import {htmlTransform} from './html-transform';
|
26 | import {isHtmlSplitterFile} from './html-splitter';
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | import File = require('vinyl');
|
32 |
|
33 | const logger = logging.getLogger('cli.build.optimize-streams');
|
34 |
|
35 | export type FileCB = (error?: Error, file?: File) => void;
|
36 | export type CSSOptimizeOptions = {
|
37 | stripWhitespace?: boolean;
|
38 | };
|
39 | export interface OptimizeOptions {
|
40 | html?: {
|
41 | minify?: boolean|{exclude?: string[]},
|
42 | };
|
43 | css?: {
|
44 | minify?: boolean|{exclude?: string[]},
|
45 | };
|
46 | js?: JsOptimizeOptions;
|
47 | entrypointPath?: string;
|
48 | rootDir?: string;
|
49 | }
|
50 |
|
51 | export type JsCompileOptions = boolean|JsCompileTarget|{
|
52 | target?: JsCompileTarget;
|
53 | exclude?: string[];
|
54 | };
|
55 |
|
56 | export interface JsOptimizeOptions {
|
57 | minify?: boolean|{exclude?: string[]};
|
58 | compile?: JsCompileOptions;
|
59 | moduleResolution?: ModuleResolutionStrategy;
|
60 | transformModulesToAmd?: boolean;
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | export class GenericOptimizeTransform extends Transform {
|
71 | optimizer: (content: string, file: File) => string;
|
72 | optimizerName: string;
|
73 |
|
74 | constructor(
|
75 | optimizerName: string,
|
76 | optimizer: (content: string, file: File) => string) {
|
77 | super({objectMode: true});
|
78 | this.optimizer = optimizer;
|
79 | this.optimizerName = optimizerName;
|
80 | }
|
81 |
|
82 | _transform(file: File, _encoding: string, callback: FileCB): void {
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | if (!file.path || file.path.indexOf('webcomponentsjs/') >= 0 ||
|
88 | file.path.indexOf('webcomponentsjs\\') >= 0) {
|
89 | callback(undefined, file);
|
90 | return;
|
91 | }
|
92 |
|
93 | if (file.contents) {
|
94 | try {
|
95 | let contents = file.contents.toString();
|
96 | contents = this.optimizer(contents, file);
|
97 | file.contents = Buffer.from(contents);
|
98 | } catch (error) {
|
99 | logger.warn(
|
100 | `${this.optimizerName}: Unable to optimize ${file.path}`,
|
101 | {err: error.message || error});
|
102 | }
|
103 | }
|
104 | callback(undefined, file);
|
105 | }
|
106 | }
|
107 |
|
108 | function getCompileTarget(
|
109 | file: vinyl, options: JsOptimizeOptions): JsCompileTarget|boolean {
|
110 | let target: JsCompileTarget|boolean|undefined;
|
111 | const compileOptions = options.compile;
|
112 | if (notExcluded(options.compile)(file)) {
|
113 | if (typeof compileOptions === 'object') {
|
114 | target =
|
115 | (compileOptions.target === undefined) ? true : compileOptions.target;
|
116 | } else {
|
117 | target = compileOptions;
|
118 | }
|
119 | if (target === undefined) {
|
120 | target = false;
|
121 | }
|
122 | } else {
|
123 | target = false;
|
124 | }
|
125 | return target;
|
126 | }
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | export class JsTransform extends GenericOptimizeTransform {
|
132 | constructor(options: OptimizeOptions) {
|
133 | const jsOptions: JsOptimizeOptions = options.js || {};
|
134 |
|
135 | const shouldMinifyFile =
|
136 | jsOptions.minify ? notExcluded(jsOptions.minify) : () => false;
|
137 |
|
138 | const transformer = (content: string, file: File) => {
|
139 | let transformModulesToAmd: boolean|'auto' = false;
|
140 |
|
141 | if (jsOptions.transformModulesToAmd) {
|
142 | if (isHtmlSplitterFile(file)) {
|
143 |
|
144 |
|
145 | transformModulesToAmd = file.isModule === true;
|
146 | } else {
|
147 |
|
148 |
|
149 | transformModulesToAmd = 'auto';
|
150 | }
|
151 | }
|
152 |
|
153 | return jsTransform(content, {
|
154 | compile: getCompileTarget(file, jsOptions),
|
155 | externalHelpers: true,
|
156 | minify: shouldMinifyFile(file),
|
157 | moduleResolution: jsOptions.moduleResolution,
|
158 | filePath: file.path,
|
159 | rootDir: options.rootDir,
|
160 | transformModulesToAmd,
|
161 | });
|
162 | };
|
163 |
|
164 | super('js-transform', transformer);
|
165 | }
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | export class HtmlTransform extends GenericOptimizeTransform {
|
172 | constructor(options: OptimizeOptions) {
|
173 | const jsOptions: JsOptimizeOptions = options.js || {};
|
174 |
|
175 | const shouldMinifyFile = options.html && options.html.minify ?
|
176 | notExcluded(options.html.minify) :
|
177 | () => false;
|
178 |
|
179 | const transformer = (content: string, file: File) => {
|
180 | const transformModulesToAmd =
|
181 | options.js && options.js.transformModulesToAmd;
|
182 | const isEntryPoint =
|
183 | !!options.entrypointPath && file.path === options.entrypointPath;
|
184 |
|
185 | let injectBabelHelpers: 'none'|'full'|'amd' = 'none';
|
186 | let injectRegeneratorRuntime = false;
|
187 | if (isEntryPoint) {
|
188 | const compileTarget = getCompileTarget(file, jsOptions);
|
189 | switch (compileTarget) {
|
190 | case 'es5':
|
191 | case true:
|
192 | injectBabelHelpers = 'full';
|
193 | injectRegeneratorRuntime = true;
|
194 | break;
|
195 | case 'es2015':
|
196 | case 'es2016':
|
197 | case 'es2017':
|
198 | injectBabelHelpers = 'full';
|
199 | injectRegeneratorRuntime = false;
|
200 | break;
|
201 | case 'es2018':
|
202 | case false:
|
203 | injectBabelHelpers = transformModulesToAmd ? 'amd' : 'none';
|
204 | injectRegeneratorRuntime = false;
|
205 | break;
|
206 | default:
|
207 | const never: never = compileTarget;
|
208 | throw new Error(`Unexpected compile target ${never}`);
|
209 | }
|
210 | }
|
211 |
|
212 | return htmlTransform(content, {
|
213 | js: {
|
214 | transformModulesToAmd,
|
215 | externalHelpers: true,
|
216 |
|
217 |
|
218 |
|
219 | },
|
220 | minifyHtml: shouldMinifyFile(file),
|
221 | injectBabelHelpers,
|
222 | injectRegeneratorRuntime,
|
223 | injectAmdLoader: isEntryPoint && transformModulesToAmd,
|
224 | });
|
225 | };
|
226 | super('html-transform', transformer);
|
227 | }
|
228 | }
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | export class CSSMinifyTransform extends GenericOptimizeTransform {
|
234 | constructor(private options: CSSOptimizeOptions) {
|
235 | super('css-slam-minify', cssSlam.css);
|
236 | }
|
237 |
|
238 | _transform(file: File, encoding: string, callback: FileCB): void {
|
239 |
|
240 | if (this.options.stripWhitespace) {
|
241 | super._transform(file, encoding, callback);
|
242 | }
|
243 | }
|
244 | }
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | export class InlineCSSOptimizeTransform extends GenericOptimizeTransform {
|
251 | constructor(private options: CSSOptimizeOptions) {
|
252 | super('css-slam-inline', cssSlam.html);
|
253 | }
|
254 |
|
255 | _transform(file: File, encoding: string, callback: FileCB): void {
|
256 |
|
257 | if (this.options.stripWhitespace) {
|
258 | super._transform(file, encoding, callback);
|
259 | }
|
260 | }
|
261 | }
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | export function getOptimizeStreams(options?: OptimizeOptions):
|
268 | NodeJS.ReadWriteStream[] {
|
269 | options = options || {};
|
270 | const streams = [];
|
271 |
|
272 | streams.push(gulpif(matchesExt('.js'), new JsTransform(options)));
|
273 | streams.push(gulpif(matchesExt('.html'), new HtmlTransform(options)));
|
274 |
|
275 | if (options.css && options.css.minify) {
|
276 | streams.push(gulpif(
|
277 | matchesExtAndNotExcluded('.css', options.css.minify),
|
278 | new CSSMinifyTransform({stripWhitespace: true})));
|
279 |
|
280 |
|
281 | streams.push(gulpif(
|
282 | matchesExtAndNotExcluded('.html', options.css.minify),
|
283 | new InlineCSSOptimizeTransform({stripWhitespace: true})));
|
284 | }
|
285 |
|
286 | return streams;
|
287 | }
|
288 |
|
289 | export function matchesExt(extension: string) {
|
290 | return (fs: vinyl) => !!fs.path && fs.relative.endsWith(extension);
|
291 | }
|
292 |
|
293 | function notExcluded(option?: JsCompileOptions) {
|
294 | const exclude = typeof option === 'object' && option.exclude || [];
|
295 | return (fs: vinyl) => !exclude.some(
|
296 | (pattern: string) => matcher.isMatch(fs.relative, pattern));
|
297 | }
|
298 |
|
299 | function matchesExtAndNotExcluded(extension: string, option: JsCompileOptions) {
|
300 | const a = matchesExt(extension);
|
301 | const b = notExcluded(option);
|
302 | return (fs: vinyl) => a(fs) && b(fs);
|
303 | }
|