@plaudit/webpack-extensions 2.9.1 → 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
  }
@@ -211,25 +212,31 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
211
212
  const srcIsDirectory = !Array.isArray(srcRoot) && node_fs_1.default.lstatSync(srcRoot).isDirectory();
212
213
  const copyFiles = srcIsDirectory && src !== dest;
213
214
  const plugins = webpackConfig.plugins?.filter(isTruthy) ?? [];
214
- plugins.push(new fork_ts_checker_webpack_plugin_1.default({
215
- async: true,
216
- typescript: {
217
- diagnosticOptions: {
218
- semantic: true,
219
- syntactic: true,
215
+ if (process.env["NO_TS_CHECKER"] !== "true") {
216
+ plugins.push(new fork_ts_checker_webpack_plugin_1.default({
217
+ async: true,
218
+ typescript: {
219
+ diagnosticOptions: {
220
+ semantic: true,
221
+ syntactic: true,
222
+ }
220
223
  }
221
- }
222
- }), new webpack_remove_empty_scripts_1.default({
224
+ }));
225
+ }
226
+ plugins.push(new webpack_remove_empty_scripts_1.default({
223
227
  stage: webpack_remove_empty_scripts_1.default.STAGE_AFTER_PROCESS_PLUGINS,
224
228
  extensions: ['css', 'scss', 'sass', 'less', 'styl', 'pcss']
225
229
  }));
226
230
  if (variablesFilePath) {
227
231
  plugins.push(new VariablesJSMonitorPlugin_1.default(variablesFilePath));
228
232
  }
229
- if (copyFiles) {
233
+ if (copyFiles && (typeof dest === 'string' || dest.directoryLayout !== 'extensions')) {
230
234
  plugins.push(new BlockJSONStyleRemappingPlugin_1.default(standaloneBlocks));
231
235
  }
232
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
+ }
233
240
  if (first) {
234
241
  first = false;
235
242
  if (process.argv.includes('--browser-sync') || process.env['BROWSER_SYNC'] === 'true') {
@@ -243,53 +250,64 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
243
250
  }
244
251
  let entry;
245
252
  if (srcIsDirectory) {
246
- entry = () => {
247
- const rawEntrypoints = node_fs_1.default.readdirSync(srcRoot, 'utf8')
248
- .map(dir => joinPossiblyAbsolutePaths(srcRoot, dir))
249
- .filter(dir => node_fs_1.default.statSync(dir).isDirectory())
250
- .flatMap(dir => {
251
- const res = [];
252
- try {
253
- const blockJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'block.json'), 'utf8'));
254
- for (const key of ["editorStyle", "style", "editorScript", "viewScript", "script"]) {
255
- if (key in blockJSON) {
256
- res.push(...mapToRealEntrypoints(blockJSON[key], dir, ep => ep.startsWith("file:") ? ep.substring(5) : ep));
257
- }
258
- }
259
- return res;
260
- }
261
- 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 = [];
262
269
  try {
263
- const packageJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'package.json'), 'utf8'));
264
- res.push(...mapToRealEntrypoints(packageJSON['main'], dir));
265
- 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;
266
277
  }
267
278
  catch (e) {
268
279
  try {
269
- 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));
270
283
  }
271
284
  catch (e) {
272
- // 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
+ }
273
291
  }
274
292
  }
275
- }
276
- return res;
277
- });
278
- const perAssetPathGroupedEntrypoints = groupEntrypointsByAssetFile(rawEntrypoints, e => e[0]);
279
- const currentEntry = {};
280
- for (const groupedEntrypoints of perAssetPathGroupedEntrypoints.values()) {
281
- if (groupedEntrypoints.length === 1) {
282
- currentEntry[groupedEntrypoints[0][0]] = groupedEntrypoints[0][1];
283
- }
284
- else {
285
- const typeCounts = {};
286
- for (const entrypoint of groupedEntrypoints) {
287
- 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
+ }
288
306
  }
289
307
  }
290
- }
291
- return currentEntry;
292
- };
308
+ return currentEntry;
309
+ };
310
+ }
293
311
  }
294
312
  else {
295
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.1",
3
+ "version": "2.10.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",