@plaudit/webpack-extensions 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ import type { PostCSSLoaderOptions } from "postcss-loader/dist/config";
2
+ export declare function postcssConfigBuilder(verbose: boolean, legacyPostcssPlugins: boolean, variables: (name: string) => string | undefined): PostCSSLoaderOptions;
@@ -0,0 +1,87 @@
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.postcssConfigBuilder = void 0;
7
+ const cssnano_1 = __importDefault(require("cssnano"));
8
+ // Options: calc
9
+ const calcOptions = { precision: 4, mediaQueries: true, selectors: true };
10
+ // Options: autoprefixerOptions
11
+ const autoprefixerOptions = { grid: "autoplace" };
12
+ // Nano Config
13
+ // http://cssnano.co/guides/optimisations/ For the value to use click on one and then
14
+ const nanoConfig = {
15
+ preset: [
16
+ 'cssnano-preset-default',
17
+ {
18
+ safe: true,
19
+ autoprefixer: false,
20
+ calc: false,
21
+ colormin: true,
22
+ convertValues: false,
23
+ discardComments: {
24
+ remove(comment) {
25
+ const commentKeepRegexp = /^!|@cms|@preserve|@cc_on|\bMIT\b|\bMPL\b|\bGPL\b|\bBSD\b|\bISCL\b|\(c\)|\bLicense\b|\bCopyright\b/i;
26
+ return !commentKeepRegexp.test(comment);
27
+ }
28
+ },
29
+ discardDuplicates: true,
30
+ discardEmpty: true,
31
+ discardOverridden: true,
32
+ discardUnused: false,
33
+ mergeIdents: false,
34
+ mergeLonghand: true,
35
+ mergeRules: false,
36
+ minifyFontValues: true,
37
+ minifyGradients: true,
38
+ minifyParams: true,
39
+ minifySelectors: true,
40
+ normalizeCharset: true,
41
+ normalizeDisplayValues: true,
42
+ normalizePositions: true,
43
+ normalizeRepeatStyle: true,
44
+ normalizeString: true,
45
+ normalizeTimingFunctions: true,
46
+ normalizeUnicode: true,
47
+ normalizeUrl: true,
48
+ normalizeWhitespace: true,
49
+ orderedValues: true,
50
+ reduceIdents: false,
51
+ reduceInitial: true,
52
+ reduceTransforms: true,
53
+ svgo: false,
54
+ uniqueSelectors: true,
55
+ zindex: false,
56
+ filterPlugins: false // I don't know where this one came from. It is not listed in the documentation.
57
+ }
58
+ ]
59
+ };
60
+ function postcssConfigBuilder(verbose, legacyPostcssPlugins, variables) {
61
+ return {
62
+ plugins: [
63
+ require("postcss-import")(),
64
+ require("@plaudit/postcss-silent-extend")({ warnOfUnused: verbose }),
65
+ require("postcss-mixins")(),
66
+ require("@plaudit/postcss-variables")({ variables }),
67
+ ...(legacyPostcssPlugins ? [
68
+ require("postcss-quantity-queries")(),
69
+ require("postcss-short-position")(),
70
+ require("postcss-short-size")(),
71
+ require("postcss-short-spacing")(),
72
+ require("postcss-fallback")() // Legacy
73
+ ] : []),
74
+ require("postcss-property-lookup")({ logLevel: "warn" }),
75
+ require("@plaudit/postcss-strip-units")(),
76
+ require("postcss-media-minmax")(),
77
+ require("postcss-inline-svg")(),
78
+ require("postcss-nested")(),
79
+ require("postcss-calc")(calcOptions),
80
+ require("@plaudit/postcss-color-function")(),
81
+ require("autoprefixer")(autoprefixerOptions),
82
+ require("postcss-reporter")({ clearReportedMessages: true }),
83
+ (0, cssnano_1.default)(nanoConfig)
84
+ ]
85
+ };
86
+ }
87
+ exports.postcssConfigBuilder = postcssConfigBuilder;
@@ -6,11 +6,11 @@ const node_fs_1 = __importDefault(require("node:fs"));
6
6
  const node_path_1 = __importDefault(require("node:path"));
7
7
  const AdditionalDependencyInjectorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/AdditionalDependencyInjectorPlugin"));
8
8
  const BlockJSONStyleRemappingPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/BlockJSONStyleRemappingPlugin"));
9
+ const static_configs_1 = require("./wordpress-scripts-wrapper/static-configs");
9
10
  const browser_sync_webpack_plugin_1 = __importDefault(require("browser-sync-webpack-plugin"));
10
11
  const copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
11
12
  const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
12
13
  const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-empty-scripts"));
13
- const cssnano_1 = __importDefault(require("cssnano"));
14
14
  function joinPossiblyAbsolutePaths(...paths) {
15
15
  return paths.reduce((res, p) => !res || node_path_1.default.isAbsolute(p) ? p : node_path_1.default.join(res, p), '') || '.';
16
16
  }
@@ -23,62 +23,49 @@ function addEntryPointWithMapper(entrypoints, entrypoint, dir, mapper = (entrypo
23
23
  }
24
24
  }
