1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import * as assert from 'assert';
|
18 | import {writeFile} from 'fs';
|
19 | import * as path from 'path';
|
20 | import * as logging from 'plylog';
|
21 | import {generate as swPrecacheGenerate, SWConfig} from 'sw-precache';
|
22 |
|
23 | import {DepsIndex} from './analyzer';
|
24 | import {LocalFsPath, posixifyPath, PosixPath} from './path-transformers';
|
25 | import {PolymerProject} from './polymer-project';
|
26 |
|
27 | const logger = logging.getLogger('polymer-build.service-worker');
|
28 |
|
29 | export interface AddServiceWorkerOptions {
|
30 | project: PolymerProject;
|
31 | buildRoot: LocalFsPath;
|
32 | bundled?: boolean;
|
33 | path?: LocalFsPath;
|
34 | swPrecacheConfig?: SWConfig|null;
|
35 | basePath?: LocalFsPath;
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function fixDeprecatedOptions(options: any): AddServiceWorkerOptions {
|
44 | if (typeof options.serviceWorkerPath !== 'undefined') {
|
45 | logger.warn(
|
46 | '"serviceWorkerPath" config option has been renamed to "path" and will no longer be supported in future versions');
|
47 | options.path = options.path || options.serviceWorkerPath;
|
48 | }
|
49 | if (typeof options.swConfig !== 'undefined') {
|
50 | logger.warn(
|
51 | '"swConfig" config option has been renamed to "swPrecacheConfig" and will no longer be supported in future versions');
|
52 | options.swPrecacheConfig = options.swPrecacheConfig || options.swConfig;
|
53 | }
|
54 | return options;
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function getPrecachedAssets(
|
62 | depsIndex: DepsIndex, project: PolymerProject): string[] {
|
63 | const precachedAssets = new Set<string>(project.config.allFragments);
|
64 | precachedAssets.add(project.config.entrypoint);
|
65 |
|
66 | for (const depImports of depsIndex.fragmentToFullDeps.values()) {
|
67 | depImports.imports.forEach((s) => precachedAssets.add(s));
|
68 | depImports.scripts.forEach((s) => precachedAssets.add(s));
|
69 | depImports.styles.forEach((s) => precachedAssets.add(s));
|
70 | }
|
71 |
|
72 | return Array.from(precachedAssets);
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function getBundledPrecachedAssets(project: PolymerProject) {
|
80 | const precachedAssets = new Set<string>(project.config.allFragments);
|
81 | precachedAssets.add(project.config.entrypoint);
|
82 |
|
83 | return Array.from(precachedAssets);
|
84 | }
|
85 |
|
86 |
|
87 | export const hasNoFileExtension = /\/[^\/\.]*(\?|$)/;
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | export async function generateServiceWorkerConfig(
|
94 | options: AddServiceWorkerOptions): Promise<SWConfig> {
|
95 | assert(!!options, '`project` & `buildRoot` options are required');
|
96 | assert(!!options.project, '`project` option is required');
|
97 | assert(!!options.buildRoot, '`buildRoot` option is required');
|
98 | options = fixDeprecatedOptions(options);
|
99 |
|
100 | options = Object.assign({}, options);
|
101 | const project = options.project;
|
102 | const buildRoot = options.buildRoot;
|
103 | const swPrecacheConfig: SWConfig =
|
104 | Object.assign({}, options.swPrecacheConfig);
|
105 |
|
106 | const depsIndex = await project.analyzer.analyzeDependencies;
|
107 | let staticFileGlobs = Array.from(swPrecacheConfig.staticFileGlobs || []);
|
108 | const precachedAssets = (options.bundled) ?
|
109 | getBundledPrecachedAssets(project) :
|
110 | getPrecachedAssets(depsIndex, project);
|
111 |
|
112 | staticFileGlobs = staticFileGlobs.concat(precachedAssets);
|
113 | staticFileGlobs = staticFileGlobs.map((filePath: string) => {
|
114 | if (filePath.startsWith(project.config.root)) {
|
115 | filePath = filePath.substring(project.config.root.length);
|
116 | }
|
117 | return path.join(buildRoot, filePath);
|
118 | });
|
119 |
|
120 | if (swPrecacheConfig.navigateFallback === undefined) {
|
121 |
|
122 | swPrecacheConfig.navigateFallback =
|
123 | path.relative(project.config.root, project.config.entrypoint);
|
124 | }
|
125 |
|
126 | if (swPrecacheConfig.navigateFallbackWhitelist === undefined) {
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | swPrecacheConfig.navigateFallbackWhitelist = [hasNoFileExtension];
|
132 | }
|
133 |
|
134 | if (swPrecacheConfig.directoryIndex === undefined) {
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | swPrecacheConfig.directoryIndex = '';
|
141 | }
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | swPrecacheConfig.stripPrefix = addTrailingSlash(posixifyPath(buildRoot));
|
152 |
|
153 | if (options.basePath) {
|
154 |
|
155 | let replacePrefix = posixifyPath(options.basePath);
|
156 | if (!replacePrefix.endsWith('/')) {
|
157 | replacePrefix = replacePrefix + '/' as PosixPath;
|
158 | }
|
159 | if (swPrecacheConfig.replacePrefix) {
|
160 | console.info(
|
161 | `Replacing service worker configuration's ` +
|
162 | `replacePrefix option (${swPrecacheConfig.replacePrefix}) ` +
|
163 | `with the build configuration's basePath (${replacePrefix}).`);
|
164 | }
|
165 | swPrecacheConfig.replacePrefix = replacePrefix;
|
166 | }
|
167 |
|
168 |
|
169 | swPrecacheConfig.staticFileGlobs = staticFileGlobs;
|
170 |
|
171 |
|
172 | swPrecacheConfig.logger = swPrecacheConfig.logger || logger.debug;
|
173 |
|
174 | return swPrecacheConfig;
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | export async function generateServiceWorker(options: AddServiceWorkerOptions):
|
182 | Promise<Buffer> {
|
183 | const swPrecacheConfig = await generateServiceWorkerConfig(options);
|
184 | return await<Promise<Buffer>>(new Promise((resolve, reject) => {
|
185 | logger.debug(`writing service worker...`, swPrecacheConfig);
|
186 | swPrecacheGenerate(
|
187 | swPrecacheConfig, (err?: Error, fileContents?: string) => {
|
188 | if (err || fileContents == null) {
|
189 | reject(err || 'No file contents provided.');
|
190 | } else {
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | fileContents = fileContents.replace(/\bfunction\(/g, 'function (');
|
197 | resolve(Buffer.from(fileContents));
|
198 | }
|
199 | });
|
200 | }));
|
201 | }
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | export function addServiceWorker(options: AddServiceWorkerOptions):
|
210 | Promise<void> {
|
211 | return generateServiceWorker(options).then((fileContents: Buffer) => {
|
212 | return new Promise<void>((resolve, reject) => {
|
213 | const serviceWorkerPath =
|
214 | path.join(options.buildRoot, options.path || 'service-worker.js');
|
215 | writeFile(serviceWorkerPath, fileContents, (err) => {
|
216 | if (err) {
|
217 | reject(err);
|
218 | } else {
|
219 | resolve();
|
220 | }
|
221 | });
|
222 | });
|
223 | });
|
224 | }
|
225 |
|
226 | function addTrailingSlash(s: string): string {
|
227 | return s.endsWith('/') ? s : s + '/';
|
228 | }
|
229 |
|
\ | No newline at end of file |