@plaudit/webpack-extensions 2.70.1 → 2.72.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/plugins/ExtensionsConfigFileGeneratorPlugin.js +5 -5
- package/build/plugins/ExtensionsConfigFileGeneratorPluginV1.js +4 -4
- package/build/plugins/PlainEntrypointsConfigFileGeneratorPlugin.js +6 -5
- package/build/plugins/PlainEntrypointsStyleBlockJSONPlugin.d.ts +3 -3
- package/build/plugins/PlainEntrypointsStyleBlockJSONPlugin.js +37 -19
- package/build/plugins/SpecialAssetHandlingPlugin.js +7 -6
- package/build/plugins/UnifiedLoaderGenerator.d.ts +1 -1
- package/build/plugins/UnifiedLoaderGenerator.js +3 -4
- package/build/shared.d.ts +3 -2
- package/build/shared.js +18 -13
- package/build/wordpress-scripts-wrapper.js +7 -6
- package/package.json +5 -4
- package/build/utils/json-to-php-but-with-__-injection.d.ts +0 -11
- package/build/utils/json-to-php-but-with-__-injection.js +0 -53
- package/build/utils/php-serializer.d.ts +0 -6
- package/build/utils/php-serializer.js +0 -53
- package/build/utils/php-writer.d.ts +0 -237
- package/build/utils/php-writer.js +0 -634
|
@@ -6,10 +6,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.ExtensionsConfigFileGeneratorPlugin = void 0;
|
|
7
7
|
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
8
8
|
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const php_writer_1 = require("@plaudit/php-writer");
|
|
10
|
+
const expressions_1 = require("@plaudit/php-writer/expressions");
|
|
9
11
|
const AbstractBiPhasicGroupAndEntryPlugin_1 = require("./AbstractBiPhasicGroupAndEntryPlugin");
|
|
10
12
|
const UnifiedLoaderGenerator_1 = require("./UnifiedLoaderGenerator");
|
|
11
13
|
const shared_1 = require("../shared");
|
|
12
|
-
const php_writer_1 = require("../utils/php-writer");
|
|
13
14
|
const pseduo_semaphore_1 = require("../utils/pseduo-semaphore");
|
|
14
15
|
const webpack_1 = require("webpack");
|
|
15
16
|
class ExtensionsConfigFileGeneratorPlugin extends AbstractBiPhasicGroupAndEntryPlugin_1.AbstractBiPhasicGroupAndEntryPlugin {
|
|
@@ -98,10 +99,9 @@ class ExtensionsConfigFileGeneratorPlugin extends AbstractBiPhasicGroupAndEntryP
|
|
|
98
99
|
return [block[0], Object.fromEntries(Object.entries(block[1]).toSorted(([a], [b]) => a.localeCompare(b)))];
|
|
99
100
|
})
|
|
100
101
|
.toSorted(([a], [b]) => a.localeCompare(b)));
|
|
101
|
-
new php_writer_1.PHPWriter()
|
|
102
|
-
.call("Plaudit\\Common\\Lib\\GutenbergUtils::loadExtensionsV3", [new
|
|
103
|
-
|
|
104
|
-
.emitAsset(compilation, node_path_1.default.join(this.dest.destination, "extensions-loader.php"));
|
|
102
|
+
(0, shared_1.emitPHPWriterAsAsset)(new php_writer_1.PHPWriter()
|
|
103
|
+
.call("Plaudit\\Common\\Lib\\GutenbergUtils::loadExtensionsV3", [new expressions_1.EnclosedLiteral((0, shared_1.makeEmittableConfigPHP)(blockExtensionsConfig, false)), expressions_1.Op.concat(expressions_1.Constants.__DIR__, '/'),
|
|
104
|
+
expressions_1.Expr.call("plaudit_webpack_extensions__resolve_base_uri", [expressions_1.Constants.__DIR__])]), compilation, node_path_1.default.join(this.dest.destination, "extensions-loader.php"));
|
|
105
105
|
}
|
|
106
106
|
attachUniquePhase(compilation) {
|
|
107
107
|
compilation.hooks.processAssets.tapPromise({ name: this.constructor.name, stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_REPORT }, async () => {
|
|
@@ -5,10 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ExtensionsConfigFileGeneratorPluginV1 = void 0;
|
|
7
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const php_writer_1 = require("@plaudit/php-writer");
|
|
9
|
+
const expressions_1 = require("@plaudit/php-writer/expressions");
|
|
8
10
|
const AbstractBiPhasicGroupAndEntryPlugin_1 = require("./AbstractBiPhasicGroupAndEntryPlugin");
|
|
9
11
|
const UnifiedLoaderGenerator_1 = require("./UnifiedLoaderGenerator");
|
|
10
|
-
const php_serializer_1 = require("../utils/php-serializer");
|
|
11
|
-
const php_writer_1 = require("../utils/php-writer");
|
|
12
12
|
const webpack_1 = require("webpack");
|
|
13
13
|
class ExtensionsConfigFileGeneratorPluginV1 extends AbstractBiPhasicGroupAndEntryPlugin_1.AbstractBiPhasicGroupAndEntryPlugin {
|
|
14
14
|
extensionsDest;
|
|
@@ -28,11 +28,11 @@ class ExtensionsConfigFileGeneratorPluginV1 extends AbstractBiPhasicGroupAndEntr
|
|
|
28
28
|
(resourceInfo[key] ?? (resourceInfo[key] = [])).push([`plaudit_block-extension_${match[1]}-${match[2]}`, fileWithAssetData]);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
compilation.emitAsset(node_path_1.default.join(this.extensionsDest, "mapping.config"), new webpack_1.sources.RawSource((0,
|
|
31
|
+
compilation.emitAsset(node_path_1.default.join(this.extensionsDest, "mapping.config"), new webpack_1.sources.RawSource((0, php_writer_1.phpSerialize)(mapping)));
|
|
32
32
|
UnifiedLoaderGenerator_1.UnifiedLoaderGenerator.semaphore.resolve(this.id, { group: `extensions-config-v1-${this.extensionsDest}`, requiresBaseURI: false, action: writer => {
|
|
33
33
|
writer
|
|
34
34
|
.use("Plaudit\\Common\\Lib\\GutenbergUtils")
|
|
35
|
-
.call("GutenbergUtils::installExtensionsSupport", [
|
|
35
|
+
.call("GutenbergUtils::installExtensionsSupport", [expressions_1.Op.concat(expressions_1.Constants.__DIR__, this.extensionsDest)]);
|
|
36
36
|
} });
|
|
37
37
|
}
|
|
38
38
|
}
|
|
@@ -5,8 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.PlainEntrypointsConfigFileGeneratorPlugin = void 0;
|
|
7
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const php_writer_1 = require("@plaudit/php-writer");
|
|
9
|
+
const expressions_1 = require("@plaudit/php-writer/expressions");
|
|
8
10
|
const shared_1 = require("../shared");
|
|
9
|
-
const php_writer_1 = require("../utils/php-writer");
|
|
10
11
|
const pseduo_semaphore_1 = require("../utils/pseduo-semaphore");
|
|
11
12
|
const AbstractBiPhasicGroupAndEntryPlugin_1 = require("./AbstractBiPhasicGroupAndEntryPlugin");
|
|
12
13
|
const UnifiedLoaderGenerator_1 = require("./UnifiedLoaderGenerator");
|
|
@@ -78,12 +79,12 @@ class PlainEntrypointsConfigFileGeneratorPlugin extends AbstractBiPhasicGroupAnd
|
|
|
78
79
|
if (!this.useUnifiedLoader) {
|
|
79
80
|
(0, shared_1.emitResolveBaseUriFunction)(writer);
|
|
80
81
|
}
|
|
81
|
-
const baseUriVar = new
|
|
82
|
+
const baseUriVar = new expressions_1.Var("base_uri");
|
|
82
83
|
for (const [priority, prioritizedHandleList] of PlainEntrypointsConfigFileGeneratorPlugin.separateHandleListByPriority(handleLists.register)) {
|
|
83
84
|
writer.action("init", writer => {
|
|
84
|
-
writer.call("plaudit_webpack_extensions__resolve_base_uri", [
|
|
85
|
+
writer.call("plaudit_webpack_extensions__resolve_base_uri", [expressions_1.Constants.__DIR__], { assignTo: baseUriVar });
|
|
85
86
|
for (const { handle, type, data } of prioritizedHandleList) {
|
|
86
|
-
writer.call(`wp_register_${type}`, [handle,
|
|
87
|
+
writer.call(`wp_register_${type}`, [handle, expressions_1.Op.concat(baseUriVar, node_path_1.default.relative(emitDir, data.src)), ...data.rest]);
|
|
87
88
|
}
|
|
88
89
|
}, { priority, accountForAlreadyDoing: this.config.includePostInitFallback });
|
|
89
90
|
}
|
|
@@ -100,7 +101,7 @@ class PlainEntrypointsConfigFileGeneratorPlugin extends AbstractBiPhasicGroupAnd
|
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
103
|
PlainEntrypointsConfigFileGeneratorPlugin.appendEnqueuingHandleLists(writer, handleLists);
|
|
103
|
-
|
|
104
|
+
(0, shared_1.emitPHPWriterAsAsset)(writer, compilation, "plain-entrypoints-loader.php");
|
|
104
105
|
}
|
|
105
106
|
static addHandlesToHandleLists(type, handles, handleLists) {
|
|
106
107
|
for (const [handle, data] of handles) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AbstractBiPhasicGroupAndEntryPlugin, EntryProvider } from "./AbstractBiPhasicGroupAndEntryPlugin";
|
|
2
|
-
import { ParsedAssetsJson, BlockEntrypointInfo, FileSegmentBlockEntrypointInfo } from "../shared";
|
|
2
|
+
import { ParsedAssetsJson, BlockEntrypointInfo, FileSegmentBlockEntrypointInfo, VerifiedAdvancedOutputConfig } from "../shared";
|
|
3
3
|
import type { VerifiedPlauditWordpressWebpackConfig } from "../utils/common-config-helpers";
|
|
4
4
|
import { Compilation } from "webpack";
|
|
5
5
|
import type WebpackRemoveEmptyScriptsPlugin from "webpack-remove-empty-scripts";
|
|
@@ -14,10 +14,10 @@ type CollatedWorkableBlockInfo = Record<string, {
|
|
|
14
14
|
blockJsonText?: string;
|
|
15
15
|
}>;
|
|
16
16
|
export declare class PlainEntrypointsStyleBlockJSONPlugin extends AbstractBiPhasicGroupAndEntryPlugin<BlockEntrypointInfo> {
|
|
17
|
-
private readonly
|
|
17
|
+
private readonly dest;
|
|
18
18
|
private readonly webpackRemoveEmptyScriptsPlugin;
|
|
19
19
|
private static readonly semaphore;
|
|
20
|
-
constructor(config: VerifiedPlauditWordpressWebpackConfig,
|
|
20
|
+
constructor(config: VerifiedPlauditWordpressWebpackConfig, dest: VerifiedAdvancedOutputConfig, webpackRemoveEmptyScriptsPlugin: WebpackRemoveEmptyScriptsPlugin, context: string, entry: EntryProvider<BlockEntrypointInfo>);
|
|
21
21
|
protected processAssets(compilation: Compilation, rawAssetData: ParsedAssetsJson): Promise<void>;
|
|
22
22
|
private emitBlockLoaderFile;
|
|
23
23
|
private transformBlocks;
|
|
@@ -10,17 +10,18 @@ const node_fs_1 = __importDefault(require("node:fs"));
|
|
|
10
10
|
const node_path_1 = __importDefault(require("node:path"));
|
|
11
11
|
const AbstractBiPhasicGroupAndEntryPlugin_1 = require("./AbstractBiPhasicGroupAndEntryPlugin");
|
|
12
12
|
const UnifiedLoaderGenerator_1 = require("./UnifiedLoaderGenerator");
|
|
13
|
+
const php_writer_1 = require("@plaudit/php-writer");
|
|
14
|
+
const expressions_1 = require("@plaudit/php-writer/expressions");
|
|
13
15
|
const shared_1 = require("../shared");
|
|
14
|
-
const php_writer_1 = require("../utils/php-writer");
|
|
15
16
|
const pseduo_semaphore_1 = require("../utils/pseduo-semaphore");
|
|
16
17
|
const webpack_1 = require("webpack");
|
|
17
18
|
class PlainEntrypointsStyleBlockJSONPlugin extends AbstractBiPhasicGroupAndEntryPlugin_1.AbstractBiPhasicGroupAndEntryPlugin {
|
|
18
|
-
|
|
19
|
+
dest;
|
|
19
20
|
webpackRemoveEmptyScriptsPlugin;
|
|
20
21
|
static semaphore = new pseduo_semaphore_1.PseudoSemaphore("Block JSON v3");
|
|
21
|
-
constructor(config,
|
|
22
|
-
super(config, `block-json-${
|
|
23
|
-
this.
|
|
22
|
+
constructor(config, dest, webpackRemoveEmptyScriptsPlugin, context, entry) {
|
|
23
|
+
super(config, `block-json-${dest.destination}`, [PlainEntrypointsStyleBlockJSONPlugin.semaphore, UnifiedLoaderGenerator_1.UnifiedLoaderGenerator.semaphore], context, entry);
|
|
24
|
+
this.dest = dest;
|
|
24
25
|
this.webpackRemoveEmptyScriptsPlugin = webpackRemoveEmptyScriptsPlugin;
|
|
25
26
|
}
|
|
26
27
|
async processAssets(compilation, rawAssetData) {
|
|
@@ -64,7 +65,7 @@ class PlainEntrypointsStyleBlockJSONPlugin extends AbstractBiPhasicGroupAndEntry
|
|
|
64
65
|
}
|
|
65
66
|
const workableBlockEntrypointsInfo = entrypoint.getFiles()
|
|
66
67
|
.map(file => this.stripOffBlocksDestPrefix(file))
|
|
67
|
-
.filter(file => !this.webpackRemoveEmptyScriptsPlugin['trash'].includes(node_path_1.default.join(this.
|
|
68
|
+
.filter(file => !this.webpackRemoveEmptyScriptsPlugin['trash'].includes(node_path_1.default.join(this.dest.destination, file)))
|
|
68
69
|
.map(file => [file, (0, shared_1.scriptOrStyleTest)(file, shared_1.scriptExtension)])
|
|
69
70
|
.filter((item) => item[1] !== '')
|
|
70
71
|
.map(([file, assetType]) => {
|
|
@@ -87,29 +88,46 @@ class PlainEntrypointsStyleBlockJSONPlugin extends AbstractBiPhasicGroupAndEntry
|
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
90
|
PlainEntrypointsStyleBlockJSONPlugin.semaphore.resolve(this.id, {
|
|
90
|
-
collatableWorkableBlockInfo: applicableBlockJsonFiles, blocksDest: this.
|
|
91
|
-
emittingWpmlXml: compilation.getAsset(node_path_1.default.join(this.
|
|
91
|
+
collatableWorkableBlockInfo: applicableBlockJsonFiles, blocksDest: this.dest.destination,
|
|
92
|
+
emittingWpmlXml: compilation.getAsset(node_path_1.default.join(this.dest.destination, "wpml-config.xml")) !== undefined
|
|
92
93
|
});
|
|
94
|
+
const generateLoader = (writer) => {
|
|
95
|
+
writer.require(node_path_1.default.join(this.dest.destination, "blockdir-loader.php"), { dirRelative: true, once: true });
|
|
96
|
+
};
|
|
97
|
+
let action;
|
|
98
|
+
const lazyLoader = this.dest.lazyLoader;
|
|
99
|
+
if (lazyLoader) {
|
|
100
|
+
try {
|
|
101
|
+
action = writer => writer.function(lazyLoader, [], generateLoader, { returnType: 'void' });
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
compilation.errors.push((0, shared_1.newWebpackErrorForFile)(["An error occurred while emitting a function for lazily loading blocks", { cause: e }], this.dest.destination));
|
|
105
|
+
this.rejectSemaphores();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
action = writer => writer.withScope(generateLoader);
|
|
111
|
+
}
|
|
93
112
|
UnifiedLoaderGenerator_1.UnifiedLoaderGenerator.semaphore.resolve(this.id, Object.keys(applicableBlockJsonFiles).length ? {
|
|
94
113
|
group: this.group,
|
|
95
114
|
requiresBaseURI: false,
|
|
96
115
|
staticallyLoadedEntrypoints: relevantEntrypoints.flatMap(re => re.entrypoint.getEntrypointChunk().files.values().toArray()),
|
|
97
|
-
action
|
|
116
|
+
action
|
|
98
117
|
} : undefined);
|
|
99
118
|
}
|
|
100
119
|
emitBlockLoaderFile(compilation, blockData) {
|
|
101
120
|
if (this.config.useUnifiedLoader) {
|
|
102
121
|
const metadata = blockData['__metadata'];
|
|
103
122
|
delete blockData['__metadata'];
|
|
104
|
-
new php_writer_1.PHPWriter()
|
|
123
|
+
(0, shared_1.emitPHPWriterAsAsset)(new php_writer_1.PHPWriter()
|
|
105
124
|
.action("init", writer => {
|
|
106
|
-
writer.call("\\Plaudit\\Common\\ACF\\BlockManager::autoloadSubfoldersV3", [
|
|
107
|
-
|
|
108
|
-
}, { accountForAlreadyDoing: this.config.includePostInitFallback })
|
|
109
|
-
.emitAsset(compilation, node_path_1.default.join(this.blocksDest, "blockdir-loader.php"));
|
|
125
|
+
writer.call("\\Plaudit\\Common\\ACF\\BlockManager::autoloadSubfoldersV3", [expressions_1.Constants.__DIR__, new expressions_1.EnclosedLiteral((0, shared_1.makeEmittableConfigPHP)(blockData, false, "\t")),
|
|
126
|
+
expressions_1.Constants.__FILE__, new expressions_1.EnclosedLiteral((0, shared_1.makeEmittableConfigPHP)(metadata, false, "\t")), null]);
|
|
127
|
+
}, { accountForAlreadyDoing: this.config.includePostInitFallback }), compilation, node_path_1.default.join(this.dest.destination, "blockdir-loader.php"));
|
|
110
128
|
}
|
|
111
129
|
else {
|
|
112
|
-
compilation.emitAsset(node_path_1.default.join(this.
|
|
130
|
+
compilation.emitAsset(node_path_1.default.join(this.dest.destination, "blockdir.config.php"), new webpack_1.sources.RawSource((0, shared_1.makeEmittableConfigPHP)(blockData, true)));
|
|
113
131
|
}
|
|
114
132
|
}
|
|
115
133
|
transformBlocks(compilation, collatedWorkableBlockInfo, emittingWpmlXml) {
|
|
@@ -141,7 +159,7 @@ class PlainEntrypointsStyleBlockJSONPlugin extends AbstractBiPhasicGroupAndEntry
|
|
|
141
159
|
blockJson["version"] = hashForVersion;
|
|
142
160
|
}
|
|
143
161
|
PlainEntrypointsStyleBlockJSONPlugin.normalizeRenderTemplate(blockJson, pathsNeedRemapping, sourceDir, outputDir, compilation);
|
|
144
|
-
const blockDirName = node_path_1.default.dirname(node_path_1.default.relative(this.
|
|
162
|
+
const blockDirName = node_path_1.default.dirname(node_path_1.default.relative(this.dest.destination, blockJsonAssetName));
|
|
145
163
|
blockData[blockDirName] = PlainEntrypointsStyleBlockJSONPlugin
|
|
146
164
|
.doFileOrHandleReplacements(compilation, Object.fromEntries(Object.entries(blockJson).filter(([k]) => k !== '$schema')), workableBlockEntrypointsInfo, epi => epi.handle);
|
|
147
165
|
compilation[blockJsonAssetName in compilation.assets ? 'updateAsset' : 'emitAsset'](blockJsonAssetName, new webpack_1.sources.RawSource(JSON.stringify(PlainEntrypointsStyleBlockJSONPlugin
|
|
@@ -215,7 +233,7 @@ class PlainEntrypointsStyleBlockJSONPlugin extends AbstractBiPhasicGroupAndEntry
|
|
|
215
233
|
return blockJson;
|
|
216
234
|
}
|
|
217
235
|
stripOffBlocksDestPrefix(file) {
|
|
218
|
-
return this.
|
|
236
|
+
return this.dest.destination && file.startsWith(this.dest.destination + "/") ? file.substring(this.dest.destination.length + 1 /* we also need to drop the "/" */) : file;
|
|
219
237
|
}
|
|
220
238
|
static hashThingForAsset(thing) {
|
|
221
239
|
return node_crypto_1.default.createHash('md5').update(thing).digest("hex").substring(0, 20).toLowerCase();
|
|
@@ -348,9 +366,9 @@ class PlainEntrypointsStyleBlockJSONPlugin extends AbstractBiPhasicGroupAndEntry
|
|
|
348
366
|
(acc[cur.blocksDest] ?? (acc[cur.blocksDest] = [])).push(cur);
|
|
349
367
|
return acc;
|
|
350
368
|
}, {});
|
|
351
|
-
const semaphoreData = allSemaphoreData[this.
|
|
369
|
+
const semaphoreData = allSemaphoreData[this.dest.destination];
|
|
352
370
|
if (semaphoreData === undefined) {
|
|
353
|
-
compilation.errors.push(new Error(`Semaphore data missing for ${this.
|
|
371
|
+
compilation.errors.push(new Error(`Semaphore data missing for ${this.dest.destination}`));
|
|
354
372
|
return;
|
|
355
373
|
}
|
|
356
374
|
const workableBlockInfo = semaphoreData.map(sd => sd.collatableWorkableBlockInfo)
|
|
@@ -5,8 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.SpecialAssetHandlingPlugin = void 0;
|
|
7
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const php_writer_1 = require("@plaudit/php-writer");
|
|
9
|
+
const expressions_1 = require("@plaudit/php-writer/expressions");
|
|
8
10
|
const shared_1 = require("../shared");
|
|
9
|
-
const php_writer_1 = require("../utils/php-writer");
|
|
10
11
|
const pseduo_semaphore_1 = require("../utils/pseduo-semaphore");
|
|
11
12
|
const AbstractBiPhasicGroupPlugin_1 = require("./AbstractBiPhasicGroupPlugin");
|
|
12
13
|
const UnifiedLoaderGenerator_1 = require("./UnifiedLoaderGenerator");
|
|
@@ -68,7 +69,7 @@ class SpecialAssetHandlingPlugin extends AbstractBiPhasicGroupPlugin_1.AbstractB
|
|
|
68
69
|
const writer = new php_writer_1.PHPWriter();
|
|
69
70
|
if (!preloadedAssets.length) {
|
|
70
71
|
if (!this.config.useUnifiedLoader) {
|
|
71
|
-
|
|
72
|
+
(0, shared_1.emitPHPWriterAsAsset)(writer, compilation, outputFile);
|
|
72
73
|
}
|
|
73
74
|
return;
|
|
74
75
|
}
|
|
@@ -76,8 +77,8 @@ class SpecialAssetHandlingPlugin extends AbstractBiPhasicGroupPlugin_1.AbstractB
|
|
|
76
77
|
(0, shared_1.emitResolveBaseUriFunction)(writer);
|
|
77
78
|
}
|
|
78
79
|
writer.action("wp_head", writer => {
|
|
79
|
-
const baseUriVar = new
|
|
80
|
-
writer.call("plaudit_webpack_extensions__resolve_base_uri", [
|
|
80
|
+
const baseUriVar = new expressions_1.Var("base_uri");
|
|
81
|
+
writer.call("plaudit_webpack_extensions__resolve_base_uri", [expressions_1.Constants.__DIR__], { assignTo: baseUriVar }).closePHP();
|
|
81
82
|
for (let [filename, { preload, fetchpriority, crossorigin }] of preloadedAssets) {
|
|
82
83
|
if (!preload) {
|
|
83
84
|
continue;
|
|
@@ -116,11 +117,11 @@ class SpecialAssetHandlingPlugin extends AbstractBiPhasicGroupPlugin_1.AbstractB
|
|
|
116
117
|
attributes.push(["crossorigin", crossorigin]);
|
|
117
118
|
}
|
|
118
119
|
const dynamicAttrs = attributes.map(([k, v]) => v ? `${k}="${v}"` : k).join(" ");
|
|
119
|
-
writer.append(`<link rel="preload" href="<?= ${
|
|
120
|
+
writer.append(`<link rel="preload" href="<?= ${expressions_1.Expr.call("esc_url", [expressions_1.Op.concat(baseUriVar, filename)])} ?>" ${dynamicAttrs}>`);
|
|
120
121
|
}
|
|
121
122
|
writer.openPHP();
|
|
122
123
|
});
|
|
123
|
-
|
|
124
|
+
(0, shared_1.emitPHPWriterAsAsset)(writer, compilation, outputFile);
|
|
124
125
|
}
|
|
125
126
|
attachUniquePhase(compilation) {
|
|
126
127
|
compilation.hooks.processAssets.tapPromise({ name: this.constructor.name, stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_REPORT }, async () => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { PHPWriter } from "@plaudit/php-writer";
|
|
1
2
|
import { AbstractBiPhasicGroupPlugin } from "./AbstractBiPhasicGroupPlugin";
|
|
2
3
|
import type { VerifiedPlauditWordpressWebpackConfig } from "../utils/common-config-helpers";
|
|
3
|
-
import { PHPWriter } from "../utils/php-writer";
|
|
4
4
|
import { PseudoSemaphore } from "../utils/pseduo-semaphore";
|
|
5
5
|
import { Compilation } from "webpack";
|
|
6
6
|
type LoaderInfo = {
|
|
@@ -5,10 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.UnifiedLoaderGenerator = void 0;
|
|
7
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const php_writer_1 = require("@plaudit/php-writer");
|
|
8
9
|
const AbstractBiPhasicGroupPlugin_1 = require("./AbstractBiPhasicGroupPlugin");
|
|
9
10
|
const shared_1 = require("../shared");
|
|
10
|
-
const json_to_php_but_with____injection_1 = __importDefault(require("../utils/json-to-php-but-with-__-injection"));
|
|
11
|
-
const php_writer_1 = require("../utils/php-writer");
|
|
12
11
|
const pseduo_semaphore_1 = require("../utils/pseduo-semaphore");
|
|
13
12
|
const webpack_1 = require("webpack");
|
|
14
13
|
class UnifiedLoaderGenerator extends AbstractBiPhasicGroupPlugin_1.AbstractBiPhasicGroupPlugin {
|
|
@@ -53,7 +52,7 @@ class UnifiedLoaderGenerator extends AbstractBiPhasicGroupPlugin_1.AbstractBiPha
|
|
|
53
52
|
continue;
|
|
54
53
|
}
|
|
55
54
|
const parsedPath = node_path_1.default.parse(file);
|
|
56
|
-
const contents = `<?php return ${(0,
|
|
55
|
+
const contents = `<?php return ${(0, php_writer_1.json2php)(assetData)};\n`;
|
|
57
56
|
compilation.emitAsset(node_path_1.default.join(parsedPath.dir, parsedPath.name) + ".asset.php", new webpack_1.sources.RawSource(contents), { size: Buffer.byteLength(contents) });
|
|
58
57
|
}
|
|
59
58
|
if (this.config.useUnifiedLoader) {
|
|
@@ -65,7 +64,7 @@ class UnifiedLoaderGenerator extends AbstractBiPhasicGroupPlugin_1.AbstractBiPha
|
|
|
65
64
|
for (const { action } of groupedLoaderInfo) {
|
|
66
65
|
action(writer);
|
|
67
66
|
}
|
|
68
|
-
|
|
67
|
+
(0, shared_1.emitPHPWriterAsAsset)(writer, compilation, "unified-loader.php");
|
|
69
68
|
});
|
|
70
69
|
}
|
|
71
70
|
});
|
package/build/shared.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { PHPWriter } from "
|
|
1
|
+
import { PHPWriter } from "@plaudit/php-writer";
|
|
2
2
|
import type { Options as PostcssFunctionsOptions } from "postcss-functions";
|
|
3
|
-
import { Compilation, type Configuration, WebpackError } from "webpack";
|
|
3
|
+
import { AssetInfo, Compilation, type Configuration, WebpackError } from "webpack";
|
|
4
4
|
export type ParsedAssetsJson = Record<string, {
|
|
5
5
|
dependencies: string[];
|
|
6
6
|
version: string;
|
|
@@ -146,4 +146,5 @@ export declare function newWebpackErrorForFile(error: string | ConstructorParame
|
|
|
146
146
|
*/
|
|
147
147
|
export declare function emitResolveBaseUriFunction(writer: PHPWriter): void;
|
|
148
148
|
export declare function getAssetsJson(compilation: Compilation): ParsedAssetsJson;
|
|
149
|
+
export declare function emitPHPWriterAsAsset(writer: PHPWriter, compilation: Compilation, file: string, assetInfo?: AssetInfo): void;
|
|
149
150
|
export {};
|
package/build/shared.js
CHANGED
|
@@ -24,9 +24,10 @@ exports.parseEnvFile = parseEnvFile;
|
|
|
24
24
|
exports.newWebpackErrorForFile = newWebpackErrorForFile;
|
|
25
25
|
exports.emitResolveBaseUriFunction = emitResolveBaseUriFunction;
|
|
26
26
|
exports.getAssetsJson = getAssetsJson;
|
|
27
|
+
exports.emitPHPWriterAsAsset = emitPHPWriterAsAsset;
|
|
27
28
|
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
28
|
-
const
|
|
29
|
-
const
|
|
29
|
+
const php_writer_1 = require("@plaudit/php-writer");
|
|
30
|
+
const expressions_1 = require("@plaudit/php-writer/expressions");
|
|
30
31
|
const webpack_1 = require("webpack");
|
|
31
32
|
function isParsedAssetsJson(thing) {
|
|
32
33
|
if (!thing || typeof thing !== 'object') {
|
|
@@ -73,7 +74,7 @@ function convertUsageLocationsHandleToEmittableHandle(handle, generatedHandle) {
|
|
|
73
74
|
return emittableHandle.replaceAll("{basename}", generatedHandle);
|
|
74
75
|
}
|
|
75
76
|
function makeEmittableConfigPHP(data, asFullFile, parentIndent = "") {
|
|
76
|
-
const prettyPrintedMetadata =
|
|
77
|
+
const prettyPrintedMetadata = php_writer_1.json2php.make({ indent: "\t", linebreak: "\n", shortArraySyntax: true })(data, parentIndent)
|
|
77
78
|
.replaceAll(/(\n\t*)\[\s+],/gs, "$1[],")
|
|
78
79
|
.replaceAll(/\[\n\t+([^\n]+)\n\t+]/gs, (_, inner) => `[${inner.trim()}]`)
|
|
79
80
|
.replaceAll(/'rest' => \[\n\t+(\[(?:'[^']+')?]),\n\t+('[^']+')(?:,\n\t+(\[[^\n]+]))?\n\t+]/gs, (_, deps, hash, args) => `'rest' => [${[deps, hash, args].filter(value => !!value).join(", ")}]`);
|
|
@@ -143,24 +144,24 @@ function newWebpackErrorForFile(error, file) {
|
|
|
143
144
|
* The primary benefit of emitting a function instead of baking its contents into each function that uses it is that it allows us to avoid recomputing the base uri multiple times
|
|
144
145
|
*/
|
|
145
146
|
function emitResolveBaseUriFunction(writer) {
|
|
146
|
-
const dir = new
|
|
147
|
-
const baseUris = new
|
|
148
|
-
const assignmentTarget = new
|
|
149
|
-
const path = new
|
|
147
|
+
const dir = new expressions_1.Var("dir");
|
|
148
|
+
const baseUris = new expressions_1.Var("base_uris");
|
|
149
|
+
const assignmentTarget = new expressions_1.ArrayAccess(baseUris, dir);
|
|
150
|
+
const path = new expressions_1.Var("path");
|
|
150
151
|
writer.function("plaudit_webpack_extensions__resolve_base_uri", [dir], writer => {
|
|
151
152
|
writer
|
|
152
153
|
.static(baseUris, { initializer: [] })
|
|
153
|
-
.if(
|
|
154
|
+
.if(expressions_1.Expr.isset(assignmentTarget))
|
|
154
155
|
.return(assignmentTarget)
|
|
155
|
-
.elseIf(
|
|
156
|
-
.assign(path,
|
|
157
|
-
.elseIf(
|
|
158
|
-
.assign(path,
|
|
156
|
+
.elseIf(expressions_1.Expr.call("str_starts_with", [dir, expressions_1.Constants.ABSPATH]))
|
|
157
|
+
.assign(path, expressions_1.Expr.call("ltrim", [expressions_1.Expr.call("substr", [dir, expressions_1.Expr.call("strlen", [expressions_1.Constants.ABSPATH])]), "/"]))
|
|
158
|
+
.elseIf(expressions_1.Expr.call("str_starts_with", [dir, "/workspace/website"]))
|
|
159
|
+
.assign(path, expressions_1.Expr.call("ltrim", [expressions_1.Expr.call("substr", [dir, 18]), "/"]))
|
|
159
160
|
.else()
|
|
160
161
|
.call("error_log", ["UNABLE TO FIGURE OUT WHAT THE RELATIVE PATH TO THE BUILT FILES DIRECTORY SHOULD BE"])
|
|
161
162
|
.assign(path, "")
|
|
162
163
|
.endIf()
|
|
163
|
-
.call("trailingslashit", [
|
|
164
|
+
.call("trailingslashit", [expressions_1.Expr.call("home_url", [path])], { return: true, assignTo: assignmentTarget });
|
|
164
165
|
}, { returnType: "string", includeExistenceCheck: true });
|
|
165
166
|
}
|
|
166
167
|
function getAssetsJson(compilation) {
|
|
@@ -178,3 +179,7 @@ function getAssetsJson(compilation) {
|
|
|
178
179
|
compilation.updateAsset("assets.json", s => s, a => ({ ...a, "parsed-assets-json": parsedAssetJson }));
|
|
179
180
|
return parsedAssetJson;
|
|
180
181
|
}
|
|
182
|
+
function emitPHPWriterAsAsset(writer, compilation, file, assetInfo) {
|
|
183
|
+
const contents = writer.toString() + "\n";
|
|
184
|
+
compilation[file in compilation.assets ? 'updateAsset' : 'emitAsset'](file, new webpack_1.sources.RawSource(contents), { size: Buffer.byteLength(contents), ...assetInfo });
|
|
185
|
+
}
|
|
@@ -172,7 +172,7 @@ function injectSupportForInliningSVGsAsStrings(rules) {
|
|
|
172
172
|
});
|
|
173
173
|
}
|
|
174
174
|
function buildVerifiedConfig(config) {
|
|
175
|
-
const { standaloneBlocks = false, stats = 'errors-warnings', variables: rawVariables, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true', postcss = {}, externals, assumeGlobalizedPlauditLibraries = true, processTranslationConfigs = true, combineAssetMetadata = true, useWebpackResourceFiltering = true, outputDir = "", plainEntrypointsVersion = 1, srcDir = "", useUnifiedLoader = false, includePostInitFallback =
|
|
175
|
+
const { standaloneBlocks = false, stats = 'errors-warnings', variables: rawVariables, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true', postcss = {}, externals, assumeGlobalizedPlauditLibraries = true, processTranslationConfigs = true, combineAssetMetadata = true, useWebpackResourceFiltering = true, outputDir = "", plainEntrypointsVersion = 1, srcDir = "", useUnifiedLoader = false, includePostInitFallback = false } = config;
|
|
176
176
|
let srcPrefixes;
|
|
177
177
|
const trailingSlashedSrcDir = srcDir && !srcDir.endsWith("/") ? srcDir + "/" : srcDir;
|
|
178
178
|
if (config.srcPrefixes === undefined) {
|
|
@@ -480,6 +480,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources, canClean
|
|
|
480
480
|
const { bundleAnalyzer, assumeGlobalizedPlauditLibraries, externalize } = currentBatch[0].dest;
|
|
481
481
|
const { plugins, removeEmptyScriptsPlugin, makeAdditionalDependencyInjectorPlugin } = buildCommonPluginConfig(webpackConfig, config, bundleAnalyzer, assumeGlobalizedPlauditLibraries, externalize);
|
|
482
482
|
handleDisablingTSCheckerIfNecessary(currentBatch.flatMap(a => a.srcRoots), scriptExtension, plugins);
|
|
483
|
+
const contextPath = webpackConfig.context ?? process.cwd();
|
|
483
484
|
for (const { dest, src, srcIsDirectory, srcRoot } of currentBatch) {
|
|
484
485
|
let plugin;
|
|
485
486
|
const sourceType = (0, shared_1.determineCurrentSourceType)(dest, srcIsDirectory);
|
|
@@ -487,7 +488,7 @@ function processIndividualWebpackConfig(config, webpackConfig, sources, canClean
|
|
|
487
488
|
if (!srcIsDirectory) {
|
|
488
489
|
continue;
|
|
489
490
|
}
|
|
490
|
-
plugin = new PlainEntrypointsStyleBlockJSONPlugin_1.PlainEntrypointsStyleBlockJSONPlugin(config, dest
|
|
491
|
+
plugin = new PlainEntrypointsStyleBlockJSONPlugin_1.PlainEntrypointsStyleBlockJSONPlugin(config, dest, removeEmptyScriptsPlugin, contextPath, (0, common_config_helpers_1.resolveEntryFromDirectory)(commonConfig, srcRoot, dest));
|
|
491
492
|
plugins.push(plugin);
|
|
492
493
|
if (config.processTranslationConfigs && !processingModules) {
|
|
493
494
|
plugins.push(new WPMLConfigBuilder_1.WPMLConfigBuilderPlugin(dest.destination, plugin));
|
|
@@ -510,8 +511,8 @@ function processIndividualWebpackConfig(config, webpackConfig, sources, canClean
|
|
|
510
511
|
return Object.fromEntries(rawEntrypoints);
|
|
511
512
|
};
|
|
512
513
|
plugin = config.extensionsVersion > 1
|
|
513
|
-
? new ExtensionsConfigFileGeneratorPlugin_1.ExtensionsConfigFileGeneratorPlugin(config, srcRoot, dest,
|
|
514
|
-
: new ExtensionsConfigFileGeneratorPluginV1_1.ExtensionsConfigFileGeneratorPluginV1(config, dest.destination,
|
|
514
|
+
? new ExtensionsConfigFileGeneratorPlugin_1.ExtensionsConfigFileGeneratorPlugin(config, srcRoot, dest, contextPath, entry)
|
|
515
|
+
: new ExtensionsConfigFileGeneratorPluginV1_1.ExtensionsConfigFileGeneratorPluginV1(config, dest.destination, contextPath, entry);
|
|
515
516
|
plugins.push(plugin);
|
|
516
517
|
}
|
|
517
518
|
else if (sourceType === "plain" /* SourceType.plain */) {
|
|
@@ -526,14 +527,14 @@ function processIndividualWebpackConfig(config, webpackConfig, sources, canClean
|
|
|
526
527
|
const entry = srcIsDirectory // This being true implies that srcRoot is not an array
|
|
527
528
|
? (0, common_config_helpers_1.resolveEntryFromDirectory)(commonConfig, srcRoot, dest)
|
|
528
529
|
: () => ({ [baseDest]: { import: Array.isArray(srcRoot) ? srcRoot : [srcRoot], plauditMetadata: { purpose: "plain-entrypoint", dest } } });
|
|
529
|
-
plugin = new PlainEntrypointsConfigFileGeneratorPlugin_1.PlainEntrypointsConfigFileGeneratorPlugin(config, process.cwd(), config.outputDir, dest.locations, config.useUnifiedLoader,
|
|
530
|
+
plugin = new PlainEntrypointsConfigFileGeneratorPlugin_1.PlainEntrypointsConfigFileGeneratorPlugin(config, process.cwd(), config.outputDir, dest.locations, config.useUnifiedLoader, contextPath, entry);
|
|
530
531
|
}
|
|
531
532
|
else {
|
|
532
533
|
if (srcIsDirectory) {
|
|
533
534
|
console.error("Using directory mode with legacy plain entrypoint handling is not supported");
|
|
534
535
|
continue;
|
|
535
536
|
}
|
|
536
|
-
plugin = new EnhancedDynamicEntryPlugin_1.EnhancedDynamicEntryPlugin(config,
|
|
537
|
+
plugin = new EnhancedDynamicEntryPlugin_1.EnhancedDynamicEntryPlugin(config, contextPath, () => ({ [baseDest]: { import: Array.isArray(srcRoot) ? srcRoot : [srcRoot], plauditMetadata: { purpose: "plain-entrypoint", dest } } }));
|
|
537
538
|
}
|
|
538
539
|
plugins.push(plugin);
|
|
539
540
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plaudit/webpack-extensions",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.72.0",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
5
5
|
"files": [
|
|
6
6
|
"/build"
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@plaudit/gutenberg-api-extensions": "^2.77.2",
|
|
24
24
|
"@types/browser-sync-webpack-plugin": "^2.2.5",
|
|
25
|
-
"@types/node": "^24.
|
|
25
|
+
"@types/node": "^24.7.2",
|
|
26
26
|
"@types/postcss-functions": "^4.0.4",
|
|
27
|
-
"@types/tapable": "^2.
|
|
27
|
+
"@types/tapable": "^2.3.0",
|
|
28
28
|
"@types/webpack": "^5.28.5",
|
|
29
29
|
"@types/webpack-sources": "^3.2.3",
|
|
30
30
|
"postcss-load-config": "^4.0.2",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"webpack-bundle-analyzer": "^4.10.2"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
+
"@plaudit/php-writer": "^1.0.0",
|
|
37
38
|
"@plaudit/postcss-color-function": "^5.0.0",
|
|
38
39
|
"@plaudit/postcss-legacy-shorthand": "^1.0.0",
|
|
39
40
|
"@plaudit/postcss-silent-extend": "^3.0.0",
|
|
@@ -63,7 +64,7 @@
|
|
|
63
64
|
"postcss-url": "^10.1.3",
|
|
64
65
|
"react": "^18.3.1",
|
|
65
66
|
"react-dom": "^18.3.1",
|
|
66
|
-
"webpack": "^5.102.
|
|
67
|
+
"webpack": "^5.102.1",
|
|
67
68
|
"webpack-remove-empty-scripts": "^1.1.1",
|
|
68
69
|
"xml-formatter": "^3.6.7"
|
|
69
70
|
},
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
type MakeProps = {
|
|
2
|
-
linebreak?: string;
|
|
3
|
-
indent?: string;
|
|
4
|
-
shortArraySyntax?: boolean;
|
|
5
|
-
stripSpaces?: boolean;
|
|
6
|
-
};
|
|
7
|
-
declare const make: (props?: MakeProps) => (obj: unknown, parentIndent?: string) => string;
|
|
8
|
-
declare const json2php: ReturnType<typeof make> & {
|
|
9
|
-
make: typeof make;
|
|
10
|
-
};
|
|
11
|
-
export default json2php;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const acceptedObjectTypes = ['[object Array]', '[object Object]'];
|
|
4
|
-
const make = (props = {}) => {
|
|
5
|
-
const linebreak = props.linebreak ?? '';
|
|
6
|
-
const indent = props.indent ?? '';
|
|
7
|
-
const stripSpaces = props.stripSpaces ?? false;
|
|
8
|
-
const shortArraySyntax = props.shortArraySyntax ?? true;
|
|
9
|
-
const arrOpen = shortArraySyntax ? '[' : 'array(';
|
|
10
|
-
const arrClose = shortArraySyntax ? ']' : ')';
|
|
11
|
-
const arrow = stripSpaces ? '=>' : ' => ';
|
|
12
|
-
function transformInner(obj, parentIndent, state) {
|
|
13
|
-
switch (typeof obj) {
|
|
14
|
-
case 'undefined':
|
|
15
|
-
return 'null';
|
|
16
|
-
case 'bigint':
|
|
17
|
-
case 'number':
|
|
18
|
-
case 'boolean':
|
|
19
|
-
return obj.toString();
|
|
20
|
-
case 'string':
|
|
21
|
-
const res = obj.replace("\\", '\\\\').replaceAll("'", "\\'");
|
|
22
|
-
if (!/\s*/.test(res) && !state.isKey && state.path.length === 4 && state.path[1] === 'attributes' && state.path[3] === 'default' && state.parent?.['translatable']) {
|
|
23
|
-
return `__('${res}')`;
|
|
24
|
-
}
|
|
25
|
-
return `'${res}'`;
|
|
26
|
-
case 'object':
|
|
27
|
-
if (obj === null)
|
|
28
|
-
return 'null';
|
|
29
|
-
const objType = Object.prototype.toString.call(obj);
|
|
30
|
-
if (objType === '[object Date]')
|
|
31
|
-
return obj.toISOString();
|
|
32
|
-
if (!acceptedObjectTypes.includes(objType))
|
|
33
|
-
return 'null';
|
|
34
|
-
const nestIndent = parentIndent + indent;
|
|
35
|
-
const items = Array.isArray(obj)
|
|
36
|
-
? obj.map((item, i) => transformInner(item, nestIndent, { path: [...state.path, i], parent: obj }))
|
|
37
|
-
: Object.entries(obj).map(([key, value]) => {
|
|
38
|
-
return transformInner(key, nestIndent, { path: [...state.path, key], parent: obj, isKey: true })
|
|
39
|
-
+ arrow + transformInner(value, nestIndent, { path: [...state.path, key], parent: obj });
|
|
40
|
-
});
|
|
41
|
-
const itemStr = items.join(`,${linebreak === '' && !stripSpaces ? ' ' : linebreak + nestIndent}`);
|
|
42
|
-
return `${arrOpen}${linebreak + nestIndent}${itemStr}${linebreak + parentIndent}${arrClose}`;
|
|
43
|
-
default:
|
|
44
|
-
return 'null';
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return function transform(obj, parentIndent = '') {
|
|
48
|
-
return transformInner(obj, parentIndent, { path: [] });
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
const json2php = make();
|
|
52
|
-
json2php.make = make;
|
|
53
|
-
exports.default = json2php;
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.phpSerialize = phpSerialize;
|
|
4
|
-
function phpSerialize(value, config) {
|
|
5
|
-
switch (typeof value) {
|
|
6
|
-
case "undefined":
|
|
7
|
-
return "N;";
|
|
8
|
-
case "object":
|
|
9
|
-
if (value === null) {
|
|
10
|
-
return "N;"; // Yep. Null is an "object" in JavaScript
|
|
11
|
-
}
|
|
12
|
-
else if (Array.isArray(value)) {
|
|
13
|
-
let res = `a:${value.length}:{`;
|
|
14
|
-
for (let i = 0; i < value.length; i++) {
|
|
15
|
-
res += `i:${i};${phpSerialize(value[i], config)}`;
|
|
16
|
-
}
|
|
17
|
-
return (res.endsWith('};') ? res.substring(0, res.length - 1) : res) + "}";
|
|
18
|
-
}
|
|
19
|
-
return entriesInArrayAndObjectFormat(config?.objectsAsArrays ?? true ? 'a' : 'O:8:"stdClass"', Object.entries(value), config);
|
|
20
|
-
case "boolean":
|
|
21
|
-
return `b:${value ? 1 : 0};`;
|
|
22
|
-
case "number":
|
|
23
|
-
return Number.isInteger(value) ? serializeInteger(value) : `d:${value};`;
|
|
24
|
-
case "string":
|
|
25
|
-
return `s:${Buffer.byteLength(value, 'utf8')}:"${value}";`;
|
|
26
|
-
case "function":
|
|
27
|
-
throw new Error(`We are unable to serialize functions into a form readable by PHP.`);
|
|
28
|
-
case "symbol":
|
|
29
|
-
value = value.toString();
|
|
30
|
-
return `s:${Buffer.byteLength(value, 'utf8')}:"${value}";`;
|
|
31
|
-
case "bigint":
|
|
32
|
-
return serializeInteger(value);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function serializeInteger(value) {
|
|
36
|
-
const str = value.toString();
|
|
37
|
-
if (str.includes("e") || str.includes("E")) {
|
|
38
|
-
return `d:${str.replace(/e/i, ".0e")};`;
|
|
39
|
-
}
|
|
40
|
-
return `i:${str};`;
|
|
41
|
-
}
|
|
42
|
-
function entriesInArrayAndObjectFormat(prefix, entries, config) {
|
|
43
|
-
const excludedKeys = config?.excludedKeys ?? [];
|
|
44
|
-
let res = "";
|
|
45
|
-
let count = 0;
|
|
46
|
-
for (const [k, v] of entries) {
|
|
47
|
-
if (!excludedKeys.includes(k)) {
|
|
48
|
-
count++;
|
|
49
|
-
res += `${phpSerialize(k, config)}${phpSerialize(v, config)}`;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return `${prefix}:${count}:{` + (res.endsWith('};') ? res.substring(0, res.length - 1) : res) + "}";
|
|
53
|
-
}
|