25
25
  }
26
+ function isTruthy(value) {
27
+ return !!value;
28
+ }
26
29
  module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
27
- const { legacyPostcssPlugins = false, variables = ["variables.js", "preprocess/variables.js"].map(p => node_path_1.default.join(process.cwd(), p)).filter(p => node_fs_1.default.existsSync(p)).map(p => require(p))[0] ?? {}, verbose = process.argv.includes('--verbose') } = config;
28
- // Nano Config
29
- // http://cssnano.co/guides/optimisations/ For the value to use click on one and then
30
- const nanoConfig = {
31
- preset: [
32
- 'cssnano-preset-default',
33
- {
34
- safe: true,
35
- autoprefixer: false,
36
- calc: false,
37
- colormin: true,
38
- convertValues: false,
39
- discardComments: {
40
- remove(comment) {
41
- const commentKeepRegexp = /^!|@cms|@preserve|@cc_on|\bMIT\b|\bMPL\b|\bGPL\b|\bBSD\b|\bISCL\b|\(c\)|\bLicense\b|\bCopyright\b/i;
42
- return !commentKeepRegexp.test(comment);
30
+ const seenPaths = new Map();
31
+ for (const bundleName of (Array.isArray(config.src) ? config.src : Object.values(config.src))) {
32
+ const key = bundleName.substring(0, bundleName.length - node_path_1.default.extname(bundleName).length);
33
+ let seen = seenPaths.get(key);
34
+ if (seen === undefined) {
35
+ seenPaths.set(key, seen = []);
36
+ }
37
+ seen.push(bundleName);
38
+ node_path_1.default.basename(bundleName, node_path_1.default.extname(bundleName));
39
+ }
40
+ let projectPrefix = undefined;
41
+ let duplicatedPaths = "";
42
+ for (const sameNamePaths of seenPaths.values()) {
43
+ if (sameNamePaths.length > 1) {
44
+ if (projectPrefix === undefined) {
45
+ for (let p = process.cwd(); p.length > 1; p = node_path_1.default.dirname(p)) {
46
+ if (node_fs_1.default.existsSync(node_path_1.default.join(p, "docker-compose.yml"))) {
47
+ projectPrefix = new RegExp(`^${p}`);
48
+ break;
43
49
  }
44
- },
45
- discardDuplicates: true,
46
- discardEmpty: true,
47
- discardOverridden: true,
48
- discardUnused: false,
49
- mergeIdents: false,
50
- mergeLonghand: true,
51
- mergeRules: false,
52
- minifyFontValues: true,
53
- minifyGradients: true,
54
- minifyParams: true,
55
- minifySelectors: true,
56
- normalizeCharset: true,
57
- normalizeDisplayValues: true,
58
- normalizePositions: true,
59
- normalizeRepeatStyle: true,
60
- normalizeString: true,
61
- normalizeTimingFunctions: true,
62
- normalizeUnicode: true,
63
- normalizeUrl: true,
64
- normalizeWhitespace: true,
65
- orderedValues: true,
66
- reduceIdents: false,
67
- reduceInitial: true,
68
- reduceTransforms: true,
69
- svgo: false,
70
- uniqueSelectors: true,
71
- zindex: false,
72
- filterPlugins: false // I don't know where this one came from. It is not listed in the documentation.
50
+ }
51
+ if (projectPrefix === undefined) {
52
+ projectPrefix = /^/;
53
+ }
73
54
  }
74
- ]
75
- };
55
+ duplicatedPaths += `\n* ${node_path_1.default.dirname(sameNamePaths[0]).replace(projectPrefix, '')}/[${sameNamePaths.map(p => node_path_1.default.basename(p)).join(', ')}]`;
56
+ }
57
+ }
58
+ if (duplicatedPaths.length !== 0) {
59
+ console.error(`Encountered multiple paths that produce the same effective bundle name:${duplicatedPaths}`);
60
+ process.exit(1);
61
+ }
62
+ const { legacyPostcssPlugins = false, variables = ["variables.js", "preprocess/variables.js"].map(p => node_path_1.default.join(process.cwd(), p)).filter(p => node_fs_1.default.existsSync(p)).map(p => require(p))[0] ?? {}, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true' } = config;
76
63
  const cssLoader = require.resolve('css-loader');
77
64
  if (cssLoader && webpackConfig.module?.rules) {
78
65
  for (const rule of webpackConfig.module.rules) {
79
- if (typeof rule === 'object' && "use" in rule && Array.isArray(rule.use)) {
66
+ if (isTruthy(rule) && typeof rule === 'object' && "use" in rule && Array.isArray(rule.use)) {
80
67
  for (const useElement of rule.use) {
81
- if (typeof useElement === 'object' && useElement.loader === cssLoader && typeof useElement.options === 'object') {
68
+ if (isTruthy(useElement) && typeof useElement === 'object' && useElement.loader === cssLoader && typeof useElement.options === 'object') {
82
69
  useElement.options['url'] = false;
83
70
  }
84
71
  }
@@ -87,47 +74,19 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
87
74
  }
88
75
  let currentEntrypoint = '';
89
76
  if (webpackConfig.module?.rules) {
90
- // Options: calc
91
- const calcOptions = { precision: 4, mediaQueries: true, selectors: true };
92
- // Options: autoprefixerOptions
93
- const autoprefixerOptions = { grid: "autoplace" };
94
- const postcssConfig = {
95
- plugins: [
96
- require("postcss-import")(),
97
- require("@plaudit/postcss-silent-extend")({ warnOfUnused: verbose }),
98
- require("postcss-mixins")(),
99
- require("@plaudit/postcss-variables")({ variables: (name, srcNode) => {
100
- if (variables[name]) {
101
- return variables[name];
102
- }
103
- if (name === 'ENV') { // This is purely a backwards-compatibility issue.
104
- return currentEntrypoint.endsWith('public.pcss') ? 'PUBLIC' : (currentEntrypoint.endsWith('block-editor.pcss') ? 'EDITOR' : '');
105
- }
106
- return undefined;
107
- } }),
108
- ...(legacyPostcssPlugins ? [
109
- require("postcss-quantity-queries")(),
110
- require("postcss-short-position")(),
111
- require("postcss-short-size")(),
112
- require("postcss-short-spacing")(),
113
- require("postcss-fallback")() // Legacy
114
- ] : []),
115
- require("postcss-property-lookup")({ logLevel: "warn" }),
116
- require("@plaudit/postcss-strip-units")(),
117
- require("postcss-media-minmax")(),
118
- require("postcss-inline-svg")(),
119
- require("postcss-nested")(),
120
- require("postcss-calc")(calcOptions),
121
- require("@plaudit/postcss-color-function")(),
122
- require("autoprefixer")(autoprefixerOptions),
123
- require("postcss-reporter")({ clearReportedMessages: true }),
124
- (0, cssnano_1.default)(nanoConfig)
125
- ]
126
- };
77
+ const postcssConfig = (0, static_configs_1.postcssConfigBuilder)(verbose, legacyPostcssPlugins, (name) => {
78
+ if (variables[name]) {
79
+ return variables[name];
80
+ }
81
+ if (name === 'ENV') { // This is purely a backwards-compatibility issue.
82
+ return currentEntrypoint.endsWith('public.pcss') ? 'PUBLIC' : (currentEntrypoint.endsWith('block-editor.pcss') ? 'EDITOR' : '');
83
+ }
84
+ return undefined;
85
+ });
127
86
  for (const rule of webpackConfig.module.rules) {
128
- if (typeof rule === 'object' && Array.isArray(rule.use)) {
87
+ if (isTruthy(rule) && typeof rule === 'object' && Array.isArray(rule.use)) {
129
88
  for (const useItem of rule.use) {
130
- if (typeof useItem === 'object' && typeof useItem.options === 'object') {
89
+ if (isTruthy(useItem) && typeof useItem === 'object' && typeof useItem.options === 'object') {
131
90
  if (useItem.options["sourceMap"] === false) {
132
91
  useItem.options["sourceMap"] = true;
133
92
  }
@@ -152,7 +111,7 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
152
111
  const srcRoot = srcRoots.length === 1 ? joinPossiblyAbsolutePaths(process.cwd(), src) : srcRoots.map(s => joinPossiblyAbsolutePaths(process.cwd(), s));
153
112
  const srcIsDirectory = !Array.isArray(srcRoot) && node_fs_1.default.lstatSync(srcRoot).isDirectory();
154
113
  const copyFiles = srcIsDirectory && src !== dest;
155
- const plugins = webpackConfig.plugins ? [...webpackConfig.plugins] : [];
114
+ const plugins = webpackConfig.plugins?.filter(isTruthy) ?? [];
156
115
  plugins.push(new fork_ts_checker_webpack_plugin_1.default({
157
116
  typescript: {
158
117
  diagnosticOptions: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",
@@ -14,12 +14,12 @@
14
14
  },
15
15
  "devDependencies": {
16
16
  "@types/browser-sync-webpack-plugin": "^2.2.2",
17
- "@types/node": "^20.2.5",
17
+ "@types/node": "^20.3.1",
18
18
  "@types/tapable": "^2.2.3",
19
19
  "@types/webpack": "^5.28.1",
20
20
  "@types/webpack-sources": "^3.2.0",
21
21
  "postcss-load-config": "^4.0.1",
22
- "postcss-loader": "^7.3.2",
22
+ "postcss-loader": "^7.3.3",
23
23
  "ts-node": "^10.9.1",
24
24
  "typescript": "^5.1.3"
25
25
  },
@@ -56,7 +56,7 @@
56
56
  "postcss-simple-vars": "^7.0.1",
57
57
  "react": "^18.2.0",
58
58
  "react-dom": "^18.2.0",
59
- "webpack": "^5.86.0",
59
+ "webpack": "^5.87.0",
60
60
  "webpack-remove-empty-scripts": "^1.0.3",
61
61
  "webpack-sources": "^3.2.3"
62
62
  },