@plaudit/webpack-extensions 2.5.1 → 2.6.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.
@@ -96,7 +96,7 @@ function addPotentiallyDuplicatedEntrypointName(entry, entrypoint, typeCounts) {
96
96
  }
97
97
  entry[potentialKey] = entrypoint[1];
98
98
  }
99
- module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
99
+ function testForDuplicatedEntryPaths(config) {
100
100
  const seenPaths = groupEntrypointsByAssetFile(Array.isArray(config.src) ? config.src : Object.values(config.src).map(bn => typeof bn === 'string' ? bn : bn.destination), bn => bn);
101
101
  let projectPrefix = undefined;
102
102
  let duplicatedPaths = "";
@@ -120,7 +120,8 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
120
120
  console.error(`Encountered multiple paths that produce the same effective bundle name:${duplicatedPaths}`);
121
121
  process.exit(1);
122
122
  }
123
- const { standaloneBlocks = false, variables = ["variables.js", "src/site/variables.js"].map(p => node_path_1.default.join(process.cwd(), p)).filter(p => node_fs_1.default.existsSync(p)).map(p => require(p))[0] ?? {}, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true' } = config;
123
+ }
124
+ function disableDefaultURLProcessing(webpackConfig) {
124
125
  const cssLoader = require.resolve('css-loader');
125
126
  if (cssLoader && webpackConfig.module?.rules) {
126
127
  for (const rule of webpackConfig.module.rules) {
@@ -133,16 +134,11 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
133
134
  }
134
135
  }
135
136
  }
136
- let currentEntrypoint = '';
137
+ }
138
+ function injectPostcssConfigOverrides(webpackConfig, variables, verbose) {
137
139
  if (webpackConfig.module?.rules) {
138
140
  const postcssConfig = (0, static_configs_1.postcssConfigBuilder)(verbose, (name) => {
139
- if (variables[name]) {
140
- return variables[name];
141
- }
142
- if (name === 'ENV') { // This is purely a backwards-compatibility issue.
143
- return currentEntrypoint.endsWith('public.pcss') ? 'PUBLIC' : (currentEntrypoint.endsWith('block-editor.pcss') ? 'EDITOR' : '');
144
- }
145
- return undefined;
141
+ return variables[name] ?? (name === 'ENV' ? '' : undefined);
146
142
  });
147
143
  for (const rule of webpackConfig.module.rules) {
148
144
  if (isTruthy(rule) && typeof rule === 'object' && Array.isArray(rule.use)) {
@@ -164,10 +160,40 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
164
160
  }
165
161
  }
166
162
  }
163
+ }
164
+ function parseEntrypointsJSON(dir) {
165
+ const entrypointsJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'entrypoints.json'), 'utf8'));
166
+ if (Array.isArray(entrypointsJSON)) {
167
+ return mapToRealEntrypoints(entrypointsJSON, dir);
168
+ }
169
+ else {
170
+ return Object.entries(entrypointsJSON).map(([name, config]) => {
171
+ if (typeof config === 'string') {
172
+ return [name, joinPossiblyAbsolutePaths(dir, config)];
173
+ }
174
+ else if (Array.isArray(config)) {
175
+ return [name, config.map(c => joinPossiblyAbsolutePaths(dir, c))];
176
+ }
177
+ else {
178
+ if (typeof config.import === 'string') {
179
+ config.import = joinPossiblyAbsolutePaths(dir, config.import);
180
+ }
181
+ else {
182
+ config.import = config.import.map(c => joinPossiblyAbsolutePaths(dir, c));
183
+ }
184
+ return [name, config];
185
+ }
186
+ });
187
+ }
188
+ }
189
+ module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
190
+ testForDuplicatedEntryPaths(config);
191
+ const { standaloneBlocks = false, variables = ["variables.js", "src/site/variables.js"].map(p => node_path_1.default.join(process.cwd(), p)).filter(p => node_fs_1.default.existsSync(p)).map(p => require(p))[0] ?? {}, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true' } = config;
192
+ disableDefaultURLProcessing(webpackConfig);
193
+ injectPostcssConfigOverrides(webpackConfig, variables, verbose);
167
194
  let first = true;
168
195
  const sources = Array.isArray(config.src) ? config.src.map(s => [s, s]) : Object.entries(config.src);
169
196
  return sources.map(([src, dest]) => {
170
- currentEntrypoint = src;
171
197
  const srcRoots = typeof dest !== 'string' && dest.withLegacyBlocksIn
172
198
  ? [...src.split(','), ...resolveLegacyBlockScriptsInFolder(dest.withLegacyBlocksIn)]
173
199
  : src.split(',');
@@ -184,7 +210,10 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
184
210
  syntactic: true,
185
211
  }
186
212
  }
187
- }), new webpack_remove_empty_scripts_1.default({ stage: webpack_remove_empty_scripts_1.default.STAGE_AFTER_PROCESS_PLUGINS, extensions: ['css', 'scss', 'sass', 'less', 'styl', 'pcss'] }));
213
+ }), new webpack_remove_empty_scripts_1.default({
214
+ stage: webpack_remove_empty_scripts_1.default.STAGE_AFTER_PROCESS_PLUGINS,
215
+ extensions: ['css', 'scss', 'sass', 'less', 'styl', 'pcss']
216
+ }));
188
217
  if (copyFiles) {
189
218
  plugins.push(new BlockJSONStyleRemappingPlugin_1.default(standaloneBlocks));
190
219
  }
