@plaudit/webpack-extensions 0.0.9
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/README.md +13 -0
- package/build/wordpress-scripts-wrapper/AdditionalDependencyInjectorPlugin.d.ts +6 -0
- package/build/wordpress-scripts-wrapper/AdditionalDependencyInjectorPlugin.js +52 -0
- package/build/wordpress-scripts-wrapper/BlockJSONStyleRemappingPlugin.d.ts +4 -0
- package/build/wordpress-scripts-wrapper/BlockJSONStyleRemappingPlugin.js +60 -0
- package/build/wordpress-scripts-wrapper/wordpressWebpackEntrypointWrapper.d.ts +2 -0
- package/build/wordpress-scripts-wrapper/wordpressWebpackEntrypointWrapper.js +72 -0
- package/build/wordpress-scripts-wrapper.d.ts +1 -0
- package/build/wordpress-scripts-wrapper.js +8 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Development
|
|
2
|
+
To test locally, either set up yarn to pull from this folder or (ideally) build and copy the resulting built files into another project for testing purposes.
|
|
3
|
+
|
|
4
|
+
# To Deploy
|
|
5
|
+
If you haven't
|
|
6
|
+
```
|
|
7
|
+
yarn global add genversion
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
To deploy
|
|
11
|
+
```
|
|
12
|
+
./publish.sh y
|
|
13
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Compiler, type WebpackPluginInstance } from "webpack";
|
|
2
|
+
export default class AdditionalDependencyInjectorPlugin implements WebpackPluginInstance {
|
|
3
|
+
private readonly filesWithAdditionalDependencies;
|
|
4
|
+
constructor(filesWithAdditionalDependencies?: Map<string, string[]>);
|
|
5
|
+
apply(compiler: Compiler): void;
|
|
6
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
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 webpack_sources_1 = require("webpack-sources");
|
|
10
|
+
class AdditionalDependencyInjectorPlugin {
|
|
11
|
+
filesWithAdditionalDependencies;
|
|
12
|
+
constructor(filesWithAdditionalDependencies = new Map()) {
|
|
13
|
+
this.filesWithAdditionalDependencies = filesWithAdditionalDependencies;
|
|
14
|
+
}
|
|
15
|
+
apply(compiler) {
|
|
16
|
+
compiler.hooks.thisCompilation.tap("AdditionalDependencyInjectorPlugin", compilation => {
|
|
17
|
+
compilation.hooks.processAssets.tap({
|
|
18
|
+
name: "AdditionalDependencyInjectorPlugin_ProcessAssets",
|
|
19
|
+
stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ANALYSE
|
|
20
|
+
}, compilationAssets => {
|
|
21
|
+
const srcDir = process.env['WP_SRC_DIRECTORY'] || 'src';
|
|
22
|
+
const srcRoot = node_path_1.default.join(process.cwd(), srcDir);
|
|
23
|
+
Object.entries(compilationAssets).map(([name, asset]) => {
|
|
24
|
+
if (name.endsWith(".asset.php") && asset instanceof webpack_sources_1.RawSource) {
|
|
25
|
+
const extensionlessName = name.substring(0, name.length - 10);
|
|
26
|
+
const additionalDependencies = [];
|
|
27
|
+
const possibleExtensions = ['ts', 'tsx', 'js', 'jsx'];
|
|
28
|
+
for (const possibleExtension of possibleExtensions) {
|
|
29
|
+
const possibleFile = node_path_1.default.join(srcRoot, `${extensionlessName}.${possibleExtension}`);
|
|
30
|
+
if (node_fs_1.default.existsSync(possibleFile)) {
|
|
31
|
+
const firstLine = node_fs_1.default.readFileSync(possibleFile, 'utf8').trim().split(/\r?\n/)[0];
|
|
32
|
+
if (firstLine.startsWith("//ADDITIONAL_DEPENDENCIES:")) {
|
|
33
|
+
additionalDependencies.push(...firstLine.substring(26).trim().split(','));
|
|
34
|
+
}
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (this.filesWithAdditionalDependencies.has(extensionlessName)) {
|
|
39
|
+
additionalDependencies.push(...this.filesWithAdditionalDependencies.get(extensionlessName));
|
|
40
|
+
}
|
|
41
|
+
const seen = new Set();
|
|
42
|
+
const additionalDeps = additionalDependencies.filter(dep => !seen.has(dep) && seen.add(dep)).sort().map(v => `'${v}'`).join(', ');
|
|
43
|
+
if (additionalDeps) {
|
|
44
|
+
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}`)));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.default = AdditionalDependencyInjectorPlugin;
|
|
@@ -0,0 +1,60 @@
|
|
|
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_path_1 = __importDefault(require("node:path"));
|
|
7
|
+
const webpack_sources_1 = require("webpack-sources");
|
|
8
|
+
class BlockJSONStyleRemappingPlugin {
|
|
9
|
+
apply(compiler) {
|
|
10
|
+
compiler.hooks.compilation.tap("BlockJSONStyleRemappingPlugin", compilation => {
|
|
11
|
+
compilation.hooks.processAssets.tap("BlockJSONStyleRemappingPlugin_ProcessAssets", 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
|
+
const toNumericChunkId = (chunk) => typeof chunk !== 'number' ? parseInt(chunk) : chunk;
|
|
23
|
+
const assetSourceFiles = new Map(stats.assets?.map(asset => [asset.name, asset.info?.sourceFilename])
|
|
24
|
+
.filter((v) => v[0] !== undefined && v[1] !== undefined) ?? []);
|
|
25
|
+
const singleFileChunkToOutputName = new Map(stats.assets?.filter((asset) => asset.chunks?.length === 1)
|
|
26
|
+
.map(asset => [toNumericChunkId(asset.chunks[0]), asset.name]) ?? []);
|
|
27
|
+
const singleFileInputToOutputName = new Map(stats.modules?.map(module => {
|
|
28
|
+
if (module.nameForCondition !== undefined && module.chunks?.length === 1) {
|
|
29
|
+
const output = singleFileChunkToOutputName.get(toNumericChunkId(module.chunks[0]));
|
|
30
|
+
return output !== undefined ? [module.nameForCondition, output] : undefined;
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}).filter((v) => v !== undefined) ?? []);
|
|
34
|
+
const styleKeys = ["editorStyle", "style", "viewStyle"];
|
|
35
|
+
Object.entries(compilationAssets).map(([name, asset]) => {
|
|
36
|
+
if (name.endsWith("block.json") && asset instanceof webpack_sources_1.RawSource) {
|
|
37
|
+
const dirname = node_path_1.default.dirname(assetSourceFiles.get(name) ?? name);
|
|
38
|
+
const json = JSON.parse(asset.source());
|
|
39
|
+
for (const styleKey of styleKeys) {
|
|
40
|
+
if (styleKey in json) {
|
|
41
|
+
const style = json[styleKey];
|
|
42
|
+
if (typeof style === 'string' && style.startsWith('file:')) {
|
|
43
|
+
const styleInputPath = node_path_1.default.normalize(node_path_1.default.join(compiler.context, dirname, style.substring(5)));
|
|
44
|
+
const styleOutputPath = singleFileInputToOutputName.get(styleInputPath);
|
|
45
|
+
if (styleOutputPath !== undefined) {
|
|
46
|
+
const prefix = style.startsWith("./", 5) ? "./" : "";
|
|
47
|
+
const relativePath = node_path_1.default.relative(node_path_1.default.dirname(name), styleOutputPath);
|
|
48
|
+
json[styleKey] = `file:${prefix}${relativePath}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
compilation.updateAsset(name, new webpack_sources_1.RawSource(JSON.stringify(json, undefined, " ")));
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.default = BlockJSONStyleRemappingPlugin;
|
|
@@ -0,0 +1,72 @@
|
|
|
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 AdditionalDependencyInjectorPlugin_1 = __importDefault(require("./AdditionalDependencyInjectorPlugin"));
|
|
9
|
+
const BlockJSONStyleRemappingPlugin_1 = __importDefault(require("./BlockJSONStyleRemappingPlugin"));
|
|
10
|
+
const copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
|
|
11
|
+
const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
|
|
12
|
+
const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-empty-scripts"));
|
|
13
|
+
function addEntryPointWithFilter(entrypoints, entrypoint, mapper = (entrypoint) => entrypoint) {
|
|
14
|
+
if (Array.isArray(entrypoint)) {
|
|
15
|
+
entrypoints.push(...entrypoint.map(mapper));
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
entrypoints.push(mapper(entrypoint));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function default_1(config) {
|
|
22
|
+
(config.plugins ?? (config.plugins = [])).push(new fork_ts_checker_webpack_plugin_1.default({
|
|
23
|
+
typescript: {
|
|
24
|
+
diagnosticOptions: {
|
|
25
|
+
semantic: true,
|
|
26
|
+
syntactic: true,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
}), new webpack_remove_empty_scripts_1.default({ stage: webpack_remove_empty_scripts_1.default.STAGE_AFTER_PROCESS_PLUGINS }), new BlockJSONStyleRemappingPlugin_1.default(), new AdditionalDependencyInjectorPlugin_1.default(), new copy_webpack_plugin_1.default({ patterns: [{ from: '**/*.(twig|svg)', context: process.env['WP_SRC_DIRECTORY'] || 'src', noErrorOnMissing: true }] }));
|
|
30
|
+
const baseEntry = config.entry;
|
|
31
|
+
config.entry = () => {
|
|
32
|
+
const srcDir = process.env['WP_SRC_DIRECTORY'] || 'src';
|
|
33
|
+
const srcRoot = node_path_1.default.join(process.cwd(), srcDir);
|
|
34
|
+
const entrypoints = node_fs_1.default.readdirSync(srcRoot, 'utf8')
|
|
35
|
+
.map(dir => node_path_1.default.join(srcRoot, dir))
|
|
36
|
+
.filter(dir => node_fs_1.default.statSync(dir).isDirectory())
|
|
37
|
+
.flatMap(dir => {
|
|
38
|
+
const entrypoints = [];
|
|
39
|
+
try {
|
|
40
|
+
const blockJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'block.json'), 'utf8'));
|
|
41
|
+
for (const key of ["editorStyle", "style", "editorScript", "viewScript", "script"]) {
|
|
42
|
+
if (key in blockJSON) {
|
|
43
|
+
addEntryPointWithFilter(entrypoints, blockJSON[key], ep => ep.startsWith("file:") ? ep.substring(5) : ep);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
try {
|
|
49
|
+
const packageJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'package.json'), 'utf8'));
|
|
50
|
+
addEntryPointWithFilter(entrypoints, packageJSON['main']);
|
|
51
|
+
addEntryPointWithFilter(entrypoints, packageJSON['style']);
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
// This just means that the directory doesn't contain any declared entrypoints.
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return entrypoints.length === 0 ? undefined : entrypoints.map(entrypoint => node_path_1.default.join(dir, entrypoint));
|
|
58
|
+
})
|
|
59
|
+
.filter((entrypoint) => typeof entrypoint === 'string' && node_fs_1.default.existsSync(entrypoint));
|
|
60
|
+
const convertEntrypointsToObject = () => entrypoints.reduce((entry, entrypoint) => {
|
|
61
|
+
const parsedEntrypoint = node_path_1.default.parse(entrypoint);
|
|
62
|
+
return {
|
|
63
|
+
...entry,
|
|
64
|
+
[node_path_1.default.join(node_path_1.default.basename(parsedEntrypoint.dir), parsedEntrypoint.name)]: { import: entrypoint }
|
|
65
|
+
};
|
|
66
|
+
}, {});
|
|
67
|
+
const firstPassBaseEntries = typeof baseEntry === 'function' ? baseEntry() : baseEntry;
|
|
68
|
+
return firstPassBaseEntries instanceof Promise ? firstPassBaseEntries.then(convertEntrypointsToObject) : convertEntrypointsToObject();
|
|
69
|
+
};
|
|
70
|
+
return config;
|
|
71
|
+
}
|
|
72
|
+
exports.default = default_1;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from "./wordpress-scripts-wrapper/wordpressWebpackEntrypointWrapper";
|
|
@@ -0,0 +1,8 @@
|
|
|
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
|
+
exports.default = void 0;
|
|
7
|
+
var wordpressWebpackEntrypointWrapper_1 = require("./wordpress-scripts-wrapper/wordpressWebpackEntrypointWrapper");
|
|
8
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(wordpressWebpackEntrypointWrapper_1).default; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@plaudit/webpack-extensions",
|
|
3
|
+
"version": "0.0.9",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"prepublishOnly": "rm -rf build && mkdir build && tsc",
|
|
6
|
+
"build": "tsc",
|
|
7
|
+
"watch": "tsc -w"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"/build"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
"./wordpress-scripts-wrapper": "./build/wordpress-scripts-wrapper.js"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/node": "^18.15.11",
|
|
17
|
+
"@types/webpack-sources": "^3.2.0",
|
|
18
|
+
"copy-webpack-plugin": "^11.0.0",
|
|
19
|
+
"fork-ts-checker-webpack-plugin": "^8.0.0",
|
|
20
|
+
"typescript": "^5.0.4",
|
|
21
|
+
"webpack": "^5.79.0",
|
|
22
|
+
"webpack-remove-empty-scripts": "^1.0.3"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@wordpress/dependency-extraction-webpack-plugin": "^4.13.0",
|
|
28
|
+
"clean-webpack-plugin": "^4.0.0",
|
|
29
|
+
"copy-webpack-plugin": "^11.0.0",
|
|
30
|
+
"eslint-plugin-jsdoc": "^39.6.4",
|
|
31
|
+
"fork-ts-checker-webpack-plugin": "^8.0.0",
|
|
32
|
+
"webpack": "^5.79.0",
|
|
33
|
+
"webpack-remove-empty-scripts": "^1.0.3",
|
|
34
|
+
"webpack-sources": "^3.2.3"
|
|
35
|
+
},
|
|
36
|
+
"resolutions": {
|
|
37
|
+
"eslint-plugin-jsdoc": "^39.6.4",
|
|
38
|
+
"clean-webpack-plugin": "^4.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|