@plaudit/webpack-extensions 1.2.1 → 1.3.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.
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const node_fs_1 = __importDefault(require("node:fs"));
7
- const node_path_1 = __importDefault(require("node:path"));
8
7
  const webpack_1 = require("webpack");
9
8
  const webpack_sources_1 = require("webpack-sources");
10
9
  class AdditionalDependencyInjectorPlugin {
@@ -14,26 +13,33 @@ class AdditionalDependencyInjectorPlugin {
14
13
  name: "AdditionalDependencyInjectorPlugin_ProcessAssets",
15
14
  stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ANALYSE
16
15
  }, compilationAssets => {
17
- for (const [name, asset] of Object.entries(compilationAssets)) {
18
- if (name.endsWith(".asset.php") && asset.constructor.name === 'RawSource') {
19
- const extensionlessName = name.substring(0, name.length - 10);
20
- const additionalDependencies = [];
21
- dependencyFinder: for (const srcRoot of compilation.contextDependencies) {
22
- for (const possibleExtension of ['ts', 'tsx', 'js', 'jsx']) {
23
- const possibleFile = node_path_1.default.join(srcRoot, `${extensionlessName}.${possibleExtension}`);
24
- if (node_fs_1.default.existsSync(possibleFile)) {
25
- const firstLine = node_fs_1.default.readFileSync(possibleFile, 'utf8').trim().split(/\r?\n/)[0];
26
- if (firstLine.startsWith("//ADDITIONAL_DEPENDENCIES:")) {
27
- additionalDependencies.push(...firstLine.substring(26).trim().split(','));
28
- }
29
- break dependencyFinder;
16
+ const assetSources = new Map();
17
+ for (const entrypoint of compilation.entrypoints.values()) {
18
+ const typeScriptOrigins = entrypoint.origins.filter(origin => origin.request.endsWith('.ts') || origin.request.endsWith('.tsx'));
19
+ if (typeScriptOrigins.length > 0) {
20
+ for (const chunk of entrypoint.chunks) {
21
+ for (const file of chunk.files) {
22
+ if (file.endsWith('.asset.php')) {
23
+ assetSources.set(file, typeScriptOrigins[0].request);
30
24
  }
31
25
  }
32
26
  }
33
- const seen = new Set();
34
- const additionalDeps = additionalDependencies.filter(dep => !seen.has(dep) && seen.add(dep)).sort().map(v => `'${v}'`).join(', ');
35
- if (additionalDeps) {
36
- 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}`)));
27
+ }
28
+ }
29
+ for (const [name, asset] of Object.entries(compilationAssets)) {
30
+ if (name.endsWith(".asset.php") && asset.constructor.name === 'RawSource') {
31
+ const assetSource = assetSources.get(name);
32
+ if (!assetSource) {
33
+ continue;
34
+ }
35
+ const firstLine = node_fs_1.default.readFileSync(assetSource, 'utf8').trim().split(/\r?\n/)[0];
36
+ if (firstLine.startsWith("//ADDITIONAL_DEPENDENCIES:")) {
37
+ const additionalDependencies = firstLine.substring(26).trim().split(',');
38
+ const seen = new Set();
39
+ const additionalDeps = additionalDependencies.filter(dep => !seen.has(dep) && seen.add(dep)).sort().map(v => `'${v}'`).join(', ');
40
+ if (additionalDeps) {
41
+ 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}`)));
42
+ }
37
43
  }
38
44
  }
39
45
  }
@@ -11,12 +11,15 @@ const copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
11
11
  const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
12
12
  const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-empty-scripts"));
13
13
  const cssnano_1 = __importDefault(require("cssnano"));
14
+ function joinPossiblyAbsolutePaths(...paths) {
15
+ return paths.reduce((res, p) => !res || node_path_1.default.isAbsolute(p) ? p : node_path_1.default.join(res, p), '') || '.';
16
+ }
14
17
  function addEntryPointWithMapper(entrypoints, entrypoint, dir, mapper = (entrypoint) => entrypoint) {
15
18
  for (const ep of Array.isArray(entrypoint) ? entrypoint : [entrypoint]) {
16
- const mapped = node_path_1.default.join(dir, mapper(ep));
19
+ const mapped = joinPossiblyAbsolutePaths(dir, mapper(ep));
17
20
  if (node_fs_1.default.existsSync(mapped)) {
18
21
  const parsedEntrypoint = node_path_1.default.parse(mapped);
19
- entrypoints[node_path_1.default.join(node_path_1.default.basename(parsedEntrypoint.dir), parsedEntrypoint.name)] = { import: mapped };
22
+ entrypoints[joinPossiblyAbsolutePaths(node_path_1.default.basename(parsedEntrypoint.dir), parsedEntrypoint.name)] = { import: mapped };
20
23
  }
21
24
  }
22
25
  }
@@ -71,6 +74,18 @@ function default_1(config, webpackConfig = require("@wordpress/scripts/config/we
71
74
  }
72
75
  ]
73
76
  };
77
+ const cssLoader = require.resolve('css-loader');
78
+ if (cssLoader && webpackConfig.module?.rules) {
79
+ for (const rule of webpackConfig.module.rules) {
80
+ if (typeof rule === 'object' && "use" in rule && Array.isArray(rule.use)) {
81
+ for (const useElement of rule.use) {
82
+ if (typeof useElement === 'object' && useElement.loader === cssLoader && typeof useElement.options === 'object') {
83
+ useElement.options['url'] = false;
84
+ }
85
+ }
86
+ }
87
+ }
88
+ }
74
89
  const postcssLoader = require.resolve('postcss-loader');
75
90
  if (postcssLoader && webpackConfig.module?.rules) {
76
91
  // Options: calc
@@ -123,9 +138,11 @@ function default_1(config, webpackConfig = require("@wordpress/scripts/config/we
123
138
  }
124
139
  }
125
140
  const sources = Array.isArray(config.src) ? config.src.map(s => [s, s]) : Object.entries(config.src);
126
- return sources.map(([srcDir, dest]) => {
127
- const srcRoot = node_path_1.default.join(process.cwd(), srcDir);
128
- const copyFiles = srcDir !== dest;
141
+ return sources.map(([src, dest]) => {
142
+ const srcRoots = src.split(',');
143
+ const srcRoot = srcRoots.length === 1 ? joinPossiblyAbsolutePaths(process.cwd(), src) : srcRoots.map(s => joinPossiblyAbsolutePaths(process.cwd(), s));
144
+ const srcIsDirectory = !Array.isArray(srcRoot) && node_fs_1.default.lstatSync(srcRoot).isDirectory();
145
+ const copyFiles = srcIsDirectory && src !== dest;
129
146
  const plugins = webpackConfig.plugins ? [...webpackConfig.plugins] : [];
130
147
  plugins.push(new fork_ts_checker_webpack_plugin_1.default({
131
148
  typescript: {
@@ -139,19 +156,10 @@ function default_1(config, webpackConfig = require("@wordpress/scripts/config/we
139
156
  plugins.push(new BlockJSONStyleRemappingPlugin_1.default());
140
157
  }
141
158
  plugins.push(new AdditionalDependencyInjectorPlugin_1.default());
142
- return {
143
- ...webpackConfig,
144
- output: {
145
- ...webpackConfig.output,
146
- path: node_path_1.default.join(process.cwd(), dest)
147
- },
148
- plugins: copyFiles
149
- ? plugins.map(plugin => plugin.constructor.name === 'CopyPlugin'
150
- ? new copy_webpack_plugin_1.default({ patterns: [{ from: '**/(block.json|*.(php|twig|svg))', context: srcRoot, noErrorOnMissing: true }] })
151
- : plugin)
152
- : plugins.filter(plugin => plugin.constructor.name !== 'CopyPlugin' && plugin.constructor.name !== 'CleanWebpackPlugin'),
153
- entry: node_fs_1.default.readdirSync(srcRoot, 'utf8')
154
- .map(dir => node_path_1.default.join(srcRoot, dir))
159
+ let entry;
160
+ if (srcIsDirectory) {
161
+ entry = node_fs_1.default.readdirSync(srcRoot, 'utf8')
162
+ .map(dir => joinPossiblyAbsolutePaths(srcRoot, dir))
155
163
  .filter(dir => node_fs_1.default.statSync(dir).isDirectory())
156
164
  .reduce((entrypoints, dir) => {
157
165
  try {
@@ -177,17 +185,17 @@ function default_1(config, webpackConfig = require("@wordpress/scripts/config/we
177
185
  else {
178
186
  for (const [name, config] of Object.entries(entrypointsJSON)) {
179
187
  if (typeof config === 'string') {
180
- entrypoints[name] = node_path_1.default.join(dir, config);
188
+ entrypoints[name] = joinPossiblyAbsolutePaths(dir, config);
181
189
  }
182
190
  else if (Array.isArray(config)) {
183
- entrypoints[name] = config.map(c => node_path_1.default.join(dir, c));
191
+ entrypoints[name] = config.map(c => joinPossiblyAbsolutePaths(dir, c));
184
192
  }
185
193
  else {
186
194
  if (typeof config.import === 'string') {
187
- config.import = node_path_1.default.join(dir, config.import);
195
+ config.import = joinPossiblyAbsolutePaths(dir, config.import);
188
196
  }
189
197
  else {
190
- config.import = config.import.map(c => node_path_1.default.join(dir, c));
198
+ config.import = config.import.map(c => joinPossiblyAbsolutePaths(dir, c));
191
199
  }
192
200
  entrypoints[name] = config;
193
201
  }
@@ -200,7 +208,40 @@ function default_1(config, webpackConfig = require("@wordpress/scripts/config/we
200
208
  }
201
209
  }
202
210
  return entrypoints;
203
- }, {})
211
+ }, {});
212
+ }
213
+ else {
214
+ const baseDest = node_path_1.default.basename(dest);
215
+ entry = {
216
+ [baseDest.includes('.') ? node_path_1.default.basename(baseDest, node_path_1.default.extname(baseDest)) : baseDest]: srcRoot
217
+ };
218
+ }
219
+ let output;
220
+ if (srcIsDirectory) {
221
+ output = {
222
+ path: joinPossiblyAbsolutePaths(process.cwd(), dest)
223
+ };
224
+ }
225
+ else {
226
+ output = {
227
+ path: joinPossiblyAbsolutePaths(process.cwd(), node_path_1.default.dirname(dest))
228
+ };
229
+ }
230
+ return {
231
+ ...webpackConfig,
232
+ devtool: 'source-map',
233
+ output: {
234
+ ...webpackConfig.output,
235
+ ...output
236
+ },
237
+ plugins: copyFiles
238
+ ? plugins.map(plugin => plugin.constructor.name === 'CopyPlugin'
239
+ ? new copy_webpack_plugin_1.default({ patterns: [{ from: '**/(block.json|*.(php|twig|svg))', context: srcRoot, noErrorOnMissing: true }] })
240
+ : plugin)
241
+ : (srcIsDirectory
242
+ ? plugins.filter(plugin => plugin.constructor.name !== 'CopyPlugin' && plugin.constructor.name !== 'CleanWebpackPlugin')
243
+ : plugins.filter(plugin => plugin.constructor.name !== 'CopyPlugin')),
244
+ entry
204
245
  };
205
246
  });
206
247
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",
@@ -13,13 +13,13 @@
13
13
  "./wordpress-scripts-wrapper": "./build/wordpress-scripts-wrapper.js"
14
14
  },
15
15
  "devDependencies": {
16
- "@types/node": "^20.2.3",
16
+ "@types/node": "^20.2.5",
17
17
  "@types/webpack": "^5.28.1",
18
18
  "@types/webpack-sources": "^3.2.0",
19
19
  "postcss-load-config": "^4.0.1",
20
- "postcss-loader": "^7.3.0",
20
+ "postcss-loader": "^7.3.2",
21
21
  "ts-node": "^10.9.1",
22
- "typescript": "^5.0.4"
22
+ "typescript": "^5.1.3"
23
23
  },
24
24
  "dependencies": {
25
25
  "@plaudit/postcss-color-function": "^5.0.0",
@@ -31,11 +31,11 @@
31
31
  "clean-webpack-plugin": "^4.0.0",
32
32
  "copy-webpack-plugin": "^11.0.0",
33
33
  "cssnano": "^6.0.1",
34
- "eslint": "^8.39.0",
34
+ "eslint": "^8.42.0",
35
35
  "eslint-plugin-jsdoc": "^43.1.1",
36
36
  "fork-ts-checker-webpack-plugin": "^8.0.0",
37
- "postcss": "^8.4.23",
38
- "postcss-calc": "^8.2.4",
37
+ "postcss": "^8.4.24",
38
+ "postcss-calc": "^9.0.1",
39
39
  "postcss-extend": "^1.0.5",
40
40
  "postcss-fallback": "^0.1.0",
41
41
  "postcss-import": "^15.1.0",
@@ -51,7 +51,7 @@
51
51
  "postcss-short-spacing": "^4.0.0",
52
52
  "react": "^18.2.0",
53
53
  "react-dom": "^18.2.0",
54
- "webpack": "^5.84.0",
54
+ "webpack": "^5.86.0",
55
55
  "webpack-remove-empty-scripts": "^1.0.3",
56
56
  "webpack-sources": "^3.2.3"
57
57
  },