1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.onCreateWebpackConfig = void 0;
|
7 | const utils_1 = require("./utils");
|
8 | const paths_1 = __importDefault(require("./paths"));
|
9 | const shared_1 = require("./shared");
|
10 | const debug_1 = require("./debug");
|
11 | const merge_anything_1 = require("merge-anything");
|
12 | const loadersRegex = /postcss-loader/;
|
13 |
|
14 | const onCreateWebpackConfig = function ({ actions, stage, getConfig }, pluginOptions) {
|
15 | var _a, _b, _c, _d, _e;
|
16 | const { plugins, ...options } = pluginOptions;
|
17 | const userOptions = options;
|
18 | const defaultOptions = {
|
19 | printSummary: true,
|
20 | printRejected: false,
|
21 | printAll: false,
|
22 | debug: false,
|
23 | develop: false,
|
24 | tailwind: false,
|
25 | ignore: [],
|
26 | purgeOnly: [],
|
27 | purgeCSSOptions: {
|
28 | rejected: (_a = userOptions.printRejected) !== null && _a !== void 0 ? _a : false,
|
29 | content: ((_b = userOptions.purgeCSSOptions) === null || _b === void 0 ? void 0 : _b.content) ? [] : [paths_1.default.src],
|
30 | css: [],
|
31 | safelist: { standard: ['html', 'body'] },
|
32 | },
|
33 | };
|
34 | if (userOptions.tailwind) {
|
35 | defaultOptions.purgeCSSOptions.defaultExtractor = (content) => { var _a; return (_a = content.match(/[^\s"'<>`]*[^\s"':<>`]/g)) !== null && _a !== void 0 ? _a : []; };
|
36 | }
|
37 |
|
38 | if (((_c = userOptions.purgeCSSOptions) === null || _c === void 0 ? void 0 : _c.safelist) &&
|
39 | Array.isArray(userOptions.purgeCSSOptions.safelist)) {
|
40 | userOptions.purgeCSSOptions.safelist = {
|
41 | standard: [...userOptions.purgeCSSOptions.safelist],
|
42 | };
|
43 | }
|
44 | const mergedUserOptions = (0, merge_anything_1.mergeAndConcat)(defaultOptions, userOptions);
|
45 | if (mergedUserOptions.printSummary && stage === 'build-html') {
|
46 | shared_1.stats.printStats();
|
47 | }
|
48 | if (stage === 'develop-html' || stage === 'build-html') {
|
49 | return;
|
50 | }
|
51 | if (stage === 'develop' && !mergedUserOptions.develop) {
|
52 | return;
|
53 | }
|
54 | const config = getConfig();
|
55 | const existingRules = (_d = config.module) === null || _d === void 0 ? void 0 : _d.rules;
|
56 | const purgecssloader = {
|
57 | loader: paths_1.default.loader,
|
58 | options: mergedUserOptions,
|
59 | };
|
60 | for (const rules of existingRules) {
|
61 | for (const rule of (_e = rules.oneOf) !== null && _e !== void 0 ? _e : []) {
|
62 | if (Array.isArray(rule.use)) {
|
63 | const index = (0, utils_1.findLoader)(rule.use, loadersRegex);
|
64 | (0, utils_1.insertLoader)(rule.use, index, purgecssloader);
|
65 | }
|
66 | }
|
67 | }
|
68 | actions.replaceWebpackConfig(config);
|
69 | if (mergedUserOptions.debug) {
|
70 | (0, debug_1.writeConfig)(getConfig());
|
71 | }
|
72 | };
|
73 | exports.onCreateWebpackConfig = onCreateWebpackConfig;
|