@jsenv/plugin-bundling 2.7.24 → 2.8.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/plugin-bundling",
3
- "version": "2.7.24",
3
+ "version": "2.8.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,10 +21,10 @@
21
21
  "/src/"
22
22
  ],
23
23
  "dependencies": {
24
- "@jsenv/humanize": "1.2.8",
25
- "@jsenv/url-meta": "8.5.2",
26
- "@jsenv/urls": "2.6.0",
27
- "@jsenv/sourcemap": "1.2.30",
24
+ "@jsenv/humanize": "1.3.0",
25
+ "@jsenv/url-meta": "8.5.4",
26
+ "@jsenv/urls": "2.6.1",
27
+ "@jsenv/sourcemap": "1.3.0",
28
28
  "lightningcss": "1.29.1",
29
29
  "rollup": "4.31.0"
30
30
  },
@@ -1,8 +1,9 @@
1
+ import { lookupPackageDirectory } from "@jsenv/filesystem";
1
2
  import { createDetailedMessage } from "@jsenv/humanize";
2
3
  import { sourcemapConverter } from "@jsenv/sourcemap";
3
4
  import { URL_META } from "@jsenv/url-meta";
4
5
  import { isFileSystemPath } from "@jsenv/urls";
5
-
6
+ import { readFileSync } from "node:fs";
6
7
  import { fileUrlConverter } from "../file_url_converter.js";
7
8
 
