@plaudit/webpack-extensions 2.9.2 → 2.10.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,5 @@
1
+ import { type Compiler, type WebpackPluginInstance } from "webpack";
2
+ export default class ExtensionsConfigFileGeneratorPlugin implements WebpackPluginInstance {
3
+ constructor();
4
+ apply(compiler: Compiler): void;
5
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const webpack_1 = require("webpack");
4
+ class ExtensionsConfigFileGeneratorPlugin {
5
+ constructor() { }
6
+ apply(compiler) {
7
+ compiler.hooks.thisCompilation.tap("ExtensionsConfigFileGeneratorPlugin", compilation => {
8
+ compilation.hooks.processAssets.tap({
9
+ name: "ExtensionsConfigFileGeneratorPlugin_ProcessAssets",
10
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ANALYSE
11
+ }, compilationAssets => {
12
+ const stats = compilation.getStats().toJson({
13
+ hash: true,
14
+ publicPath: true,
15
+ assets: true,
16
+ chunks: true,
17
+ modules: true,
18
+ source: true,
19
+ errorDetails: false,
20
+ timings: false
21
+ });
22
+ if (!stats.assets) {
23
+ throw new Error("Stats did not include assets despite them being requested");
24
+ }
25
+ if (!stats.modules) {
26
+ throw new Error("Stats did not include modules despite them being requested");
27
+ }
28
+ const mapping = {};
29
+ for (const asset of stats.assets) {
30
+ if (asset.chunks?.length === 1 && asset.size && !asset.name.endsWith('.asset.php')) {
31
+ const match = /^(.+?)-(editor-style|style|view-script|editor-script|script)\.(?:css|m?js)$/i.exec(asset.name);
32
+ if (match) {
33
+ if (!mapping[match[1]]) {
34
+ mapping[match[1]] = {};
35
+ }
36
+ let key;
37
+ if (match[2] === "editor-script") {
38
+ key = "editorScript";
39
+ }
40
+ else if (match[2] === "view-script") {
41
+ key = "viewScript";
42
+ }
43
+ else if (match[2] === "editor-style") {
44
+ key = "editorStyle";
45
+ }
46
+ else {
47
+ key = match[2];
48
+ }
49
+ if (!mapping[match[1]][key]) {
50
+ mapping[match[1]][key] = [];
51
+ }
52
+ mapping[match[1]][key].push([`plaudit_block-extension_${match[1]}-${match[2]}`, asset.name]);
53
+ }
54
+ }
55
+ }
56
+ let contents = `a:${Object.keys(mapping).length}:{`;
57
+ for (const [blockSlug, blockMappings] of Object.entries(mapping)) {
58
+ contents += `s:${blockSlug.length}:"${blockSlug}";`;
59
+ contents += `a:${Object.entries(blockMappings).length}:{`;
60
+ for (const [mapping, handlerFiles] of Object.entries(blockMappings)) {
61
+ contents += `s:${mapping.length}:"${mapping}";`;
62
+ contents += `a:${handlerFiles.length}:{`;
63
+ for (let i = 0; i < handlerFiles.length; i++) {
64
+ const handlerFile = handlerFiles[i];
65
+ contents += `i:${i};a:2:{i:0;s:${handlerFile[0].length}:"${handlerFile[0]}";i:1;s:${handlerFile[1].length}:"${handlerFile[1]}";}`;
66
+ }
67
+ contents += "}";
68
+ }
69
+ contents += "}";
70
+ }
71
+ contents += "}";
72
+ compilation.emitAsset("mapping.config", new webpack_1.sources.RawSource(contents.trim()));
73
+ });
74
+ });
75
+ }
76
+ }
77
+ exports.default = ExtensionsConfigFileGeneratorPlugin;
@@ -3,6 +3,7 @@ interface AdvancedOutputConfig {
3
3
  destination: string;
4
4
  withLegacyBlocksIn?: string | undefined;
5
5
  additionalDependencies?: string[];
6
+ directoryLayout?: 'blocks' | 'extensions';
6
7
  }
7
8
  interface PlauditWordpressWebpackConfig {
8
9
  standaloneBlocks?: boolean;
@@ -6,12 +6,13 @@ 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 ExtensionsConfigFileGeneratorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/ExtensionsConfigFileGeneratorPlugin"));
10
+ const VariablesJSMonitorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/VariablesJSMonitorPlugin"));
9
11
  const static_configs_1 = require("./wordpress-scripts-wrapper/static-configs");
10
12
  const browser_sync_webpack_plugin_1 = __importDefault(require("browser-sync-webpack-plugin"));
11
13
  const copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
12
14
  const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
13
15
  const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-empty-scripts"));
14
- const VariablesJSMonitorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/VariablesJSMonitorPlugin"));
15
16
  function joinPossiblyAbsolutePaths(...paths) {
16
17
  return paths.reduce((res, p) => !res || node_path_1.default.isAbsolute(p) ? p : node_path_1.default.join(res, p), '') || '.';
17
18
  }
@@ -229,10 +230,13 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
229
230
  if (variablesFilePath) {
230
231
  plugins.push(new VariablesJSMonitorPlugin_1.default(variablesFilePath));
231
232
  }
232
- if (copyFiles) {
233
+ if (copyFiles && (typeof dest === 'string' || dest.directoryLayout !== 'extensions')) {
233
234
  plugins.push(new BlockJSONStyleRemappingPlugin_1.default(standaloneBlocks));
234
235
  }
235
236
  plugins.push(new AdditionalDependencyInjectorPlugin_1.default(typeof dest !== 'string' && dest.additionalDependencies ? dest.additionalDependencies : []));
237
+ if (srcIsDirectory && (typeof dest !== 'string' && dest.directoryLayout === 'extensions')) {
238
+ plugins.push(new ExtensionsConfigFileGeneratorPlugin_1.default());
239
+ }
236
240
  if (first) {
237
241
  first = false;
238
242
  if (process.argv.includes('--browser-sync') || process.env['BROWSER_SYNC'] === 'true') {
@@ -246,53 +250,64 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
246
250
  }
247
251
  let entry;
248
252
  if (srcIsDirectory) {
249
- entry = () => {
250
- const rawEntrypoints = node_fs_1.default.readdirSync(srcRoot, 'utf8')
251
- .map(dir => joinPossiblyAbsolutePaths(srcRoot, dir))
252
- .filter(dir => node_fs_1.default.statSync(dir).isDirectory())
253
- .flatMap(dir => {
254
- const res = [];
255
- try {
256
- const blockJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'block.json'), 'utf8'));
257
- for (const key of ["editorStyle", "style", "editorScript", "viewScript", "script"]) {
258
- if (key in blockJSON) {
259
- res.push(...mapToRealEntrypoints(blockJSON[key], dir, ep => ep.startsWith("file:") ? ep.substring(5) : ep));
260
- }
261
- }
262
- return res;
263
- }
264
- catch (e) {
253
+ if (typeof dest !== 'string' && dest.directoryLayout === 'extensions') {
254
+ entry = () => {
255
+ const rawEntrypoints = node_fs_1.default.readdirSync(srcRoot, 'utf8')
256
+ .map(file => joinPossiblyAbsolutePaths(srcRoot, file))
257
+ .filter(file => (scriptExtension.test(file) || styleExtension.test(file)) && node_fs_1.default.statSync(file).isFile())
258
+ .map((file) => [node_path_1.default.basename(file, node_path_1.default.extname(file)), file]);
259
+ return Object.fromEntries(rawEntrypoints);
260
+ };
261
+ }
262
+ else {
263
+ entry = () => {
264
+ const rawEntrypoints = node_fs_1.default.readdirSync(srcRoot, 'utf8')
265
+ .map(dir => joinPossiblyAbsolutePaths(srcRoot, dir))
266
+ .filter(dir => node_fs_1.default.statSync(dir).isDirectory())
267
+ .flatMap(dir => {
268
+ const res = [];
265
269
  try {
266
- const packageJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'package.json'), 'utf8'));
267
- res.push(...mapToRealEntrypoints(packageJSON['main'], dir));
268
- res.push(...mapToRealEntrypoints(packageJSON['style'], dir));
270
+ const blockJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'block.json'), 'utf8'));
271
+ for (const key of ["editorStyle", "style", "editorScript", "viewScript", "script"]) {
272
+ if (key in blockJSON) {
273
+ res.push(...mapToRealEntrypoints(blockJSON[key], dir, ep => ep.startsWith("file:") ? ep.substring(5) : ep));
274
+ }
275
+ }
276
+ return res;
269
277
  }
270
278
  catch (e) {
271
279
  try {
272
- res.push(...parseEntrypointsJSON(dir));
280
+ const packageJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'package.json'), 'utf8'));
281
+ res.push(...mapToRealEntrypoints(packageJSON['main'], dir));
282
+ res.push(...mapToRealEntrypoints(packageJSON['style'], dir));
273
283
  }
274
284
  catch (e) {
275
- // This just means that the directory doesn't contain any declared entrypoints.
285
+ try {
286
+ res.push(...parseEntrypointsJSON(dir));
287
+ }
288
+ catch (e) {
289
+ // This just means that the directory doesn't contain any declared entrypoints.
290
+ }
276
291
  }
277
292
  }
278
- }
279
- return res;
280
- });
281
- const perAssetPathGroupedEntrypoints = groupEntrypointsByAssetFile(rawEntrypoints, e => e[0]);
282
- const currentEntry = {};
283
- for (const groupedEntrypoints of perAssetPathGroupedEntrypoints.values()) {
284
- if (groupedEntrypoints.length === 1) {
285
- currentEntry[groupedEntrypoints[0][0]] = groupedEntrypoints[0][1];
286
- }
287
- else {
288
- const typeCounts = {};
289
- for (const entrypoint of groupedEntrypoints) {
290
- addPotentiallyDuplicatedEntrypointName(currentEntry, entrypoint, typeCounts);
293
+ return res;
294
+ });
295
+ const perAssetPathGroupedEntrypoints = groupEntrypointsByAssetFile(rawEntrypoints, e => e[0]);
296
+ const currentEntry = {};
297
+ for (const groupedEntrypoints of perAssetPathGroupedEntrypoints.values()) {
298
+ if (groupedEntrypoints.length === 1) {
299
+ currentEntry[groupedEntrypoints[0][0]] = groupedEntrypoints[0][1];
300
+ }
301
+ else {
302
+ const typeCounts = {};
303
+ for (const entrypoint of groupedEntrypoints) {
304
+ addPotentiallyDuplicatedEntrypointName(currentEntry, entrypoint, typeCounts);
305
+ }
291
306
  }
292
307
  }
293
- }
294
- return currentEntry;
295
- };
308
+ return currentEntry;
309
+ };
310
+ }
296
311
  }
297
312
  else {
298
313
  const baseDest = node_path_1.default.basename(destPath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.9.2",
3
+ "version": "2.10.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",