-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathbuilds.ts
281 lines (254 loc) · 8.58 KB
/
builds.ts
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
275
276
277
278
279
280
281
/**
* @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
*/
import {BrowserCapability} from 'browser-capabilities';
export type JsCompileTarget = 'es5'|'es2015'|'es2016'|'es2017'|'es2018';
export interface ProjectBuildOptions {
/**
* The name of this build, used to determine the output directory name.
*/
name?: string;
/**
* A build preset for this build. A build can inherit some base configuration
* from a named preset.
*/
preset?: string;
/**
* Generate a service worker for your application to cache all files and
* assets on the client.
*
* Polymer CLI will generate a service worker for your build using the
* [sw-precache library](https://github.com/GoogleChrome/sw-precache). To
* customize your service worker, create a sw-precache-config.js file in your
* project directory that exports your configuration. See the [sw-precache
* README](https://github.com/GoogleChrome/sw-precache) for a list of all
* supported options.
*
* Note that the sw-precache library uses a cache-first strategy for maximum
* speed and makes some other assumptions about how your service worker should
* behave. Read the "Considerations" section of the sw-precache README to make
* sure that this is suitable for your application.
*/
addServiceWorker?: boolean;
/**
* If `true`, generate an [HTTP/2 Push
* Manifest](https://github.com/GoogleChrome/http2-push-manifest) for your
* application.
*/
addPushManifest?: boolean;
/**
* A config file that's passed to the [sw-precache
* library](https://github.com/GoogleChrome/sw-precache). See [its
* README](https://github.com/GoogleChrome/sw-precache) for details of the
* format of this file.
*
* Ignored if `addServiceWorker` is not `true`.
*
* Defaults to `"sw-precache-config.js`.
*/
swPrecacheConfig?: string;
/**
* Insert prefetch link elements into your fragments so that all dependencies
* are prefetched immediately. Add dependency prefetching by inserting `<link
* rel="prefetch">` tags into entrypoint and `<link rel="import">` tags into
* fragments and shell for all dependencies.
*
* Note this option may trigger duplicate requests. See
* https://github.com/Polymer/polymer-build/issues/239 for details.
*/
insertPrefetchLinks?: boolean;
/**
* By default, fragments are unbundled. This is optimal for HTTP/2-compatible
* servers and clients.
*
* If the --bundle flag is supplied, all fragments are bundled together to
* reduce the number of file requests. This is optimal for sending to clients
* or serving from servers that are not HTTP/2 compatible.
*/
bundle?: boolean|{
/** URLs of files and/or folders that should not be inlined. */
excludes?: string[],
/** Inline external CSS file contents into <style> tags. */
inlineCss?: boolean,
/** Inline external Javascript file contents into <script> tags. */
inlineScripts?: boolean,
/** Rewrite element attributes inside of templates when inlining html. */
rewriteUrlsInTemplates?: boolean,
/** Create identity source maps for inline scripts. */
sourcemaps?: boolean,
/**
* Remove all comments except those tagged '@license', or starting with
* `<!--!` or `<!--#`, when true.
*/
stripComments?: boolean,
/** Remove unreachable/unused code when bundling. */
treeshake?: boolean,
};
/** Options for processing HTML. */
html?: {
/** Minify HTMl by removing comments and whitespace. */
minify?: boolean|{
/** HTML files listed here will not be minified. */
exclude?: string[],
},
};
/** Options for processing CSS. */
css?: {
/** Minify inlined and external CSS. */
minify?: boolean|{
/** CSS files listed here will not be minified. */
exclude?: string[],
},
};
/** Options for processing JavaScript. */
js?: {
/** Minify inlined and external JavaScript. */
minify?: boolean|{
/** JavaScript files listed here will not be minified. */
exclude?: string[],
},
/** Use babel to compile all ES6 JS down to ES5 for older browsers. */
compile?:
boolean|JsCompileTarget|{
target?: JsCompileTarget;
/** JavaScript files listed here will not be compiled. */
exclude?: string[],
},
/** Transform ES modules to AMD modules. */
transformModulesToAmd?: boolean,
};
/**
* Capabilities required for a browser to consume this build. Values include
* `es2015` and `push`. See canonical list at:
* https://github.com/Polymer/prpl-server-node/blob/master/src/capabilities.ts
*
* This field is purely a hint to servers reading this configuration, and
* does not affect the build process. A server supporting differential
* serving (e.g. prpl-server) can use this field to help decide which build
* to serve to a given user agent.
*/
browserCapabilities?: BrowserCapability[];
/**
* Update the entrypoint's `<base>` tag, to support serving this build from a
* non-root path, such as when doing differential serving based on user
* agent. Requires that a `<base>` tag already exists. This works well in
* conjunction with the convention of using relative URLs for static
* resources and absolute URLs for application routes.
*
* If `true`, use the build `name`. If a `string`, use that value.
* Leading/trailing slashes are optional.
*/
basePath?: boolean|string;
}
export const buildPresets = new Map<string, ProjectBuildOptions>([
[
'es5-bundled',
{
name: 'es5-bundled',
js: {
minify: true,
compile: 'es5',
transformModulesToAmd: true,
},
css: {minify: true},
html: {minify: true},
bundle: true,
addServiceWorker: true,
addPushManifest: false,
}
],
[
'es6-bundled',
{
name: 'es6-bundled',
browserCapabilities: ['es2015'],
js: {
minify: true,
compile: 'es2015',
transformModulesToAmd: true,
},
css: {minify: true},
html: {minify: true},
bundle: true,
addServiceWorker: true,
addPushManifest: false,
}
],
[
'es6-unbundled',
{
name: 'es6-unbundled',
browserCapabilities: ['es2015', 'push'],
js: {
minify: true,
compile: 'es2015',
transformModulesToAmd: true,
},
css: {minify: true},
html: {minify: true},
bundle: false,
addServiceWorker: true,
addPushManifest: true,
}
],
[
'uncompiled-bundled',
{
name: 'uncompiled-bundled',
browserCapabilities: ['es2018', 'modules'],
js: {minify: true},
css: {minify: true},
html: {minify: true},
bundle: true,
addServiceWorker: true,
addPushManifest: false,
}
],
[
'uncompiled-unbundled',
{
name: 'uncompiled-unbundled',
browserCapabilities: ['es2018', 'modules', 'push'],
js: {minify: true},
css: {minify: true},
html: {minify: true},
bundle: false,
addServiceWorker: true,
addPushManifest: true,
}
],
]);
export function isValidPreset(presetName: string) {
return buildPresets.has(presetName);
}
/**
* Apply a build preset (if a valid one exists on the config object) by
* deep merging the given config with the preset values.
*/
export function applyBuildPreset(config: ProjectBuildOptions) {
const presetName = config.preset;
if (!presetName || !isValidPreset(presetName)) {
return config;
}
const presetConfig = buildPresets.get(presetName) || {};
const mergedConfig = Object.assign({}, presetConfig, config);
// Object.assign is shallow, so we need to make sure we properly merge these
// deep options as well.
// NOTE(fks) 05-05-2017: While a little annoying, we use multiple
// Object.assign() calls here so that we do not filter-out additional
// user-defined build options at the config level.
mergedConfig.js = Object.assign({}, presetConfig.js, config.js);
mergedConfig.css = Object.assign({}, presetConfig.css, config.css);
mergedConfig.html = Object.assign({}, presetConfig.html, config.html);
return mergedConfig;
}