@plaudit/webpack-extensions 2.1.2 → 2.1.4

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.
@@ -14,29 +14,76 @@ const webpack_remove_empty_scripts_1 = __importDefault(require("webpack-remove-e
14
14
  function joinPossiblyAbsolutePaths(...paths) {
15
15
  return paths.reduce((res, p) => !res || node_path_1.default.isAbsolute(p) ? p : node_path_1.default.join(res, p), '') || '.';
16
16
  }
17
- function addEntryPointWithMapper(entrypoints, entrypoint, dir, mapper = (entrypoint) => entrypoint) {
18
- for (const ep of Array.isArray(entrypoint) ? entrypoint : [entrypoint]) {
19
- const mapped = joinPossiblyAbsolutePaths(dir, mapper(ep));
20
- if (node_fs_1.default.existsSync(mapped)) {
21
- const parsedEntrypoint = node_path_1.default.parse(mapped);
22
- entrypoints[joinPossiblyAbsolutePaths(node_path_1.default.basename(parsedEntrypoint.dir), parsedEntrypoint.name)] = { import: mapped };
23
- }
24
- }
17
+ function mapToRealEntrypoints(entrypoint, dir, mapper = (entrypoint) => entrypoint) {
18
+ return (Array.isArray(entrypoint) ? entrypoint : [entrypoint])
19
+ .map(ep => joinPossiblyAbsolutePaths(dir, mapper(ep)))
20
+ .filter(ep => node_fs_1.default.existsSync(ep))
21
+ .map(ep => {
22
+ const parsedEntrypoint = node_path_1.default.parse(ep);
23
+ return [joinPossiblyAbsolutePaths(node_path_1.default.basename(parsedEntrypoint.dir), parsedEntrypoint.name), { import: ep }];
24
+ });
25
25
  }
26
26
  function isTruthy(value) {
27
27
  return !!value;
28
28
  }
29
- module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
29
+ function groupEntrypointsByAssetFile(entrypoints, entrypointNameExtractor) {
30
30
  const seenPaths = new Map();
31
- for (const bundleName of (Array.isArray(config.src) ? config.src : Object.values(config.src))) {
32
- const key = bundleName.substring(0, bundleName.length - node_path_1.default.extname(bundleName).length);
31
+ for (const entrypoint of entrypoints) {
32
+ const entrypointName = entrypointNameExtractor(entrypoint);
33
+ const key = entrypointName.substring(0, entrypointName.length - node_path_1.default.extname(entrypointName).length);
33
34
  let seen = seenPaths.get(key);
34
35
  if (seen === undefined) {
35
36
  seenPaths.set(key, seen = []);
36
37
  }
37
- seen.push(bundleName);
38
- node_path_1.default.basename(bundleName, node_path_1.default.extname(bundleName));
38
+ seen.push(entrypoint);
39
+ }
40
+ return seenPaths;
41
+ }
42
+ const scriptExtension = /\.[jt]sx?$/;
43
+ const styleExtension = /\.(p?c|sa)ss$/;
44
+ function scriptOrStyleTest(entryPath) {
45
+ return scriptExtension.test(entryPath) ? "script" : (styleExtension.test(entryPath) ? "style" : "");
46
+ }
47
+ function determineEntrypointType(entrypoint) {
48
+ let res = scriptOrStyleTest(entrypoint[0]);
49
+ if (res) {
50
+ return res;
51
+ }
52
+ if (typeof entrypoint[1] === 'string') {
53
+ return scriptOrStyleTest(entrypoint[1]);
54
+ }
55
+ else if (Array.isArray(entrypoint[1])) {
56
+ return entrypoint[1].reduce((prior, ep) => prior || scriptOrStyleTest(ep), "");
57
+ }
58
+ else if (typeof entrypoint[1].import === 'string') {
59
+ return scriptOrStyleTest(entrypoint[1].import);
60
+ }
61
+ else {
62
+ return entrypoint[1].import.reduce((prior, ep) => prior || scriptOrStyleTest(ep), "");
39
63
  }
64
+ }
65
+ function injectTypeAndCountToEntrypointName(entrypointName, type, typeCounts) {
66
+ const entrypointBasename = entrypointName.substring(0, entrypointName.length - node_path_1.default.extname(entrypointName).length);
67
+ const parts = [];
68
+ if (type) {
69
+ parts.push(type);
70
+ }
71
+ if (typeCounts[type] ?? (typeCounts[type] = 0)) {
72
+ parts.push(typeCounts[type].toString());
73
+ }
74
+ typeCounts[type] += 1;
75
+ return `${entrypointBasename}_${parts.join('-')}${node_path_1.default.extname(entrypointName)}`;
76
+ }
77
+ function addPotentiallyDuplicatedEntrypointName(entry, entrypoint, typeCounts) {
78
+ const type = determineEntrypointType(entrypoint);
79
+ let potentialKey = injectTypeAndCountToEntrypointName(entrypoint[0], type, typeCounts);
80
+ while (entry[potentialKey]) {
81
+ potentialKey = injectTypeAndCountToEntrypointName(entrypoint[0], type, typeCounts);
82
+ }
83
+ entry[potentialKey] = entrypoint[1];
84
+ }
85
+ module.exports = function (config, webpackConfig = require("@wordpress/scripts/config/webpack.config")) {
86
+ const seenPaths = groupEntrypointsByAssetFile(Array.isArray(config.src) ? config.src : Object.values(config.src), bn => bn);
40
87
  let projectPrefix = undefined;
41
88
  let duplicatedPaths = "";
42
89
  for (const sameNamePaths of seenPaths.values()) {
@@ -59,7 +106,7 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
59
106
  console.error(`Encountered multiple paths that produce the same effective bundle name:${duplicatedPaths}`);
60
107
  process.exit(1);
61
108
  }
62
- const { variables = ["variables.js", "preprocess/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;
109
+ const { 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;
63
110
  const cssLoader = require.resolve('css-loader');
64
111
  if (cssLoader && webpackConfig.module?.rules) {
65
112
  for (const rule of webpackConfig.module.rules) {
@@ -113,6 +160,7 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
113
160
  const copyFiles = srcIsDirectory && src !== dest;
114
161
  const plugins = webpackConfig.plugins?.filter(isTruthy) ?? [];
115
162
  plugins.push(new fork_ts_checker_webpack_plugin_1.default({
163
+ async: true,
116
164
  typescript: {
117
165
  diagnosticOptions: {
118
166
  semantic: true,
@@ -137,37 +185,39 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
137
185
  }
138
186
  let entry;
139
187
  if (srcIsDirectory) {
140
- entry = node_fs_1.default.readdirSync(srcRoot, 'utf8')
188
+ const rawEntrypoints = node_fs_1.default.readdirSync(srcRoot, 'utf8')
141
189
  .map(dir => joinPossiblyAbsolutePaths(srcRoot, dir))
142
190
  .filter(dir => node_fs_1.default.statSync(dir).isDirectory())
143
- .reduce((entrypoints, dir) => {
191
+ .flatMap(dir => {
192
+ const res = [];
144
193
  try {
145
194
  const blockJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'block.json'), 'utf8'));
146
195
  for (const key of ["editorStyle", "style", "editorScript", "viewScript", "script"]) {
147
196
  if (key in blockJSON) {
148
- addEntryPointWithMapper(entrypoints, blockJSON[key], dir, ep => ep.startsWith("file:") ? ep.substring(5) : ep);
197
+ res.push(...mapToRealEntrypoints(blockJSON[key], dir, ep => ep.startsWith("file:") ? ep.substring(5) : ep));
149
198
  }
150
199
  }
200
+ return res;
151
201
  }
152
202
  catch (e) {
153
203
  try {
154
204
  const packageJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'package.json'), 'utf8'));
155
- addEntryPointWithMapper(entrypoints, packageJSON['main'], dir);
156
- addEntryPointWithMapper(entrypoints, packageJSON['style'], dir);
205
+ res.push(...mapToRealEntrypoints(packageJSON['main'], dir));
206
+ res.push(...mapToRealEntrypoints(packageJSON['style'], dir));
157
207
  }
158
208
  catch (e) {
159
209
  try {
160
210
  const entrypointsJSON = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.join(dir, 'entrypoints.json'), 'utf8'));
161
211
  if (Array.isArray(entrypointsJSON)) {
162
- addEntryPointWithMapper(entrypoints, entrypointsJSON, dir);
212
+ res.push(...mapToRealEntrypoints(entrypointsJSON, dir));
163
213
  }
164
214
  else {
165
215
  for (const [name, config] of Object.entries(entrypointsJSON)) {
166
216
  if (typeof config === 'string') {
167
- entrypoints[name] = joinPossiblyAbsolutePaths(dir, config);
217
+ res.push([name, joinPossiblyAbsolutePaths(dir, config)]);
168
218
  }
169
219
  else if (Array.isArray(config)) {
170
- entrypoints[name] = config.map(c => joinPossiblyAbsolutePaths(dir, c));
220
+ res.push([name, config.map(c => joinPossiblyAbsolutePaths(dir, c))]);
171
221
  }
172
222
  else {
173
223
  if (typeof config.import === 'string') {
@@ -176,7 +226,7 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
176
226
  else {
177
227
  config.import = config.import.map(c => joinPossiblyAbsolutePaths(dir, c));
178
228
  }
179
- entrypoints[name] = config;
229
+ res.push([name, config]);
180
230
  }
181
231
  }
182
232
  }
@@ -186,8 +236,21 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
186
236
  }
187
237
  }
188
238
  }
189
- return entrypoints;
190
- }, {});
239
+ return res;
240
+ });
241
+ const perAssetPathGroupedEntrypoints = groupEntrypointsByAssetFile(rawEntrypoints, e => e[0]);
242
+ entry = {};
243
+ for (const groupedEntrypoints of perAssetPathGroupedEntrypoints.values()) {
244
+ if (groupedEntrypoints.length === 1) {
245
+ entry[groupedEntrypoints[0][0]] = groupedEntrypoints[0][1];
246
+ }
247
+ else {
248
+ const typeCounts = {};
249
+ for (const entrypoint of groupedEntrypoints) {
250
+ addPotentiallyDuplicatedEntrypointName(entry, entrypoint, typeCounts);
251
+ }
252
+ }
253
+ }
191
254
  }
192
255
  else {
193
256
  const baseDest = node_path_1.default.basename(dest);
@@ -209,6 +272,7 @@ module.exports = function (config, webpackConfig = require("@wordpress/scripts/c
209
272
  return {
210
273
  ...webpackConfig,
211
274
  devtool: 'source-map',
275
+ mode: "production",
212
276
  output: {
213
277
  ...webpackConfig.output,
214
278
  ...output
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plaudit/webpack-extensions",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "scripts": {
5
5
  "prepublishOnly": "rm -rf build && mkdir build && tsc",
6
6
  "build": "tsc",