@@ -202,72 +231,53 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
202
231
  }
203
232
  let entry;
204
233
  if (srcIsDirectory) {
205
- const rawEntrypoints = node_fs_1.default.readdirSync(srcRoot, 'utf8')
206
- .map(dir => joinPossiblyAbsolutePaths(srcRoot, dir))
207
- .filter(dir => node_fs_1.default.statSync(dir).isDirectory())
208
- .flatMap(dir => {
209
- const res = [];
210
- try {
211
- const blockJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'block.json'), 'utf8'));
212
- for (const key of ["editorStyle", "style", "editorScript", "viewScript", "script"]) {
213
- if (key in blockJSON) {
214
- res.push(...mapToRealEntrypoints(blockJSON[key], dir, ep => ep.startsWith("file:") ? ep.substring(5) : ep));
215
- }
216
- }
217
- return res;
218
- }
219
- catch (e) {
234
+ entry = () => {
235
+ const rawEntrypoints = node_fs_1.default.readdirSync(srcRoot, 'utf8')
236
+ .map(dir => joinPossiblyAbsolutePaths(srcRoot, dir))
237
+ .filter(dir => node_fs_1.default.statSync(dir).isDirectory())
238
+ .flatMap(dir => {
239
+ const res = [];
220
240
  try {
221
- const packageJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'package.json'), 'utf8'));
222
- res.push(...mapToRealEntrypoints(packageJSON['main'], dir));
223
- res.push(...mapToRealEntrypoints(packageJSON['style'], dir));
241
+ const blockJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'block.json'), 'utf8'));
242
+ for (const key of ["editorStyle", "style", "editorScript", "viewScript", "script"]) {
243
+ if (key in blockJSON) {
244
+ res.push(...mapToRealEntrypoints(blockJSON[key], dir, ep => ep.startsWith("file:") ? ep.substring(5) : ep));
245
+ }
246
+ }
247
+ return res;
224
248
  }
225
249
  catch (e) {
226
250
  try {
227
- const entrypointsJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'entrypoints.json'), 'utf8'));
228
- if (Array.isArray(entrypointsJSON)) {
229
- res.push(...mapToRealEntrypoints(entrypointsJSON, dir));
230
- }
231
- else {
232
- for (const [name, config] of Object.entries(entrypointsJSON)) {
233
- if (typeof config === 'string') {
234
- res.push([name, joinPossiblyAbsolutePaths(dir, config)]);
235
- }
236
- else if (Array.isArray(config)) {
237
- res.push([name, config.map(c => joinPossiblyAbsolutePaths(dir, c))]);
238
- }
239
- else {
240
- if (typeof config.import === 'string') {
241
- config.import = joinPossiblyAbsolutePaths(dir, config.import);
242
- }
243
- else {
244
- config.import = config.import.map(c => joinPossiblyAbsolutePaths(dir, c));
245
- }
246
- res.push([name, config]);
247
- }
248
- }
249
- }
251
+ const packageJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'package.json'), 'utf8'));
252
+ res.push(...mapToRealEntrypoints(packageJSON['main'], dir));
253
+ res.push(...mapToRealEntrypoints(packageJSON['style'], dir));
250
254
  }
