@plaudit/webpack-extensions 2.2.1 → 2.4.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.
@@ -1,4 +1,6 @@
1
1
  import { type Compiler, type WebpackPluginInstance } from "webpack";
2
2
  export default class AdditionalDependencyInjectorPlugin implements WebpackPluginInstance {
3
+ private readonly entrypointAdditionalDependencies;
4
+ constructor(entrypointAdditionalDependencies: string[]);
3
5
  apply(compiler: Compiler): void;
4
6
  }
@@ -5,8 +5,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const node_fs_1 = __importDefault(require("node:fs"));
7
7
  const webpack_1 = require("webpack");
8
- const webpack_sources_1 = require("webpack-sources");
9
8
  class AdditionalDependencyInjectorPlugin {
9
+ entrypointAdditionalDependencies;
10
+ constructor(entrypointAdditionalDependencies) {
11
+ this.entrypointAdditionalDependencies = entrypointAdditionalDependencies;
12
+ }
10
13
  apply(compiler) {
11
14
  compiler.hooks.thisCompilation.tap("AdditionalDependencyInjectorPlugin", compilation => {
12
15
  compilation.hooks.processAssets.tap({
@@ -32,13 +35,16 @@ class AdditionalDependencyInjectorPlugin {
32
35
  if (!assetSource) {
33
36
  continue;
34
37
  }
38
+ const additionalDependencies = [...this.entrypointAdditionalDependencies];
35
39
  const firstLine = node_fs_1.default.readFileSync(assetSource, 'utf8').trim().split(/\r?\n/)[0];
36
40
  if (firstLine.startsWith("//ADDITIONAL_DEPENDENCIES:")) {
37
- const additionalDependencies = firstLine.substring(26).trim().split(',').map(dep => dep.trim());
41
+ additionalDependencies.push(...firstLine.substring(26).trim().split(',').map(dep => dep.trim()));
42
+ }
43
+ if (additionalDependencies.length > 0) {
38
44
  const seen = new Set();
39
45
  const additionalDeps = additionalDependencies.filter(dep => !seen.has(dep) && seen.add(dep)).sort().map(v => `'${v}'`).join(', ');
40
- if (additionalDeps) {
41
- compilation.updateAsset(name, new webpack_sources_1.RawSource(asset.source().replace(/('dependencies'\s+=>\s+(?:array\(|\[))(.*)([)\]], 'version')/g, (match, g1, g2, g3) => `${g1}${g2.length > 0 ? `${g2}, ${additionalDeps}` : additionalDeps}${g3}`)));
46
+ if (additionalDeps.length > 0) {
47
+ compilation.updateAsset(name, new webpack_1.sources.RawSource(asset.source().toString().replace(/('dependencies'\s+=>\s+(?:array\(|\[))(.*)([)\]], 'version')/g, (match, g1, g2, g3) => `${g1}${g2.length > 0 ? `${g2}, ${additionalDeps}` : additionalDeps}${g3}`)));
42
48
  }
43
49
  }
44
50
  }
@@ -1,5 +1,7 @@
1
1
  import type { Compiler, WebpackPluginInstance } from "webpack";
2
2
  export default class BlockJSONStyleRemappingPlugin implements WebpackPluginInstance {
3
+ private readonly standaloneBlocks;
4
+ constructor(standaloneBlocks: boolean);
3
5
  apply(compiler: Compiler): void;
4
6
  findCommonAncestor(...paths: string[]): string[];
5
7
  findRelativeRouteBetween(path1: string, path2: string): string;
@@ -7,6 +7,10 @@ const node_fs_1 = __importDefault(require("node:fs"));
7
7
  const node_path_1 = __importDefault(require("node:path"));
8
8
  const webpack_sources_1 = require("webpack-sources");
9
9
  class BlockJSONStyleRemappingPlugin {
10
+ standaloneBlocks;
11
+ constructor(standaloneBlocks) {
12
+ this.standaloneBlocks = standaloneBlocks;
13
+ }
10
14
  apply(compiler) {
11
15
  compiler.hooks.compilation.tap("BlockJSONStyleRemappingPlugin", compilation => {
12
16
  compilation.hooks.afterProcessAssets.tap("BlockJSONStyleRemappingPlugin_ProcessAssets", compilationAssets => {
@@ -70,7 +74,7 @@ class BlockJSONStyleRemappingPlugin {
70
74
  }
71
75
  }
72
76
  }
73
- if (json["plaudit"] !== "simple") {
77
+ if (!this.standaloneBlocks && json["plaudit"] !== "simple") {
74
78
  const sourceDir = node_path_1.default.join(compiler.context, dirname);
75
79
  const outputDir = node_path_1.default.join(compiler.outputPath, node_path_1.default.dirname(name));
76
80
  const stripFilePrefix = (file) => file.startsWith("file:./") ? file.substring(7) : file;
@@ -1,8 +1,14 @@
1
1
  import type { Configuration } from "webpack";
2
+ interface AdvancedOutputConfig {
3
+ destination: string;
4
+ withLegacyBlocksIn?: string | undefined;
5
+ additionalDependencies?: string[];
6
+ }
2
7
  interface PlauditWordpressWebpackConfig {
8
+ standaloneBlocks?: boolean;
3
9
  variables?: Record<string, any>;
4
10
  verbose?: boolean;
5
- src: string[] | Record<string, string>;
11
+ src: string[] | Record<string, string | AdvancedOutputConfig>;
6
12
  }
7
13
  declare const _default: (config: PlauditWordpressWebpackConfig, webpackConfig?: Configuration) => Configuration[];
8
14
  export = _default;
@@ -39,6 +39,20 @@ function groupEntrypointsByAssetFile(entrypoints, entrypointNameExtractor) {
39
39
  }
40
40
  return seenPaths;
41
41
  }
42
+ function resolveLegacyBlockScriptsInFolder(folder) {
43
+ const blockScriptEntrypoints = [];
44
+ for (const blockDir of node_fs_1.default.readdirSync(folder)) {
45
+ const fullBlockDir = node_path_1.default.join(folder, blockDir);
46
+ const packageJSON = node_path_1.default.join(fullBlockDir, 'package.json');
47
+ if (node_fs_1.default.existsSync(packageJSON)) {
48
+ const main = JSON.parse(node_fs_1.default.readFileSync(packageJSON, 'utf-8'))['main'];
49
+ if (main && node_fs_1.default.existsSync(node_path_1.default.join(fullBlockDir, main))) {
50
+ blockScriptEntrypoints.push(node_path_1.default.join(fullBlockDir, main));
51
+ }
52
+ }
53
+ }
54
+ return blockScriptEntrypoints;
55
+ }
42
56
  const scriptExtension = /\.[jt]sx?$/;
43
57
  const styleExtension = /\.(p?c|sa)ss$/;
44
58
  function scriptOrStyleTest(entryPath) {
@@ -83,7 +97,7 @@ function addPotentiallyDuplicatedEntrypointName(entry, entrypoint, typeCounts) {
83
97
  entry[potentialKey] = entrypoint[1];
84
98
  }
85
99
  module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
86
- const seenPaths = groupEntrypointsByAssetFile(Array.isArray(config.src) ? config.src : Object.values(config.src), bn => bn);
100
+ const seenPaths = groupEntrypointsByAssetFile(Array.isArray(config.src) ? config.src : Object.values(config.src).map(bn => typeof bn === 'string' ? bn : bn.destination), bn => bn);
87
101
  let projectPrefix = undefined;
88
102
  let duplicatedPaths = "";
89
103
  for (const sameNamePaths of seenPaths.values()) {
@@ -106,7 +120,7 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
106
120
  console.error(`Encountered multiple paths that produce the same effective bundle name:${duplicatedPaths}`);
107
121
  process.exit(1);
108
122
  }
109
- const { variables = ["variables.js", "src/site/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;
123
+ const { standaloneBlocks = false, variables = ["variables.js", "src/site/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;
110
124
  const cssLoader = require.resolve('css-loader');
111
125
  if (cssLoader && webpackConfig.module?.rules) {
112
126
  for (const rule of webpackConfig.module.rules) {
@@ -154,7 +168,10 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
154
168
  const sources = Array.isArray(config.src) ? config.src.map(s => [s, s]) : Object.entries(config.src);
155
169
  return sources.map(([src, dest]) => {
156
170
  currentEntrypoint = src;
157
- const srcRoots = src.split(',');
171
+ const srcRoots = typeof dest !== 'string' && dest.withLegacyBlocksIn
172
+ ? [...src.split(','), ...resolveLegacyBlockScriptsInFolder(dest.withLegacyBlocksIn)]
173
+ : src.split(',');
174
+ const destPath = typeof dest === 'string' ? dest : dest.destination;
158
175
  const srcRoot = srcRoots.length === 1 ? joinPossiblyAbsolutePaths(process.cwd(), src) : srcRoots.map(s => joinPossiblyAbsolutePaths(process.cwd(), s));
159
176
  const srcIsDirectory = !Array.isArray(srcRoot) && node_fs_1.default.lstatSync(srcRoot).isDirectory();
160
177
  const copyFiles = srcIsDirectory && src !== dest;
@@ -169,9 +186,9 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
169
186
  }
170
187
  }), new webpack_remove_empty_scripts_1.default({ stage: webpack_remove_empty_scripts_1.default.STAGE_AFTER_PROCESS_PLUGINS, extensions: ['css', 'scss', 'sass', 'less', 'styl', 'pcss'] }));
171
188
  if (copyFiles) {
172
- plugins.push(new BlockJSONStyleRemappingPlugin_1.default());
189
+ plugins.push(new BlockJSONStyleRemappingPlugin_1.default(standaloneBlocks));
173
190
  }
174
- plugins.push(new AdditionalDependencyInjectorPlugin_1.default());
191
+ plugins.push(new AdditionalDependencyInjectorPlugin_1.default(typeof dest !== 'string' && dest.additionalDependencies ? dest.additionalDependencies : []));
175
192
  if (first) {
176
193
  first = false;
177
194
  if (process.argv.includes('--browser-sync') || process.env['BROWSER_SYNC'] === 'true') {
@@ -253,7 +270,7 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
253
270
  }
254
271
  }
255
272
  else {
256
- const baseDest = node_path_1.default.basename(dest);
273
+ const baseDest = node_path_1.default.basename(destPath);
257
274
  entry = {
258
275
  [baseDest.includes('.') ? node_path_1.default.basename(baseDest, node_path_1.default.extname(baseDest)) : baseDest]: srcRoot
259
276
  };
@@ -261,12 +278,12 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
261
278
  let output;
262
279
  if (srcIsDirectory) {
263
280
  output = {
264
- path: joinPossiblyAbsolutePaths(process.cwd(), dest)
281
+ path: joinPossiblyAbsolutePaths(process.cwd(), destPath)
265
282
  };
266
283
  }
267
284
  else {
268
285
  output = {
269
- path: joinPossiblyAbsolutePaths(process.cwd(), node_path_1.default.dirname(dest))
286
+ path: joinPossiblyAbsolutePaths(process.cwd(), node_path_1.default.dirname(destPath))
270
287
  };
271
288
  }
272
289
  return {
@@ -279,7 +296,8 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
279
296
  },
280
297
  plugins: copyFiles
281
298
  ? plugins.map(plugin => plugin.constructor.name === 'CopyPlugin'
282
- ? new copy_webpack_plugin_1.default({ patterns: [{ from: '**/(block.json|*.(asset\.php|svg))', context: srcRoot, noErrorOnMissing: true }] })
299
+ ? new copy_webpack_plugin_1.default({ patterns: [{ from: standaloneBlocks ? '**/(block.json|*.(php|twig|svg))' : '**/(block.json|*.(asset\.php|svg))',
300
+ context: srcRoot, noErrorOnMissing: true }] })
283
301
  : plugin)
284
302
  : (srcIsDirectory
285
303
  ? plugins.filter(plugin => plugin.constructor.name !== 'CopyPlugin' && plugin.constructor.name !== 'CleanWebpackPlugin')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.2.1",
3
+ "version": "2.4.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",
@@ -14,14 +14,14 @@
14
14
  },
15
15
  "devDependencies": {
16
16
  "@types/browser-sync-webpack-plugin": "^2.2.2",
17
- "@types/node": "^20.3.1",
17
+ "@types/node": "^20.3.2",
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
22
  "postcss-loader": "^7.3.3",
23
23
  "ts-node": "^10.9.1",
24
- "typescript": "^5.1.3"
24
+ "typescript": "^5.1.6"
25
25
  },
26
26
  "dependencies": {
27
27
  "@plaudit/postcss-color-function": "^5.0.0",
@@ -35,7 +35,7 @@
35
35
  "clean-webpack-plugin": "^4.0.0",
36
36
  "copy-webpack-plugin": "^11.0.0",
37
37
  "cssnano": "^6.0.1",
38
- "eslint": "^8.42.0",
38
+ "eslint": "^8.43.0",
39
39
  "eslint-plugin-jsdoc": "^43.1.1",
40
40
  "fork-ts-checker-webpack-plugin": "^8.0.0",
41
41
  "postcss": "^8.4.24",
@@ -56,9 +56,8 @@
56
56
  "postcss-simple-vars": "^7.0.1",
57
57
  "react": "^18.2.0",
58
58
  "react-dom": "^18.2.0",
59
- "webpack": "^5.88.0",
60
- "webpack-remove-empty-scripts": "^1.0.3",
61
- "webpack-sources": "^3.2.3"
59
+ "webpack": "^5.88.1",
60
+ "webpack-remove-empty-scripts": "^1.0.3"
62
61
  },
63
62
  "resolutions": {
64
63
  "eslint-plugin-jsdoc": "^43.1.1"