8
9
  export const bundleJsModules = async (
@@ -35,15 +36,59 @@ export const bundleJsModules = async (
35
36
  }
36
37
 
37
38
  let manualChunks;
38
- if (Object.keys(chunks).length) {
39
+ if (chunks) {
40
+ if (chunks.vendors) {
41
+ chunks.vendors = {
42
+ "file:///**/node_modules/": true,
43
+ ...chunks.vendors,
44
+ };
45
+ } else {
46
+ chunks.vendors = {
47
+ "file:///**/node_modules/": true,
48
+ };
49
+ }
50
+ const packageDirectoryUrl = lookupPackageDirectory(rootDirectoryUrl);
51
+ if (packageDirectoryUrl) {
52
+ try {
53
+ const packageFileContent = readFileSync(
54
+ new URL("package.json", packageDirectoryUrl),
55
+ "utf8",
56
+ );
57
+ const packageJSON = JSON.parse(packageFileContent);
58
+ const workspaces = packageJSON.workspaces;
59
+ if (workspaces) {
60
+ const workspacePatterns = {};
61
+ for (const workspace of workspaces) {
62
+ const workspacePattern = new URL(
63
+ workspace.endsWith("/*") ? workspace.slice(0, -1) : workspace,
64
+ packageDirectoryUrl,
65
+ ).href;
66
+ workspacePatterns[workspacePattern] = true;
67
+ }
68
+ if (chunks.workspaces) {
69
+ chunks.workspaces = {
70
+ ...workspacePatterns,
71
+ ...chunks.workspaces,
72
+ };
73
+ } else {
74
+ chunks.workspaces = workspacePatterns;
75
+ }
76
+ }
77
+ } catch {}
78
+ }
79
+
39
80
  const associations = URL_META.resolveAssociations(chunks, rootDirectoryUrl);
40
- manualChunks = (id) => {
81
+ manualChunks = (id, manualChunksApi) => {
41
82
  if (rollupOutput.manualChunks) {
42
- const manualChunkName = rollupOutput.manualChunks(id);
83
+ const manualChunkName = rollupOutput.manualChunks(id, manualChunksApi);
43
84
  if (manualChunkName) {
44
85
  return manualChunkName;
45
86
  }
46
87
  }
88
+ const moduleInfo = manualChunksApi.getModuleInfo(id);
89
+ if (moduleInfo.isEntry || moduleInfo.dynamicImporters.length) {
90
+ return null;
91
+ }
47
92
  const url = fileUrlConverter.asFileUrl(id);
48
93
  const urlObject = new URL(url);
49
94
  urlObject.search = "";
@@ -52,8 +97,12 @@ export const bundleJsModules = async (
52
97
  url: urlWithoutSearch,
53
98
  associations,
54
99
  });
55
- const chunkName = Object.keys(meta).find((key) => meta[key]);
56
- return chunkName || null;
100
+ for (const chunkNameCandidate of Object.keys(meta)) {
101
+ if (meta[chunkNameCandidate]) {
102
+ return chunkNameCandidate;
103
+ }
104
+ }
105
+ return undefined;
57
106
  };
58
107
  }
59
108
 
@@ -91,6 +140,12 @@ export const bundleJsModules = async (
91
140
  // ideally we should disable only for jsenv files
92
141
  return;
93
142
  }
143
+ if (
144
+ warning.code === "INVALID_ANNOTATION" &&
145
+ warning.loc.file.includes("/node_modules/")
146
+ ) {
147
+ return;
148
+ }
94
149
  logger.warn(String(warning));
95
150
  },
96
151
  ...rollupInput,
@@ -180,13 +235,13 @@ const rollupPluginJsenv = ({
180
235
  async buildStart() {
181
236
  _rollupEmitFile = (...args) => this.emitFile(...args);
182
237
  let previousNonEntryPointModuleId;
183
- jsModuleUrlInfos.forEach((jsModuleUrlInfo) => {
238
+ for (const jsModuleUrlInfo of jsModuleUrlInfos) {
184
239
  const id = jsModuleUrlInfo.url;
185
240
  if (jsModuleUrlInfo.isEntryPoint) {
186
241
  emitChunk({
187
242
  id,
188
243
  });
189
- return;
244
+ continue;
190
245
  }
191
246
  let preserveSignature;
192
247
  if (strictExports) {
@@ -218,8 +273,10 @@ const rollupPluginJsenv = ({
218
273
  : null,
219
274
  preserveSignature,
220
275
  });
221
- previousNonEntryPointModuleId = id;
222
- });
276
+ if (jsModuleUrlInfo.originalUrl.startsWith("file:")) {
277
+ previousNonEntryPointModuleId = id;
278
+ }
279
+ }
223
280
  },
224
281
  async generateBundle(outputOptions, rollupResult) {
225
282
  _rollupEmitFile = (...args) => this.emitFile(...args);
@@ -291,16 +348,32 @@ const rollupPluginJsenv = ({
291
348
  remapReference:
292
349
  specifierToUrlMap.size > 0
293
350
  ? (reference) => {
294
- // rollup generate specifiers only for static and dynamic imports
295
- // other references (like new URL()) are ignored
296
- // there is no need to remap them back
297
351
  if (
298
- reference.type === "js_import" &&
299
- reference.subtype !== "import_meta_resolve"
352
+ preserveDynamicImport &&
353
+ reference.subtype === "dynamic_import"
354
+ ) {
355
+ // when dynamic import are preserved, no need to remap them
356
+ return reference.specifier;
357
+ }
358
+ if (
359
+ reference.type !== "js_import" ||
360
+ reference.subtype === "import_meta_resolve"
300
361
  ) {
301
- return specifierToUrlMap.get(reference.specifier);
362
+ // rollup generate specifiers only for static and dynamic imports
363
+ // other references (like new URL()) are ignored
364
+ // there is no need to remap them back
365
+ return reference.specifier;
302
366
  }
303
- return reference.specifier;
367
+ const specifierBeforeRollup = specifierToUrlMap.get(
368
+ reference.specifier,
369
+ );
370
+ if (!specifierBeforeRollup) {
371
+ console.warn(
372
+ `cannot remap "${reference.specifier}" back to specifier before rollup, this is unexpected.`,
373
+ );
374
+ return reference.specifier;
375
+ }
376
+ return specifierBeforeRollup;
304
377
  }
305
378
  : undefined,
306
379
  };
@@ -341,31 +414,30 @@ const rollupPluginJsenv = ({
341
414
  },
342
415
  // https://rollupjs.org/guide/en/#resolvedynamicimport
343
416
  resolveDynamicImport: (specifier, importer) => {
344
- if (preserveDynamicImport) {
345
- let urlObject;
346
- if (specifier[0] === "/") {
347
- urlObject = new URL(specifier.slice(1), rootDirectoryUrl);
348
- } else {
349
- if (isFileSystemPath(importer)) {
350
- importer = fileUrlConverter.asFileUrl(importer);
351
- }
352
- urlObject = new URL(specifier, importer);
353
- }
354
- const searchParamsToAdd =
355
- augmentDynamicImportUrlSearchParams(urlObject);
356
- if (searchParamsToAdd) {
357
- Object.keys(searchParamsToAdd).forEach((key) => {
358
- const value = searchParamsToAdd[key];
359
- if (value === undefined) {
360
- urlObject.searchParams.delete(key);
361
- } else {
362
- urlObject.searchParams.set(key, value);
363
- }
364
- });
417
+ if (!preserveDynamicImport) {
418
+ return null;
419
+ }
420
+ let urlObject;
421
+ if (specifier[0] === "/") {
422
+ urlObject = new URL(specifier.slice(1), rootDirectoryUrl);
423
+ } else {
424
+ if (isFileSystemPath(importer)) {
425
+ importer = fileUrlConverter.asFileUrl(importer);
365
426
  }
366
- return { external: true, id: urlObject.href };
427
+ urlObject = new URL(specifier, importer);
428
+ }
429
+ const searchParamsToAdd = augmentDynamicImportUrlSearchParams(urlObject);
430
+ if (searchParamsToAdd) {
431
+ Object.keys(searchParamsToAdd).forEach((key) => {
432
+ const value = searchParamsToAdd[key];
433
+ if (value === undefined) {
434
+ urlObject.searchParams.delete(key);
435
+ } else {
436
+ urlObject.searchParams.set(key, value);
437
+ }
438
+ });
367
439
  }
368
- return null;
440
+ return { external: true, id: urlObject.href };
369
441
  },
370
442
  resolveId: (specifier, importer = rootDirectoryUrl) => {
371
443
  if (isFileSystemPath(importer)) {