1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const cssSlam = require("css-slam");
|
17 | const gulpif = require("gulp-if");
|
18 | const logging = require("plylog");
|
19 | const stream_1 = require("stream");
|
20 | const matcher = require("matcher");
|
21 | const js_transform_1 = require("./js-transform");
|
22 | const html_transform_1 = require("./html-transform");
|
23 | const html_splitter_1 = require("./html-splitter");
|
24 | const logger = logging.getLogger('cli.build.optimize-streams');
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | class GenericOptimizeTransform extends stream_1.Transform {
|
33 | constructor(optimizerName, optimizer) {
|
34 | super({ objectMode: true });
|
35 | this.optimizer = optimizer;
|
36 | this.optimizerName = optimizerName;
|
37 | }
|
38 | _transform(file, _encoding, callback) {
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | if (!file.path || file.path.indexOf('webcomponentsjs/') >= 0 ||
|
44 | file.path.indexOf('webcomponentsjs\\') >= 0) {
|
45 | callback(undefined, file);
|
46 | return;
|
47 | }
|
48 | if (file.contents) {
|
49 | try {
|
50 | let contents = file.contents.toString();
|
51 | contents = this.optimizer(contents, file);
|
52 | file.contents = Buffer.from(contents);
|
53 | }
|
54 | catch (error) {
|
55 | logger.warn(`${this.optimizerName}: Unable to optimize ${file.path}`, { err: error.message || error });
|
56 | }
|
57 | }
|
58 | callback(undefined, file);
|
59 | }
|
60 | }
|
61 | exports.GenericOptimizeTransform = GenericOptimizeTransform;
|
62 | function getCompileTarget(file, options) {
|
63 | let target;
|
64 | const compileOptions = options.compile;
|
65 | if (notExcluded(options.compile)(file)) {
|
66 | if (typeof compileOptions === 'object') {
|
67 | target =
|
68 | (compileOptions.target === undefined) ? true : compileOptions.target;
|
69 | }
|
70 | else {
|
71 | target = compileOptions;
|
72 | }
|
73 | if (target === undefined) {
|
74 | target = false;
|
75 | }
|
76 | }
|
77 | else {
|
78 | target = false;
|
79 | }
|
80 | return target;
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 | class JsTransform extends GenericOptimizeTransform {
|
86 | constructor(options) {
|
87 | const jsOptions = options.js || {};
|
88 | const shouldMinifyFile = jsOptions.minify ? notExcluded(jsOptions.minify) : () => false;
|
89 | const transformer = (content, file) => {
|
90 | let transformModulesToAmd = false;
|
91 | if (jsOptions.transformModulesToAmd) {
|
92 | if (html_splitter_1.isHtmlSplitterFile(file)) {
|
93 |
|
94 |
|
95 | transformModulesToAmd = file.isModule === true;
|
96 | }
|
97 | else {
|
98 |
|
99 |
|
100 | transformModulesToAmd = 'auto';
|
101 | }
|
102 | }
|
103 | return js_transform_1.jsTransform(content, {
|
104 | compile: getCompileTarget(file, jsOptions),
|
105 | externalHelpers: true,
|
106 | minify: shouldMinifyFile(file),
|
107 | moduleResolution: jsOptions.moduleResolution,
|
108 | filePath: file.path,
|
109 | rootDir: options.rootDir,
|
110 | transformModulesToAmd,
|
111 | });
|
112 | };
|
113 | super('js-transform', transformer);
|
114 | }
|
115 | }
|
116 | exports.JsTransform = JsTransform;
|
117 |
|
118 |
|
119 |
|
120 | class HtmlTransform extends GenericOptimizeTransform {
|
121 | constructor(options) {
|
122 | const jsOptions = options.js || {};
|
123 | const shouldMinifyFile = options.html && options.html.minify ?
|
124 | notExcluded(options.html.minify) :
|
125 | () => false;
|
126 | const transformer = (content, file) => {
|
127 | const transformModulesToAmd = options.js && options.js.transformModulesToAmd;
|
128 | const isEntryPoint = !!options.entrypointPath && file.path === options.entrypointPath;
|
129 | let injectBabelHelpers = 'none';
|
130 | let injectRegeneratorRuntime = false;
|
131 | if (isEntryPoint) {
|
132 | const compileTarget = getCompileTarget(file, jsOptions);
|
133 | switch (compileTarget) {
|
134 | case 'es5':
|
135 | case true:
|
136 | injectBabelHelpers = 'full';
|
137 | injectRegeneratorRuntime = true;
|
138 | break;
|
139 | case 'es2015':
|
140 | case 'es2016':
|
141 | case 'es2017':
|
142 | injectBabelHelpers = 'full';
|
143 | injectRegeneratorRuntime = false;
|
144 | break;
|
145 | case 'es2018':
|
146 | case false:
|
147 | injectBabelHelpers = transformModulesToAmd ? 'amd' : 'none';
|
148 | injectRegeneratorRuntime = false;
|
149 | break;
|
150 | default:
|
151 | const never = compileTarget;
|
152 | throw new Error(`Unexpected compile target ${never}`);
|
153 | }
|
154 | }
|
155 | return html_transform_1.htmlTransform(content, {
|
156 | js: {
|
157 | transformModulesToAmd,
|
158 | externalHelpers: true,
|
159 | },
|
160 | minifyHtml: shouldMinifyFile(file),
|
161 | injectBabelHelpers,
|
162 | injectRegeneratorRuntime,
|
163 | injectAmdLoader: isEntryPoint && transformModulesToAmd,
|
164 | });
|
165 | };
|
166 | super('html-transform', transformer);
|
167 | }
|
168 | }
|
169 | exports.HtmlTransform = HtmlTransform;
|
170 |
|
171 |
|
172 |
|
173 | class CSSMinifyTransform extends GenericOptimizeTransform {
|
174 | constructor(options) {
|
175 | super('css-slam-minify', cssSlam.css);
|
176 | this.options = options;
|
177 | }
|
178 | _transform(file, encoding, callback) {
|
179 |
|
180 | if (this.options.stripWhitespace) {
|
181 | super._transform(file, encoding, callback);
|
182 | }
|
183 | }
|
184 | }
|
185 | exports.CSSMinifyTransform = CSSMinifyTransform;
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | class InlineCSSOptimizeTransform extends GenericOptimizeTransform {
|
191 | constructor(options) {
|
192 | super('css-slam-inline', cssSlam.html);
|
193 | this.options = options;
|
194 | }
|
195 | _transform(file, encoding, callback) {
|
196 |
|
197 | if (this.options.stripWhitespace) {
|
198 | super._transform(file, encoding, callback);
|
199 | }
|
200 | }
|
201 | }
|
202 | exports.InlineCSSOptimizeTransform = InlineCSSOptimizeTransform;
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | function getOptimizeStreams(options) {
|
208 | options = options || {};
|
209 | const streams = [];
|
210 | streams.push(gulpif(matchesExt('.js'), new JsTransform(options)));
|
211 | streams.push(gulpif(matchesExt('.html'), new HtmlTransform(options)));
|
212 | if (options.css && options.css.minify) {
|
213 | streams.push(gulpif(matchesExtAndNotExcluded('.css', options.css.minify), new CSSMinifyTransform({ stripWhitespace: true })));
|
214 |
|
215 |
|
216 | streams.push(gulpif(matchesExtAndNotExcluded('.html', options.css.minify), new InlineCSSOptimizeTransform({ stripWhitespace: true })));
|
217 | }
|
218 | return streams;
|
219 | }
|
220 | exports.getOptimizeStreams = getOptimizeStreams;
|
221 | function matchesExt(extension) {
|
222 | return (fs) => !!fs.path && fs.relative.endsWith(extension);
|
223 | }
|
224 | exports.matchesExt = matchesExt;
|
225 | function notExcluded(option) {
|
226 | const exclude = typeof option === 'object' && option.exclude || [];
|
227 | return (fs) => !exclude.some((pattern) => matcher.isMatch(fs.relative, pattern));
|
228 | }
|
229 | function matchesExtAndNotExcluded(extension, option) {
|
230 | const a = matchesExt(extension);
|
231 | const b = notExcluded(option);
|
232 | return (fs) => a(fs) && b(fs);
|
233 | }
|
234 |
|
\ | No newline at end of file |