251
255
  catch (e) {
252
- // This just means that the directory doesn't contain any declared entrypoints.
256
+ try {
257
+ res.push(...parseEntrypointsJSON(dir));
258
+ }
259
+ catch (e) {
260
+ // This just means that the directory doesn't contain any declared entrypoints.
261
+ }
253
262
  }
254
263
  }
255
- }
256
- return res;
257
- });
258
- const perAssetPathGroupedEntrypoints = groupEntrypointsByAssetFile(rawEntrypoints, e => e[0]);
259
- entry = {};
260
- for (const groupedEntrypoints of perAssetPathGroupedEntrypoints.values()) {
261
- if (groupedEntrypoints.length === 1) {
262
- entry[groupedEntrypoints[0][0]] = groupedEntrypoints[0][1];
263
- }
264
- else {
265
- const typeCounts = {};
266
- for (const entrypoint of groupedEntrypoints) {
267
- addPotentiallyDuplicatedEntrypointName(entry, entrypoint, typeCounts);
264
+ return res;
265
+ });
266
+ const perAssetPathGroupedEntrypoints = groupEntrypointsByAssetFile(rawEntrypoints, e => e[0]);
267
+ const currentEntry = {};
268
+ for (const groupedEntrypoints of perAssetPathGroupedEntrypoints.values()) {
269
+ if (groupedEntrypoints.length === 1) {
270
+ currentEntry[groupedEntrypoints[0][0]] = groupedEntrypoints[0][1];
271
+ }
272
+ else {
273
+ const typeCounts = {};
274
+ for (const entrypoint of groupedEntrypoints) {
275
+ addPotentiallyDuplicatedEntrypointName(currentEntry, entrypoint, typeCounts);
276
+ }
268
277
  }
269
278
  }
270
- }
279
+ return currentEntry;
280
+ };
271
281
  }
272
282
  else {
273
283
  const baseDest = node_path_1.default.basename(destPath);
@@ -275,24 +285,13 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
275
285
  [baseDest.includes('.') ? node_path_1.default.basename(baseDest, node_path_1.default.extname(baseDest)) : baseDest]: srcRoot
276
286
  };
277
287
  }
278
- let output;
279
- if (srcIsDirectory) {
280
- output = {
281
- path: joinPossiblyAbsolutePaths(process.cwd(), destPath)
282
- };
283
- }
284
- else {
285
- output = {
286
- path: joinPossiblyAbsolutePaths(process.cwd(), node_path_1.default.dirname(destPath))
287
- };
288
- }
289
288
  return {
290
289
  ...webpackConfig,
291
290
  devtool: 'source-map',
292
291
  mode: "production",
293
292
  output: {
294
293
  ...webpackConfig.output,
295
- ...output
294
+ path: joinPossiblyAbsolutePaths(process.cwd(), srcIsDirectory ? destPath : node_path_1.default.dirname(destPath))
296
295
  },
297
296
  plugins: copyFiles
298
297
  ? plugins.map(plugin => plugin.constructor.name === 'CopyPlugin'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.5.1",
3
+ "version": "2.6.0",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",
@@ -38,7 +38,7 @@
38
38
  "eslint": "^8.43.0",
39
39
  "eslint-plugin-jsdoc": "^43.1.1",
40
40
  "fork-ts-checker-webpack-plugin": "^8.0.0",
41
- "postcss": "^8.4.24",
41
+ "postcss": "^8.4.25",
42
42
  "postcss-calc": "^9.0.1",
43
43
  "postcss-fallback": "^0.1.0",
44
44
  "postcss-functions": "^4.0.2",