1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
10 | for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
11 | };
|
12 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
13 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
14 | };
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | exports.setupPlugins = exports.resolveFile = exports.serviceWorkerDefaultExclude = exports.serviceWorkerDefaultInclude = void 0;
|
17 | const crypto_1 = require("crypto");
|
18 | const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
|
19 | const globby_1 = __importDefault(require("globby"));
|
20 | const html_webpack_plugin_1 = __importDefault(require("html-webpack-plugin"));
|
21 | const path_1 = require("path");
|
22 |
|
23 | const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin"));
|
24 | const webpack_1 = require("webpack");
|
25 |
|
26 | const webpack_bundle_analyzer_1 = require("webpack-bundle-analyzer");
|
27 |
|
28 | const workbox_webpack_plugin_1 = require("workbox-webpack-plugin");
|
29 | const environment_1 = require("./environment");
|
30 | const rules_1 = require("./rules");
|
31 | __exportStar(require("./babel-remove-function"), exports);
|
32 | exports.serviceWorkerDefaultInclude = [
|
33 | /\.(?:html|js|json|mjs|css)$/,
|
34 | /images.+\.(?:bmp|jpg|jpeg|png|svg|webp)$/
|
35 | ];
|
36 | exports.serviceWorkerDefaultExclude = [
|
37 | /\.map$/,
|
38 | /bundle(?:-.+)?\.(?:mjs|js)$/,
|
39 | /404\.html/
|
40 | ];
|
41 | class ServiceWorkerEnvironment {
|
42 | constructor({ dest, version, debug }) {
|
43 | this.dest = dest;
|
44 | this.version = version;
|
45 | this.debug = debug;
|
46 | }
|
47 | apply(compiler) {
|
48 | const dest = this.dest;
|
49 | compiler.hooks.emit.tap('ServiceWorkerEnvironment', (current) => {
|
50 | const content = `self.__version = '${this.version}'; self.__debug = ${this.debug};`;
|
51 | current.assets[dest] = {
|
52 | source() {
|
53 | return content;
|
54 | },
|
55 | size() {
|
56 | return content.length;
|
57 | }
|
58 | };
|
59 | });
|
60 | compiler.hooks.compilation.tap('ServiceWorkerEnvironment', (current) => {
|
61 | current.cache['service-worker-environment'] = dest;
|
62 | });
|
63 | }
|
64 | }
|
65 | class HtmlWebpackTrackerPlugin {
|
66 | constructor() {
|
67 | this.files = new Map();
|
68 | }
|
69 | apply(compiler) {
|
70 | compiler.hooks.compilation.tap('HtmlWebpackTrackerPlugin', (current) => {
|
71 | const plugin = html_webpack_plugin_1.default;
|
72 | plugin
|
73 | .getHooks(current)
|
74 | .afterEmit.tap('HtmlWebpackTrackerPlugin', ({ outputName, plugin }) => {
|
75 | current.cache[`html-webpack-tracker-plugin:${plugin.options.id}`] = outputName;
|
76 | });
|
77 | });
|
78 | }
|
79 | }
|
80 | async function resolveFile(options, key, pattern) {
|
81 | var _a;
|
82 | let file = (_a = options[key]) !== null && _a !== void 0 ? _a : true;
|
83 | if (file === true) {
|
84 | file = (await globby_1.default(path_1.resolve(options.srcFolder, pattern)))[0];
|
85 | }
|
86 | return typeof file === 'string' ? file : null;
|
87 | }
|
88 | exports.resolveFile = resolveFile;
|
89 | async function setupPlugins(options) {
|
90 | var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
|
91 | const pluginsOptions = (_a = options.plugins) !== null && _a !== void 0 ? _a : {};
|
92 | const swOptions = (_b = options.serviceWorker) !== null && _b !== void 0 ? _b : {};
|
93 | const rules = (_c = options.rules) !== null && _c !== void 0 ? _c : {};
|
94 | const useTypescript = await rules_1.checkTypescript(rules, options.srcFolder);
|
95 | const analyze = (_d = pluginsOptions.analyze) !== null && _d !== void 0 ? _d : true;
|
96 | const hmr = (_f = (_e = options.server) === null || _e === void 0 ? void 0 : _e.hot) !== null && _f !== void 0 ? _f : true;
|
97 | const indexFile = await resolveFile(options, 'index', './index.html.(js|ts|jsx|tsx)');
|
98 | const error404 = await resolveFile(options, 'error404', './404.html.(js|ts|jsx|tsx)');
|
99 | const manifest = (await globby_1.default(path_1.resolve(options.srcFolder, './manifest.json.(js|ts)')))[0];
|
100 | const robots = (await globby_1.default(path_1.resolve(options.srcFolder, './robots.txt.(js|ts)')))[0];
|
101 | let plugins = [
|
102 | new webpack_1.EnvironmentPlugin({
|
103 | NODE_ENV: options.environment
|
104 | }),
|
105 | new webpack_1.DefinePlugin({
|
106 | ENV: JSON.stringify(options.env),
|
107 | VERSION: JSON.stringify(options.version),
|
108 | ICONS: JSON.stringify(options.icons)
|
109 | }),
|
110 | new HtmlWebpackTrackerPlugin()
|
111 | ];
|
112 | if (manifest && ((_g = rules.manifest) !== null && _g !== void 0 ? _g : true)) {
|
113 | plugins.push(new html_webpack_plugin_1.default({
|
114 | id: 'manifest',
|
115 | filename: 'manifest-[contenthash].json',
|
116 | template: manifest,
|
117 | minify: true,
|
118 | inject: false
|
119 | }));
|
120 | }
|
121 | if (robots && ((_h = rules.robots) !== null && _h !== void 0 ? _h : true)) {
|
122 | plugins.push(new html_webpack_plugin_1.default({
|
123 | id: 'robots',
|
124 | filename: 'robots.txt',
|
125 | template: robots,
|
126 | minify: false,
|
127 | inject: false
|
128 | }));
|
129 | }
|
130 | if (useTypescript) {
|
131 | plugins.push(new fork_ts_checker_webpack_plugin_1.default({
|
132 | async: false,
|
133 | typescript: {
|
134 | enabled: true
|
135 | }
|
136 | }));
|
137 | }
|
138 | if (indexFile) {
|
139 | plugins.push(new html_webpack_plugin_1.default({
|
140 | template: indexFile,
|
141 | minify: { collapseWhitespace: true },
|
142 | inject: false
|
143 | }));
|
144 | }
|
145 | if (error404) {
|
146 | plugins.push(new html_webpack_plugin_1.default({
|
147 | template: error404,
|
148 | filename: '404.html',
|
149 | minify: { collapseWhitespace: true },
|
150 | inject: false
|
151 | }));
|
152 | }
|
153 | if (options.environment === 'production') {
|
154 | if ((_j = pluginsOptions.minify) !== null && _j !== void 0 ? _j : true) {
|
155 | plugins.push(new terser_webpack_plugin_1.default((_k = options.uglify) !== null && _k !== void 0 ? _k : {}));
|
156 | }
|
157 | }
|
158 | else if (hmr) {
|
159 | plugins.push(new webpack_1.HotModuleReplacementPlugin());
|
160 | }
|
161 | if (analyze) {
|
162 | if (path_1.basename(process.argv[1]) !== 'webpack') {
|
163 | plugins.push(new webpack_bundle_analyzer_1.BundleAnalyzerPlugin({
|
164 | analyzerMode: typeof analyze === 'string' ? analyze : 'server',
|
165 | analyzerHost: (_m = (_l = options.server) === null || _l === void 0 ? void 0 : _l.host) !== null && _m !== void 0 ? _m : 'home.cowtech.it',
|
166 | analyzerPort: ((_p = (_o = options.server) === null || _o === void 0 ? void 0 : _o.port) !== null && _p !== void 0 ? _p : 4200) + 2,
|
167 | generateStatsFile: analyze === 'static',
|
168 | openAnalyzer: false
|
169 | }));
|
170 | }
|
171 | else {
|
172 | plugins.push(new webpack_bundle_analyzer_1.BundleAnalyzerPlugin({
|
173 | analyzerMode: 'static',
|
174 | generateStatsFile: true,
|
175 | openAnalyzer: false
|
176 | }));
|
177 | }
|
178 | }
|
179 | if (swOptions.enabled === true || options.environment === 'production') {
|
180 | const swSrc = await resolveFile(options, 'serviceWorker.src', './(service-worker|sw).(js|ts)');
|
181 | if (swSrc) {
|
182 |
|
183 | const hashFactory = crypto_1.createHash('md4');
|
184 | hashFactory.update(JSON.stringify({ version: options.version }));
|
185 | const hash = hashFactory.digest('hex');
|
186 | const swDest = (_q = swOptions.dest) !== null && _q !== void 0 ? _q : 'sw.js';
|
187 | const envFile = swDest.replace(/\.js$/, `-env-${hash}.js`);
|
188 | exports.serviceWorkerDefaultExclude.push(envFile);
|
189 | plugins.push(new ServiceWorkerEnvironment({
|
190 | dest: envFile,
|
191 | version: options.version,
|
192 | debug: (_r = swOptions.debug) !== null && _r !== void 0 ? _r : options.environment !== 'production'
|
193 | }), new workbox_webpack_plugin_1.InjectManifest({
|
194 | swSrc,
|
195 | swDest,
|
196 | include: exports.serviceWorkerDefaultInclude,
|
197 | exclude: exports.serviceWorkerDefaultExclude,
|
198 | importScripts: [`/${envFile}`],
|
199 | ...((_s = swOptions.options) !== null && _s !== void 0 ? _s : {})
|
200 | }));
|
201 | }
|
202 | }
|
203 | if (pluginsOptions.additional) {
|
204 | plugins = plugins.concat(pluginsOptions.additional);
|
205 | }
|
206 | return environment_1.runHook(plugins, pluginsOptions.afterHook);
|
207 | }
|
208 | exports.setupPlugins = setupPlugins;
|