@plaudit/webpack-extensions 2.5.2 → 2.6.1

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.
@@ -0,0 +1,6 @@
1
+ import type { Compiler, WebpackPluginInstance } from "webpack";
2
+ export default class VariablesJSMonitorPlugin implements WebpackPluginInstance {
3
+ private readonly variablesFilePath;
4
+ constructor(variablesFilePath: string);
5
+ apply(compiler: Compiler): void;
6
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class VariablesJSMonitorPlugin {
4
+ variablesFilePath;
5
+ constructor(variablesFilePath) {
6
+ this.variablesFilePath = variablesFilePath;
7
+ }
8
+ apply(compiler) {
9
+ compiler.hooks.make.tap('VariablesJSMonitorPlugin', compilation => {
10
+ if (!compilation.fileDependencies.has(this.variablesFilePath)) {
11
+ compilation.fileDependencies.add(this.variablesFilePath);
12
+ }
13
+ });
14
+ }
15
+ }
16
+ exports.default = VariablesJSMonitorPlugin;
@@ -11,6 +11,7 @@ const browser_sync_webpack_plugin_1 = __importDefault(require("browser-sync-webp
11
11
  const copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
12
12
  const fork_ts_checker_webpack_plugin_1 = __importDefault(require("fork-ts-checker-webpack-plugin"));
13
13
  const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-empty-scripts"));
14
+ const VariablesJSMonitorPlugin_1 = __importDefault(require("./wordpress-scripts-wrapper/VariablesJSMonitorPlugin"));
14
15
  function joinPossiblyAbsolutePaths(...paths) {
15
16
  return paths.reduce((res, p) => !res || node_path_1.default.isAbsolute(p) ? p : node_path_1.default.join(res, p), '') || '.';
16
17
  }
@@ -138,7 +139,7 @@ function disableDefaultURLProcessing(webpackConfig) {
138
139
  function injectPostcssConfigOverrides(webpackConfig, variables, verbose) {
139
140
  if (webpackConfig.module?.rules) {
140
141
  const postcssConfig = (0, static_configs_1.postcssConfigBuilder)(verbose, (name) => {
141
- return variables[name] ?? (name === 'ENV' ? '' : undefined);
142
+ return variables(name) ?? (name === 'ENV' ? '' : undefined);
142
143
  });
143
144
  for (const rule of webpackConfig.module.rules) {
144
145
  if (isTruthy(rule) && typeof rule === 'object' && Array.isArray(rule.use)) {
@@ -188,9 +189,14 @@ function parseEntrypointsJSON(dir) {
188
189
  }
189
190
  module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
190
191
  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
+ const { standaloneBlocks = false, variables: rawVariables, verbose = process.argv.includes('--verbose') || process.env['VERBOSE'] === 'true' } = config;
193
+ let variablesFilePath = undefined;
194
+ let currentVariables = rawVariables ?? {};
195
+ if (!rawVariables) {
196
+ variablesFilePath = ["variables.js", "src/site/variables.js"].map(p => node_path_1.default.join(process.cwd(), p)).filter(p => node_fs_1.default.existsSync(p))[0];
197
+ }
192
198
  disableDefaultURLProcessing(webpackConfig);
193
- injectPostcssConfigOverrides(webpackConfig, variables, verbose);
199
+ injectPostcssConfigOverrides(webpackConfig, name => currentVariables[name], verbose);
194
200
  let first = true;
195
201
  const sources = Array.isArray(config.src) ? config.src.map(s => [s, s]) : Object.entries(config.src);
196
202
  return sources.map(([src, dest]) => {
@@ -214,6 +220,9 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
214
220
  stage: webpack_remove_empty_scripts_1.default.STAGE_AFTER_PROCESS_PLUGINS,
215
221
  extensions: ['css', 'scss', 'sass', 'less', 'styl', 'pcss']
216
222
  }));
223
+ if (variablesFilePath) {
224
+ plugins.push(new VariablesJSMonitorPlugin_1.default(variablesFilePath));
225
+ }
217
226
  if (copyFiles) {
218
227
  plugins.push(new BlockJSONStyleRemappingPlugin_1.default(standaloneBlocks));
219
228
  }
@@ -231,67 +240,59 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
231
240
  }
232
241
  let entry;
233
242
  if (srcIsDirectory) {
234
- const rawEntrypoints = node_fs_1.default.readdirSync(srcRoot, 'utf8')
235
- .map(dir => joinPossiblyAbsolutePaths(srcRoot, dir))
236
- .filter(dir => node_fs_1.default.statSync(dir).isDirectory())
237
- .flatMap(dir => {
238
- const res = [];
239
- try {
240
- const blockJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'block.json'), 'utf8'));
241
- for (const key of ["editorStyle", "style", "editorScript", "viewScript", "script"]) {
242
- if (key in blockJSON) {
243
- res.push(...mapToRealEntrypoints(blockJSON[key], dir, ep => ep.startsWith("file:") ? ep.substring(5) : ep));
244
- }
245
- }
246
- return res;
247
- }
248
- catch (e) {
243
+ entry = () => {
244
+ const rawEntrypoints = node_fs_1.default.readdirSync(srcRoot, 'utf8')
245
+ .map(dir => joinPossiblyAbsolutePaths(srcRoot, dir))
246
+ .filter(dir => node_fs_1.default.statSync(dir).isDirectory())
247
+ .flatMap(dir => {
248
+ const res = [];
249
249
  try {
250
- const packageJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'package.json'), 'utf8'));
251
- res.push(...mapToRealEntrypoints(packageJSON['main'], dir));
252
- res.push(...mapToRealEntrypoints(packageJSON['style'], dir));
250
+ const blockJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'block.json'), 'utf8'));
251
+ for (const key of ["editorStyle", "style", "editorScript", "viewScript", "script"]) {
252
+ if (key in blockJSON) {
253
+ res.push(...mapToRealEntrypoints(blockJSON[key], dir, ep => ep.startsWith("file:") ? ep.substring(5) : ep));
254
+ }
255
+ }
256
+ return res;
253
257
  }
254
258
  catch (e) {
255
259
  try {
256
- res.push(...parseEntrypointsJSON(dir));
260
+ const packageJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'package.json'), 'utf8'));
261
+ res.push(...mapToRealEntrypoints(packageJSON['main'], dir));
262
+ res.push(...mapToRealEntrypoints(packageJSON['style'], dir));
257
263
  }
258
264
  catch (e) {
259
- // This just means that the directory doesn't contain any declared entrypoints.
265
+ try {
266
+ res.push(...parseEntrypointsJSON(dir));
267
+ }
268
+ catch (e) {
269
+ // This just means that the directory doesn't contain any declared entrypoints.
270
+ }
260
271
  }
261
272
  }
262
- }
263
- return res;
264
- });
265
- const perAssetPathGroupedEntrypoints = groupEntrypointsByAssetFile(rawEntrypoints, e => e[0]);
266
- entry = {};
267
- for (const groupedEntrypoints of perAssetPathGroupedEntrypoints.values()) {
268
- if (groupedEntrypoints.length === 1) {
269
- entry[groupedEntrypoints[0][0]] = groupedEntrypoints[0][1];
270
- }
271
- else {
272
- const typeCounts = {};
273
- for (const entrypoint of groupedEntrypoints) {
274
- addPotentiallyDuplicatedEntrypointName(entry, entrypoint, typeCounts);
273
+ return res;
274
+ });
275
+ const perAssetPathGroupedEntrypoints = groupEntrypointsByAssetFile(rawEntrypoints, e => e[0]);
276
+ const currentEntry = {};
277
+ for (const groupedEntrypoints of perAssetPathGroupedEntrypoints.values()) {
278
+ if (groupedEntrypoints.length === 1) {
279
+ currentEntry[groupedEntrypoints[0][0]] = groupedEntrypoints[0][1];
280
+ }
281
+ else {
282
+ const typeCounts = {};
283
+ for (const entrypoint of groupedEntrypoints) {
284
+ addPotentiallyDuplicatedEntrypointName(currentEntry, entrypoint, typeCounts);
285
+ }
275
286
  }
276
287
  }
277
- }
288
+ return currentEntry;
289
+ };
278
290
  }
