@plaudit/webpack-extensions 2.13.0 → 2.14.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.
- package/build/wordpress-scripts-wrapper/{BlockJSONStyleRemappingPlugin.d.ts → BlockJSONManagingPlugin.d.ts} +1 -1
- package/build/wordpress-scripts-wrapper/BlockJSONManagingPlugin.js +166 -0
- package/build/wordpress-scripts-wrapper/ExtensionsConfigFileGeneratorPlugin.js +5 -31
- package/build/wordpress-scripts-wrapper/php-serializer.d.ts +1 -0
- package/build/wordpress-scripts-wrapper/php-serializer.js +41 -0
- package/build/wordpress-scripts-wrapper.js +2 -2
- package/package.json +10 -10
- package/build/wordpress-scripts-wrapper/BlockJSONStyleRemappingPlugin.js +0 -160
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Compiler, type WebpackPluginInstance } from "webpack";
|
|
2
|
-
export default class
|
|
2
|
+
export default class BlockJSONManagingPlugin implements WebpackPluginInstance {
|
|
3
3
|
private readonly standaloneBlocks;
|
|
4
4
|
constructor(standaloneBlocks: boolean);
|
|
5
5
|
apply(compiler: Compiler): void;
|
|
@@ -0,0 +1,166 @@
|
|
|
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
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
7
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const webpack_1 = require("webpack");
|
|
9
|
+
const php_serializer_1 = __importDefault(require("./php-serializer"));
|
|
10
|
+
class BlockJSONManagingPlugin {
|
|
11
|
+
standaloneBlocks;
|
|
12
|
+
constructor(standaloneBlocks) {
|
|
13
|
+
this.standaloneBlocks = standaloneBlocks;
|
|
14
|
+
}
|
|
15
|
+
apply(compiler) {
|
|
16
|
+
compiler.hooks.compilation.tap("BlockJSONManagingPlugin", compilation => {
|
|
17
|
+
compilation.hooks.afterProcessAssets.tap("BlockJSONStyleRemappingPlugin_ProcessAssets", compilationAssets => {
|
|
18
|
+
const stats = compilation.getStats().toJson({
|
|
19
|
+
hash: true,
|
|
20
|
+
publicPath: true,
|
|
21
|
+
assets: true,
|
|
22
|
+
chunks: true,
|
|
23
|
+
modules: true,
|
|
24
|
+
source: true,
|
|
25
|
+
errorDetails: false,
|
|
26
|
+
timings: false
|
|
27
|
+
});
|
|
28
|
+
if (!stats.assets) {
|
|
29
|
+
throw new Error("Stats did not include assets despite them being requested");
|
|
30
|
+
}
|
|
31
|
+
if (!stats.modules) {
|
|
32
|
+
throw new Error("Stats did not include modules despite them being requested");
|
|
33
|
+
}
|
|
34
|
+
const assetSourceFiles = new Map(stats.assets
|
|
35
|
+
.map(asset => [asset.name, asset.info?.sourceFilename])
|
|
36
|
+
.filter((v) => v[0] !== undefined && v[1] !== undefined));
|
|
37
|
+
const singleFileChunkToOutputName = new Map(stats.assets
|
|
38
|
+
.filter((asset) => asset.chunks?.length === 1)
|
|
39
|
+
.filter(asset => !asset.name.endsWith('.asset.php'))
|
|
40
|
+
.map(asset => [asset.chunks[0], asset.name]));
|
|
41
|
+
const singleFileInputToOutputName = new Map(stats.modules
|
|
42
|
+
.map(module => {
|
|
43
|
+
if (module.nameForCondition !== undefined && module.chunks?.length === 1) {
|
|
44
|
+
const output = singleFileChunkToOutputName.get(module.chunks[0]);
|
|
45
|
+
return output !== undefined ? [module.nameForCondition, output] : undefined;
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
})
|
|
49
|
+
.filter((v) => v !== undefined));
|
|
50
|
+
const remapValue = (value, name, dirname) => {
|
|
51
|
+
if (value.startsWith("file:")) {
|
|
52
|
+
const styleInputPath = node_path_1.default.normalize(node_path_1.default.join(compiler.context, dirname, value.substring(5)));
|
|
53
|
+
const styleOutputPath = singleFileInputToOutputName.get(styleInputPath);
|
|
54
|
+
if (styleOutputPath !== undefined) {
|
|
55
|
+
const prefix = value.startsWith("./", 5) ? "./" : "";
|
|
56
|
+
const relativePath = node_path_1.default.relative(node_path_1.default.dirname(name), styleOutputPath);
|
|
57
|
+
return `file:${prefix}${relativePath}`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
};
|
|
62
|
+
const blockDirConfigData = {};
|
|
63
|
+
const mappableKeys = ["editorStyle", "style", "viewStyle", "viewScript", "script", "editorScript"];
|
|
64
|
+
for (const [name, asset] of Object.entries(compilationAssets)) {
|
|
65
|
+
if (name.endsWith("block.json")) {
|
|
66
|
+
blockDirConfigData[name] = true;
|
|
67
|
+
if (asset.constructor.name === 'RawSource') {
|
|
68
|
+
const dirname = node_path_1.default.dirname(assetSourceFiles.get(name) ?? name);
|
|
69
|
+
const json = JSON.parse(asset.source().toString());
|
|
70
|
+
for (const mappableKey of mappableKeys) {
|
|
71
|
+
if (mappableKey in json) {
|
|
72
|
+
const unmappedValue = json[mappableKey];
|
|
73
|
+
if (Array.isArray(unmappedValue)) {
|
|
74
|
+
json[mappableKey] = unmappedValue.map(value => remapValue(value, name, dirname));
|
|
75
|
+
}
|
|
76
|
+
else if (typeof unmappedValue === 'string') {
|
|
77
|
+
json[mappableKey] = remapValue(unmappedValue, name, dirname);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (!this.standaloneBlocks && json["plaudit"] !== "simple") {
|
|
82
|
+
const sourceDir = node_path_1.default.join(compiler.context, dirname);
|
|
83
|
+
const outputDir = node_path_1.default.join(compiler.outputPath, node_path_1.default.dirname(name));
|
|
84
|
+
const stripFilePrefix = (file) => file.startsWith("file:./") ? file.substring(7) : file;
|
|
85
|
+
let setupFiles = (json["plaudit"]?.["setup"]
|
|
86
|
+
? (typeof json["plaudit"]["setup"] === 'string' ? [json["plaudit"]["setup"]] : json["plaudit"]["setup"])
|
|
87
|
+
: ["setup.php"])
|
|
88
|
+
.map(p => node_path_1.default.normalize(node_path_1.default.join(sourceDir, stripFilePrefix(p))))
|
|
89
|
+
.filter(p => node_fs_1.default.existsSync(p))
|
|
90
|
+
.map(p => `file:./${this.findRelativeRouteBetween(outputDir, p)}`);
|
|
91
|
+
if (setupFiles.length === 0) {
|
|
92
|
+
if (json["plaudit"]?.["setup"] !== undefined) {
|
|
93
|
+
delete json["plaudit"]["setup"];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
if (typeof json["plaudit"] !== 'object') {
|
|
98
|
+
json["plaudit"] = {};
|
|
99
|
+
}
|
|
100
|
+
json["plaudit"]["setup"] = setupFiles.length === 1 ? setupFiles[0] : setupFiles;
|
|
101
|
+
}
|
|
102
|
+
if (json["render"] && (json["acf"] || json["plaudit"])) {
|
|
103
|
+
json["render_template"] = json["render"];
|
|
104
|
+
delete json["render"];
|
|
105
|
+
}
|
|
106
|
+
if (json["acf"]) {
|
|
107
|
+
if (json["acf"]["renderTemplate"]) {
|
|
108
|
+
json["render_template"] = json["acf"]["renderTemplate"];
|
|
109
|
+
delete json["acf"]["renderTemplate"];
|
|
110
|
+
}
|
|
111
|
+
else if (json["acf"]["render_template"]) {
|
|
112
|
+
json["render_template"] = json["acf"]["render_template"];
|
|
113
|
+
delete json["acf"]["render_template"];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let renderTemplate = json["render"] ?? json["render_template"];
|
|
117
|
+
renderTemplate = (renderTemplate
|
|
118
|
+
? (typeof renderTemplate === 'string' ? [renderTemplate] : renderTemplate)
|
|
119
|
+
: ["template.php", "template.twig"])
|
|
120
|
+
.map(p => node_path_1.default.normalize(node_path_1.default.join(sourceDir, stripFilePrefix(p))))
|
|
121
|
+
.filter(p => node_fs_1.default.existsSync(p))
|
|
122
|
+
.map(p => `file:./${this.findRelativeRouteBetween(outputDir, p)}`);
|
|
123
|
+
if (renderTemplate.length === 0) {
|
|
124
|
+
delete json["render_template"];
|
|
125
|
+
delete json["render"];
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
let validTemplateLocation, invalidTemplateLocation;
|
|
129
|
+
if (json["acf"] || json["plaudit"]) {
|
|
130
|
+
validTemplateLocation = "render_template";
|
|
131
|
+
invalidTemplateLocation = "render";
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
validTemplateLocation = "render";
|
|
135
|
+
invalidTemplateLocation = "render_template";
|
|
136
|
+
}
|
|
137
|
+
delete json[invalidTemplateLocation];
|
|
138
|
+
json[validTemplateLocation] = renderTemplate.length === 1 ? renderTemplate[0] : renderTemplate;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
compilation.updateAsset(name, new webpack_1.sources.RawSource(JSON.stringify(json, undefined, " ")));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
compilation.emitAsset("blockdir.config", new webpack_1.sources.RawSource((0, php_serializer_1.default)(blockDirConfigData)));
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
findCommonAncestor(...paths) {
|
|
150
|
+
return paths.map(p => node_path_1.default.normalize(p).split(node_path_1.default.sep)).reduce((prior, current) => {
|
|
151
|
+
for (let i = 0, limit = Math.min(prior.length, current.length); i < limit; i++) {
|
|
152
|
+
if (prior[i] !== current[i]) {
|
|
153
|
+
return prior.slice(0, i);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return current.length < prior.length ? current : prior;
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
findRelativeRouteBetween(path1, path2) {
|
|
160
|
+
const commonAncestor = this.findCommonAncestor(path1, path2);
|
|
161
|
+
const route = Array(path1.split(node_path_1.default.sep).length - commonAncestor.length).fill("..");
|
|
162
|
+
route.push(node_path_1.default.relative(commonAncestor.join(node_path_1.default.sep), path2));
|
|
163
|
+
return route.join(node_path_1.default.sep);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
exports.default = BlockJSONManagingPlugin;
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
7
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
8
|
const webpack_1 = require("webpack");
|
|
9
|
+
const php_serializer_1 = __importDefault(require("./php-serializer"));
|
|
9
10
|
class ExtensionsConfigFileGeneratorPlugin {
|
|
10
11
|
extensionsPath;
|
|
11
12
|
constructor(extensionsPath) {
|
|
@@ -55,15 +56,10 @@ class ExtensionsConfigFileGeneratorPlugin {
|
|
|
55
56
|
}
|
|
56
57
|
let match;
|
|
57
58
|
if ((match = /^(.+?)-setup.php$/i.exec(asset.name)) && match[1]) {
|
|
58
|
-
|
|
59
|
-
mapping[match[1]] = { resources: {} };
|
|
60
|
-
}
|
|
61
|
-
mapping[match[1]].setup = `${asset.name}`;
|
|
59
|
+
(mapping[match[1]] ?? (mapping[match[1]] = [{}]))[1] = `${asset.name}`;
|
|
62
60
|
}
|
|
63
61
|
else if ((match = /^(.+?)-(editor-style|style|view-script|editor-script|script)\.(?:css|m?js)$/i.exec(asset.name)) && match[1] && match[2]) {
|
|
64
|
-
|
|
65
|
-
mapping[match[1]] = { resources: {} };
|
|
66
|
-
}
|
|
62
|
+
const resourceInfo = (mapping[match[1]] ?? (mapping[match[1]] = [{}]))[0];
|
|
67
63
|
let key;
|
|
68
64
|
if (match[2] === "editor-script") {
|
|
69
65
|
key = "editorScript";
|
|
@@ -77,32 +73,10 @@ class ExtensionsConfigFileGeneratorPlugin {
|
|
|
77
73
|
else {
|
|
78
74
|
key = match[2];
|
|
79
75
|
}
|
|
80
|
-
(
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
let contents = `a:${Object.keys(mapping).length}:{`;
|
|
84
|
-
for (const [blockSlug, { setup, resources }] of Object.entries(mapping)) {
|
|
85
|
-
contents += `s:${blockSlug.length}:"${blockSlug}";`;
|
|
86
|
-
contents += `a:${setup ? 2 : 1}:{`; // Open the mapping for the current block as a whole
|
|
87
|
-
// Open the mapping for the current block's resource files
|
|
88
|
-
contents += `i:0;a:${Object.entries(resources).length}:{`;
|
|
89
|
-
for (const [mapping, handlerFiles] of Object.entries(resources)) {
|
|
90
|
-
contents += `s:${mapping.length}:"${mapping}";`;
|
|
91
|
-
contents += `a:${handlerFiles.length}:{`;
|
|
92
|
-
let count = 0;
|
|
93
|
-
for (const handlerFile of handlerFiles) {
|
|
94
|
-
contents += `i:${count++};a:2:{i:0;s:${handlerFile[0].length}:"${handlerFile[0]}";i:1;s:${handlerFile[1].length}:"${handlerFile[1]}";}`;
|
|
95
|
-
}
|
|
96
|
-
contents += "}";
|
|
97
|
-
}
|
|
98
|
-
contents += "}"; // Close the mapping for the current block's resource files
|
|
99
|
-
if (setup) {
|
|
100
|
-
contents += `i:1;s:${setup.length}:"${setup}";`;
|
|
76
|
+
(resourceInfo[key] ?? (resourceInfo[key] = [])).push([`plaudit_block-extension_${match[1]}-${match[2]}`, asset.name]);
|
|
101
77
|
}
|
|
102
|
-
contents += "}"; // Close the mapping for the current block as a whole
|
|
103
78
|
}
|
|
104
|
-
|
|
105
|
-
compilation.emitAsset("mapping.config", new webpack_1.sources.RawSource(contents.trim()));
|
|
79
|
+
compilation.emitAsset("mapping.config", new webpack_1.sources.RawSource((0, php_serializer_1.default)(mapping)));
|
|
106
80
|
});
|
|
107
81
|
});
|
|
108
82
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function phpSerialize(value: any, objectsAsArrays?: boolean): string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function phpSerialize(value, objectsAsArrays = true) {
|
|
4
|
+
switch (typeof value) {
|
|
5
|
+
case "undefined":
|
|
6
|
+
return "N;";
|
|
7
|
+
case "object":
|
|
8
|
+
if (value === null) {
|
|
9
|
+
return "N;"; // Yep. Null is an "object" in JavaScript
|
|
10
|
+
}
|
|
11
|
+
else if (Array.isArray(value)) {
|
|
12
|
+
let res = `a:${value.length}:{`;
|
|
13
|
+
for (let i = 0; i < value.length; i++) {
|
|
14
|
+
res += `i:${i};${phpSerialize(value[i], objectsAsArrays)}`;
|
|
15
|
+
}
|
|
16
|
+
return (res.endsWith('};') ? res.substring(0, res.length - 1) : res) + "}";
|
|
17
|
+
}
|
|
18
|
+
return entriesInArrayAndObjectFormat(objectsAsArrays ? 'a' : 'O:8:"stdClass"', Object.entries(value), objectsAsArrays);
|
|
19
|
+
case "boolean":
|
|
20
|
+
return `b:${value ? 1 : 0};`;
|
|
21
|
+
case "number":
|
|
22
|
+
return `${Number.isInteger(value) ? 'i' : 'd'}:${value};`;
|
|
23
|
+
case "string":
|
|
24
|
+
return `s:${value.length}:"${value}";`;
|
|
25
|
+
case "function":
|
|
26
|
+
throw new Error(`We are unable to serialize functions into a form readable by PHP.`);
|
|
27
|
+
case "symbol":
|
|
28
|
+
value = value.toString();
|
|
29
|
+
return `s:${value.length}:"${value}";`;
|
|
30
|
+
case "bigint":
|
|
31
|
+
return `i:${value};`;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.default = phpSerialize;
|
|
35
|
+
function entriesInArrayAndObjectFormat(prefix, entries, objectsAsArrays) {
|
|
36
|
+
let res = `${prefix}:${entries.length}:{`;
|
|
37
|
+
for (const [k, v] of entries) {
|
|
38
|
+
res += `${phpSerialize(k, objectsAsArrays)}${phpSerialize(v, objectsAsArrays)}`;
|
|
39
|
+
}
|
|
40
|
+
return (res.endsWith('};') ? res.substring(0, res.length - 1) : res) + "}";
|
|
41
|
+
}
|
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
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
|
-
const
|
|
8
|
+
const BlockJSONManagingPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/BlockJSONManagingPlugin"));
|
|
9
9
|
const ExtensionsConfigFileGeneratorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/ExtensionsConfigFileGeneratorPlugin"));
|
|
10
10
|
const VariablesJSMonitorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/VariablesJSMonitorPlugin"));
|
|
11
11
|
const BrowserSyncPlugin_1 = require("./wordpress-scripts-wrapper/BrowserSyncPlugin");
|
|
@@ -253,7 +253,7 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
|
|
|
253
253
|
plugins.push(new VariablesJSMonitorPlugin_1.default(variablesFilePath));
|
|
254
254
|
}
|
|
255
255
|
if (copyFiles && (typeof dest === 'string' || dest.directoryLayout !== 'extensions')) {
|
|
256
|
-
plugins.push(new
|
|
256
|
+
plugins.push(new BlockJSONManagingPlugin_1.default(standaloneBlocks));
|
|
257
257
|
}
|
|
258
258
|
plugins.push(new AdditionalDependencyInjectorPlugin_1.default(typeof dest !== 'string' && dest.additionalDependencies ? dest.additionalDependencies : []));
|
|
259
259
|
if (srcIsDirectory && (typeof dest !== 'string' && dest.directoryLayout === 'extensions')) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plaudit/webpack-extensions",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.14.0",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"prepublishOnly": "rm -rf build && mkdir build && tsc",
|
|
6
6
|
"build": "tsc",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@types/browser-sync-webpack-plugin": "^2.2.3",
|
|
22
|
-
"@types/node": "^20.11.
|
|
22
|
+
"@types/node": "^20.11.20",
|
|
23
23
|
"@types/tapable": "^2.2.7",
|
|
24
24
|
"@types/webpack": "^5.28.5",
|
|
25
25
|
"@types/webpack-sources": "^3.2.3",
|
|
@@ -33,21 +33,21 @@
|
|
|
33
33
|
"@plaudit/postcss-silent-extend": "^3.0.0",
|
|
34
34
|
"@plaudit/postcss-strip-units": "^3.0.0",
|
|
35
35
|
"@plaudit/postcss-variables": "^1.0.0",
|
|
36
|
-
"@wordpress/scripts": "^27.
|
|
36
|
+
"@wordpress/scripts": "^27.3.0",
|
|
37
37
|
"autoprefixer": "^10.4.17",
|
|
38
38
|
"browser-sync": "^3.0.2",
|
|
39
39
|
"clean-webpack-plugin": "^4.0.0",
|
|
40
|
-
"copy-webpack-plugin": "^
|
|
41
|
-
"cssnano": "^6.0.
|
|
42
|
-
"eslint": "^8.
|
|
43
|
-
"eslint-plugin-jsdoc": "^48.0
|
|
40
|
+
"copy-webpack-plugin": "^12.0.2",
|
|
41
|
+
"cssnano": "^6.0.5",
|
|
42
|
+
"eslint": "^8.57.0",
|
|
43
|
+
"eslint-plugin-jsdoc": "^48.2.0",
|
|
44
44
|
"fork-ts-checker-webpack-plugin": "^9.0.2",
|
|
45
|
-
"postcss": "^8.4.
|
|
45
|
+
"postcss": "^8.4.35",
|
|
46
46
|
"postcss-calc": "^9.0.1",
|
|
47
47
|
"postcss-discard-comments": "^6.0.1",
|
|
48
48
|
"postcss-fallback": "^0.1.0",
|
|
49
49
|
"postcss-functions": "^4.0.2",
|
|
50
|
-
"postcss-import": "^16.0.
|
|
50
|
+
"postcss-import": "^16.0.1",
|
|
51
51
|
"postcss-inline-svg": "^6.0.0",
|
|
52
52
|
"postcss-media-minmax": "^5.0.0",
|
|
53
53
|
"postcss-mixins": "6.2.3",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"postcss-url": "^10.1.3",
|
|
63
63
|
"react": "^18.2.0",
|
|
64
64
|
"react-dom": "^18.2.0",
|
|
65
|
-
"webpack": "^5.90.
|
|
65
|
+
"webpack": "^5.90.3",
|
|
66
66
|
"webpack-remove-empty-scripts": "^1.0.4"
|
|
67
67
|
}
|
|
68
68
|
}
|
|
@@ -1,160 +0,0 @@
|
|
|
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
|
-
const node_fs_1 = __importDefault(require("node:fs"));
|
|
7
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
-
const webpack_1 = require("webpack");
|
|
9
|
-
class BlockJSONStyleRemappingPlugin {
|
|
10
|
-
standaloneBlocks;
|
|
11
|
-
constructor(standaloneBlocks) {
|
|
12
|
-
this.standaloneBlocks = standaloneBlocks;
|
|
13
|
-
}
|
|
14
|
-
apply(compiler) {
|
|
15
|
-
compiler.hooks.compilation.tap("BlockJSONStyleRemappingPlugin", compilation => {
|
|
16
|
-
compilation.hooks.afterProcessAssets.tap("BlockJSONStyleRemappingPlugin_ProcessAssets", compilationAssets => {
|
|
17
|
-
const stats = compilation.getStats().toJson({
|
|
18
|
-
hash: true,
|
|
19
|
-
publicPath: true,
|
|
20
|
-
assets: true,
|
|
21
|
-
chunks: true,
|
|
22
|
-
modules: true,
|
|
23
|
-
source: true,
|
|
24
|
-
errorDetails: false,
|
|
25
|
-
timings: false
|
|
26
|
-
});
|
|
27
|
-
if (!stats.assets) {
|
|
28
|
-
throw new Error("Stats did not include assets despite them being requested");
|
|
29
|
-
}
|
|
30
|
-
if (!stats.modules) {
|
|
31
|
-
throw new Error("Stats did not include modules despite them being requested");
|
|
32
|
-
}
|
|
33
|
-
const assetSourceFiles = new Map(stats.assets
|
|
34
|
-
.map(asset => [asset.name, asset.info?.sourceFilename])
|
|
35
|
-
.filter((v) => v[0] !== undefined && v[1] !== undefined));
|
|
36
|
-
const singleFileChunkToOutputName = new Map(stats.assets
|
|
37
|
-
.filter((asset) => asset.chunks?.length === 1)
|
|
38
|
-
.filter(asset => !asset.name.endsWith('.asset.php'))
|
|
39
|
-
.map(asset => [asset.chunks[0], asset.name]));
|
|
40
|
-
const singleFileInputToOutputName = new Map(stats.modules
|
|
41
|
-
.map(module => {
|
|
42
|
-
if (module.nameForCondition !== undefined && module.chunks?.length === 1) {
|
|
43
|
-
const output = singleFileChunkToOutputName.get(module.chunks[0]);
|
|
44
|
-
return output !== undefined ? [module.nameForCondition, output] : undefined;
|
|
45
|
-
}
|
|
46
|
-
return undefined;
|
|
47
|
-
})
|
|
48
|
-
.filter((v) => v !== undefined));
|
|
49
|
-
const remapValue = (value, name, dirname) => {
|
|
50
|
-
if (value.startsWith("file:")) {
|
|
51
|
-
const styleInputPath = node_path_1.default.normalize(node_path_1.default.join(compiler.context, dirname, value.substring(5)));
|
|
52
|
-
const styleOutputPath = singleFileInputToOutputName.get(styleInputPath);
|
|
53
|
-
if (styleOutputPath !== undefined) {
|
|
54
|
-
const prefix = value.startsWith("./", 5) ? "./" : "";
|
|
55
|
-
const relativePath = node_path_1.default.relative(node_path_1.default.dirname(name), styleOutputPath);
|
|
56
|
-
return `file:${prefix}${relativePath}`;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return value;
|
|
60
|
-
};
|
|
61
|
-
const mappableKeys = ["editorStyle", "style", "viewStyle", "viewScript", "script", "editorScript"];
|
|
62
|
-
for (const [name, asset] of Object.entries(compilationAssets)) {
|
|
63
|
-
if (name.endsWith("block.json") && asset.constructor.name === 'RawSource') {
|
|
64
|
-
const dirname = node_path_1.default.dirname(assetSourceFiles.get(name) ?? name);
|
|
65
|
-
const json = JSON.parse(asset.source().toString());
|
|
66
|
-
for (const mappableKey of mappableKeys) {
|
|
67
|
-
if (mappableKey in json) {
|
|
68
|
-
const unmappedValue = json[mappableKey];
|
|
69
|
-
if (Array.isArray(unmappedValue)) {
|
|
70
|
-
json[mappableKey] = unmappedValue.map(value => remapValue(value, name, dirname));
|
|
71
|
-
}
|
|
72
|
-
else if (typeof unmappedValue === 'string') {
|
|
73
|
-
json[mappableKey] = remapValue(unmappedValue, name, dirname);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
if (!this.standaloneBlocks && json["plaudit"] !== "simple") {
|
|
78
|
-
const sourceDir = node_path_1.default.join(compiler.context, dirname);
|
|
79
|
-
const outputDir = node_path_1.default.join(compiler.outputPath, node_path_1.default.dirname(name));
|
|
80
|
-
const stripFilePrefix = (file) => file.startsWith("file:./") ? file.substring(7) : file;
|
|
81
|
-
let setupFiles = (json["plaudit"]?.["setup"]
|
|
82
|
-
? (typeof json["plaudit"]["setup"] === 'string' ? [json["plaudit"]["setup"]] : json["plaudit"]["setup"])
|
|
83
|
-
: ["setup.php"])
|
|
84
|
-
.map(p => node_path_1.default.normalize(node_path_1.default.join(sourceDir, stripFilePrefix(p))))
|
|
85
|
-
.filter(p => node_fs_1.default.existsSync(p))
|
|
86
|
-
.map(p => `file:./${this.findRelativeRouteBetween(outputDir, p)}`);
|
|
87
|
-
if (setupFiles.length === 0) {
|
|
88
|
-
if (json["plaudit"]?.["setup"] !== undefined) {
|
|
89
|
-
delete json["plaudit"]["setup"];
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
if (typeof json["plaudit"] !== 'object') {
|
|
94
|
-
json["plaudit"] = {};
|
|
95
|
-
}
|
|
96
|
-
json["plaudit"]["setup"] = setupFiles.length === 1 ? setupFiles[0] : setupFiles;
|
|
97
|
-
}
|
|
98
|
-
if (json["render"] && (json["acf"] || json["plaudit"])) {
|
|
99
|
-
json["render_template"] = json["render"];
|
|
100
|
-
delete json["render"];
|
|
101
|
-
}
|
|
102
|
-
if (json["acf"]) {
|
|
103
|
-
if (json["acf"]["renderTemplate"]) {
|
|
104
|
-
json["render_template"] = json["acf"]["renderTemplate"];
|
|
105
|
-
delete json["acf"]["renderTemplate"];
|
|
106
|
-
}
|
|
107
|
-
else if (json["acf"]["render_template"]) {
|
|
108
|
-
json["render_template"] = json["acf"]["render_template"];
|
|
109
|
-
delete json["acf"]["render_template"];
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
let renderTemplate = json["render"] ?? json["render_template"];
|
|
113
|
-
renderTemplate = (renderTemplate
|
|
114
|
-
? (typeof renderTemplate === 'string' ? [renderTemplate] : renderTemplate)
|
|
115
|
-
: ["template.php", "template.twig"])
|
|
116
|
-
.map(p => node_path_1.default.normalize(node_path_1.default.join(sourceDir, stripFilePrefix(p))))
|
|
117
|
-
.filter(p => node_fs_1.default.existsSync(p))
|
|
118
|
-
.map(p => `file:./${this.findRelativeRouteBetween(outputDir, p)}`);
|
|
119
|
-
if (renderTemplate.length === 0) {
|
|
120
|
-
delete json["render_template"];
|
|
121
|
-
delete json["render"];
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
let validTemplateLocation, invalidTemplateLocation;
|
|
125
|
-
if (json["acf"] || json["plaudit"]) {
|
|
126
|
-
validTemplateLocation = "render_template";
|
|
127
|
-
invalidTemplateLocation = "render";
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
validTemplateLocation = "render";
|
|
131
|
-
invalidTemplateLocation = "render_template";
|
|
132
|
-
}
|
|
133
|
-
delete json[invalidTemplateLocation];
|
|
134
|
-
json[validTemplateLocation] = renderTemplate.length === 1 ? renderTemplate[0] : renderTemplate;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
compilation.updateAsset(name, new webpack_1.sources.RawSource(JSON.stringify(json, undefined, " ")));
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
findCommonAncestor(...paths) {
|
|
144
|
-
return paths.map(p => node_path_1.default.normalize(p).split(node_path_1.default.sep)).reduce((prior, current) => {
|
|
145
|
-
for (let i = 0, limit = Math.min(prior.length, current.length); i < limit; i++) {
|
|
146
|
-
if (prior[i] !== current[i]) {
|
|
147
|
-
return prior.slice(0, i);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
return current.length < prior.length ? current : prior;
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
findRelativeRouteBetween(path1, path2) {
|
|
154
|
-
const commonAncestor = this.findCommonAncestor(path1, path2);
|
|
155
|
-
const route = Array(path1.split(node_path_1.default.sep).length - commonAncestor.length).fill("..");
|
|
156
|
-
route.push(node_path_1.default.relative(commonAncestor.join(node_path_1.default.sep), path2));
|
|
157
|
-
return route.join(node_path_1.default.sep);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
exports.default = BlockJSONStyleRemappingPlugin;
|