source: frontend/node_modules/workbox-build/src/lib/bundle.ts

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 Copyright 2019 Google LLC
3
4 Use of this source code is governed by an MIT-style
5 license that can be found in the LICENSE file or at
6 https://opensource.org/licenses/MIT.
7*/
8
9import {babel} from '@rollup/plugin-babel';
10import {nodeResolve} from '@rollup/plugin-node-resolve';
11import {rollup, Plugin} from 'rollup';
12import {terser} from 'rollup-plugin-terser';
13import {writeFile} from 'fs-extra';
14import omt from '@surma/rollup-plugin-off-main-thread';
15import presetEnv from '@babel/preset-env';
16import replace from '@rollup/plugin-replace';
17import tempy from 'tempy';
18import upath from 'upath';
19
20import {GeneratePartial, RequiredSWDestPartial} from '../types';
21
22interface NameAndContents {
23 contents: string | Uint8Array;
24 name: string;
25}
26
27export async function bundle({
28 babelPresetEnvTargets,
29 inlineWorkboxRuntime,
30 mode,
31 sourcemap,
32 swDest,
33 unbundledCode,
34}: Omit<GeneratePartial, 'runtimeCaching'> &
35 RequiredSWDestPartial & {unbundledCode: string}): Promise<
36 Array<NameAndContents>
37> {
38 // We need to write this to the "real" file system, as Rollup won't read from
39 // a custom file system.
40 const {dir, base} = upath.parse(swDest);
41
42 const temporaryFile = tempy.file({name: base});
43 await writeFile(temporaryFile, unbundledCode);
44
45 const plugins = [
46 nodeResolve(),
47 replace({
48 // See https://github.com/GoogleChrome/workbox/issues/2769
49 'preventAssignment': true,
50 'process.env.NODE_ENV': JSON.stringify(mode),
51 }),
52 babel({
53 babelHelpers: 'bundled',
54 // Disable the logic that checks for local Babel config files:
55 // https://github.com/GoogleChrome/workbox/issues/2111
56 babelrc: false,
57 configFile: false,
58 presets: [
59 [
60 presetEnv,
61 {
62 targets: {
63 browsers: babelPresetEnvTargets,
64 },
65 loose: true,
66 },
67 ],
68 ],
69 }),
70 ];
71
72 if (mode === 'production') {
73 plugins.push(
74 terser({
75 mangle: {
76 toplevel: true,
77 properties: {
78 regex: /(^_|_$)/,
79 },
80 },
81 }),
82 );
83 }
84
85 const rollupConfig: {
86 input: string;
87 manualChunks?: (id: string) => string | undefined;
88 plugins: Array<Plugin>;
89 } = {
90 plugins,
91 input: temporaryFile,
92 };
93
94 // Rollup will inline the runtime by default. If we don't want that, we need
95 // to add in some additional config.
96 if (!inlineWorkboxRuntime) {
97 // No lint for omt(), library has no types.
98 // eslint-disable-next-line @typescript-eslint/no-unsafe-call
99 rollupConfig.plugins.unshift(omt());
100 rollupConfig.manualChunks = (id) => {
101 return id.includes('workbox') ? 'workbox' : undefined;
102 };
103 }
104
105 const bundle = await rollup(rollupConfig);
106
107 const {output} = await bundle.generate({
108 sourcemap,
109 // Using an external Workbox runtime requires 'amd'.
110 format: inlineWorkboxRuntime ? 'es' : 'amd',
111 });
112
113 const files: Array<NameAndContents> = [];
114 for (const chunkOrAsset of output) {
115 if (chunkOrAsset.type === 'asset') {
116 files.push({
117 name: chunkOrAsset.fileName,
118 contents: chunkOrAsset.source,
119 });
120 } else {
121 let code = chunkOrAsset.code;
122
123 if (chunkOrAsset.map) {
124 const sourceMapFile = chunkOrAsset.fileName + '.map';
125 code += `//# sourceMappingURL=${sourceMapFile}\n`;
126
127 files.push({
128 name: sourceMapFile,
129 contents: chunkOrAsset.map.toString(),
130 });
131 }
132
133 files.push({
134 name: chunkOrAsset.fileName,
135 contents: code,
136 });
137 }
138 }
139
140 // Make sure that if there was a directory portion included in swDest, it's
141 // preprended to all of the generated files.
142 return files.map((file) => {
143 file.name = upath.format({
144 dir,
145 base: file.name,
146 ext: '',
147 name: '',
148 root: '',
149 });
150 return file;
151 });
152}
Note: See TracBrowser for help on using the repository browser.