279
291
  else {
280
292
  const baseDest = node_path_1.default.basename(destPath);
281
- entry = {
293
+ entry = () => ({
282
294
  [baseDest.includes('.') ? node_path_1.default.basename(baseDest, node_path_1.default.extname(baseDest)) : baseDest]: srcRoot
283
- };
284
- }
285
- let output;
286
- if (srcIsDirectory) {
287
- output = {
288
- path: joinPossiblyAbsolutePaths(process.cwd(), destPath)
289
- };
290
- }
291
- else {
292
- output = {
293
- path: joinPossiblyAbsolutePaths(process.cwd(), node_path_1.default.dirname(destPath))
294
- };
295
+ });
295
296
  }
296
297
  return {
297
298
  ...webpackConfig,
@@ -299,7 +300,7 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
299
300
  mode: "production",
300
301
  output: {
301
302
  ...webpackConfig.output,
302
- ...output
303
+ path: joinPossiblyAbsolutePaths(process.cwd(), srcIsDirectory ? destPath : node_path_1.default.dirname(destPath))
303
304
  },
304
305
  plugins: copyFiles
305
306
  ? plugins.map(plugin => plugin.constructor.name === 'CopyPlugin'
@@ -309,7 +310,13 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
309
310
  : (srcIsDirectory
310
311
  ? plugins.filter(plugin => plugin.constructor.name !== 'CopyPlugin' && plugin.constructor.name !== 'CleanWebpackPlugin')
311
312
  : plugins.filter(plugin => plugin.constructor.name !== 'CopyPlugin')),
312
- entry
313
+ entry() {
314
+ if (variablesFilePath) {
315
+ delete require.cache[require.resolve(variablesFilePath)];
316
+ currentVariables = require(variablesFilePath);
317
+ }
318
+ return entry();
319
+ }
313
320
  };
314
321
  });
315
322
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.5.2",
3
+ "version": "2.6.1",
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",