@jsenv/plugin-bundling 2.8.7 → 2.9.3

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.8.7",
3
+ "version": "2.9.3",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -20,14 +20,17 @@
20
20
  "files": [
21
21
  "/src/"
22
22
  ],
23
- "sideEffects": false,
23
+ "sideEffects": [
24
+ "./tests/chunks_vendors/snapshots/0_default/jsenv_plugin_bundling_node_modules.js",
25
+ "./tests/chunks_vendors/snapshots/1_vendors/vendors.js"
26
+ ],
24
27
  "dependencies": {
25
- "@jsenv/humanize": "1.4.0",
28
+ "@jsenv/humanize": "1.5.0",
26
29
  "@jsenv/url-meta": "8.5.6",
27
- "@jsenv/urls": "2.7.1",
28
- "@jsenv/sourcemap": "1.3.4",
29
- "lightningcss": "1.29.2",
30
- "rollup": "4.35.0"
30
+ "@jsenv/urls": "2.7.2",
31
+ "@jsenv/sourcemap": "1.3.5",
32
+ "lightningcss": "1.29.3",
33
+ "rollup": "4.40.0"
31
34
  },
32
35
  "peerDependencies": {
33
36
  "@jsenv/core": "*"
@@ -1,4 +1,3 @@
1
- import { lookupPackageDirectory, readPackageAtOrNull } from "@jsenv/filesystem";
2
1
  import { createDetailedMessage } from "@jsenv/humanize";
3
2
  import { isSpecifierForNodeBuiltin } from "@jsenv/node-esm-resolution/src/node_builtin_specifiers.js";
4
3
  import { sourcemapConverter } from "@jsenv/sourcemap";
@@ -7,9 +6,9 @@ import {
7
6
  injectQueryParams,
8
7
  isFileSystemPath,
9
8
  urlToBasename,
9
+ urlToExtension,
10
10
  urlToRelativeUrl,
11
11
  } from "@jsenv/urls";
12
- import { readFileSync } from "node:fs";
13
12
  import { fileUrlConverter } from "../file_url_converter.js";
14
13
 
15
14
  export const bundleJsModules = async (
@@ -32,6 +31,7 @@ export const bundleJsModules = async (
32
31
  signal,
33
32
  logger,
34
33
  rootDirectoryUrl,
34
+ packageDirectory,
35
35
  runtimeCompat,
36
36
  sourcemaps,
37
37
  isSupportedOnCurrentClients,
@@ -73,9 +73,8 @@ export const bundleJsModules = async (
73
73
  if (chunks) {
74
74
  let workspaces;
75
75
  let packageName;
76
- const packageDirectoryUrl = lookupPackageDirectory(rootDirectoryUrl);
77
- if (packageDirectoryUrl) {
78
- const packageJSON = readPackageAtOrNull(packageDirectoryUrl);
76
+ if (packageDirectory.url) {
77
+ const packageJSON = packageDirectory.read(packageDirectory.url);
79
78
  if (packageJSON) {
80
79
  packageName = packageJSON.name;
81
80
  workspaces = packageJSON.workspaces;
@@ -106,7 +105,7 @@ export const bundleJsModules = async (
106
105
  for (const workspace of workspaces) {
107
106
  const workspacePattern = new URL(
108
107
  workspace.endsWith("/*") ? workspace.slice(0, -1) : workspace,
109
- packageDirectoryUrl,
108
+ packageDirectory.url,
110
109
  ).href;
111
110
  workspacePatterns[workspacePattern] = true;
112
111
  }
@@ -307,96 +306,29 @@ const rollupPluginJsenv = ({
307
306
  return new URL(rollupFileInfo.fileName, buildDirectoryUrl).href;
308
307
  };
309
308
 
310
- const packageSideEffectsCacheMap = new Map();
311
- const readClosestPackageJsonSideEffects = (url) => {
312
- const packageDirectoryUrl = lookupPackageDirectory(url);
313
- if (!packageDirectoryUrl) {
314
- return undefined;
315
- }
316
- const fromCache = packageSideEffectsCacheMap.get(packageDirectoryUrl);
317
- if (fromCache) {
318
- return fromCache.value;
319
- }
320
- try {
321
- const packageFileContent = readFileSync(
322
- new URL("./package.json", packageDirectoryUrl),
323
- "utf8",
324
- );
325
- const packageJSON = JSON.parse(packageFileContent);
326
- return storePackageSideEffect(packageDirectoryUrl, packageJSON);
327
- } catch {
328
- return storePackageSideEffect(packageDirectoryUrl, null);
329
- }
330
- };
331
- const storePackageSideEffect = (packageDirectoryUrl, packageJson) => {
332
- if (!packageJson) {
333
- packageSideEffectsCacheMap.set(packageDirectoryUrl, { value: undefined });
334
- return undefined;
335
- }
336
- const value = packageJson.sideEffects;
337
- if (Array.isArray(value)) {
338
- const sideEffectPatterns = {};
339
- for (const v of value) {
340
- sideEffectPatterns[v] = true;
341
- }
342
- const associations = URL_META.resolveAssociations(
343
- { sideEffects: sideEffectPatterns },
344
- packageDirectoryUrl,
345
- );
346
- const isMatching = (url) => {
347
- const meta = URL_META.applyAssociations({ url, associations });
348
- return meta.sideEffects || false;
349
- };
350
- packageSideEffectsCacheMap.set(packageDirectoryUrl, {
351
- value: isMatching,
352
- });
353
- return isMatching;
354
- }
355
- packageSideEffectsCacheMap.set(packageDirectoryUrl, { value });
356
- return value;
357
- };
358
-
359
- const inferSideEffectsFromResolvedUrl = (url) => {
309
+ const getModuleSideEffects = (url) => {
360
310
  if (url.startsWith("ignore:")) {
361
- // console.log(`may have side effect: ${url}`);
362
- // double ignore we must keep the import
363
- return null;
311
+ url = url.slice("ignore:".length);
364
312
  }
365
313
  if (isSpecifierForNodeBuiltin(url)) {
366
314
  return false;
367
315
  }
368
- const closestPackageJsonSideEffects =
369
- readClosestPackageJsonSideEffects(url);
370
- if (closestPackageJsonSideEffects === undefined) {
371
- // console.log(`may have side effect: ${url}`);
372
- return null;
373
- }
374
- if (typeof closestPackageJsonSideEffects === "function") {
375
- const haveSideEffect = closestPackageJsonSideEffects(url);
376
- // if (haveSideEffect) {
377
- // console.log(`have side effect: ${url}`);
378
- // }
379
- return haveSideEffect;
316
+ if (urlToExtension(url) === ".css") {
317
+ return true;
380
318
  }
381
- return closestPackageJsonSideEffects;
382
- };
383
- const getModuleSideEffects = (url, importer) => {
384
- if (!url.startsWith("ignore:")) {
385
- return inferSideEffectsFromResolvedUrl(url);
319
+ const urlInfo = graph.getUrlInfo(url);
320
+ if (!urlInfo) {
321
+ return null; // we don't know
386
322
  }
387
- url = url.slice("ignore:".length);
388
- if (url.startsWith("file:")) {
389
- return inferSideEffectsFromResolvedUrl(url);
323
+ if (urlInfo.contentSideEffects.length === 0) {
324
+ return null; // we don't know
390
325
  }
391
- try {
392
- const result = kitchen.resolve(url, importer);
393
- if (result.packageDirectoryUrl) {
394
- storePackageSideEffect(result.packageDirectoryUrl, result.packageJson);
326
+ for (const contentSideEffect of urlInfo.contentSideEffects) {
327
+ if (contentSideEffect.has) {
328
+ return true;
395
329
  }
396
- return inferSideEffectsFromResolvedUrl(url);
397
- } catch {
398
- return null;
399
330
  }
331
+ return false;
400
332
  };
401
333
 
402
334
  const resolveImport = (specifier, importer) => {
@@ -408,9 +340,7 @@ const rollupPluginJsenv = ({
408
340
 
409
341
  const dynamicImportIdSet = new Set();
410
342
  const assignDynamicImportId = (urlImportedDynamically) => {
411
- const urlInfo = jsModuleUrlInfos[0].context.kitchen.graph.getUrlInfo(
412
- urlImportedDynamically,
413
- );
343
+ const urlInfo = kitchen.graph.getUrlInfo(urlImportedDynamically);
414
344
  let dynamicImportIdBase =
415
345
  urlInfo && urlInfo.filenameHint
416
346
  ? filenameWithoutExtension(urlInfo.filenameHint)
@@ -528,7 +458,22 @@ const rollupPluginJsenv = ({
528
458
  const dynamicImportUrl = getOriginalUrl(
529
459
  dynamicImportRollupFileInfo,
530
460
  );
531
- const rollupSpecifier = `./${dynamicImportRollupFileInfo.fileName}`;
461
+ const importerBuildUrl = new URL(
462
+ rollupFileInfo.fileName,
463
+ buildDirectoryUrl,
464
+ ).href;
465
+ const urlToImport = new URL(
466
+ dynamicImportRollupFileInfo.fileName,
467
+ buildDirectoryUrl,
468
+ ).href;
469
+ const specifierRelative = urlToRelativeUrl(
470
+ urlToImport,
471
+ importerBuildUrl,
472
+ );
473
+ const rollupSpecifier =
474
+ specifierRelative[0] === "."
475
+ ? specifierRelative
476
+ : `./${specifierRelative}`;
532
477
  specifierToUrlMap.set(rollupSpecifier, dynamicImportUrl);
533
478
  }
534
479
  }
@@ -639,10 +584,7 @@ const rollupPluginJsenv = ({
639
584
  }
640
585
  if (chunkInfo.isDynamicEntry) {
641
586
  const originalFileUrl = getOriginalUrl(chunkInfo, true);
642
- const urlInfo =
643
- jsModuleUrlInfos[0].context.kitchen.graph.getUrlInfo(
644
- originalFileUrl,
645
- );
587
+ const urlInfo = kitchen.graph.getUrlInfo(originalFileUrl);
646
588
  if (urlInfo && urlInfo.filenameHint) {
647
589
  return urlInfo.filenameHint;
648
590
  }
@@ -661,33 +603,50 @@ const rollupPluginJsenv = ({
661
603
  if (isFileSystemPath(importer)) {
662
604
  importer = PATH_AND_URL_CONVERTER.asFileUrl(importer);
663
605
  }
664
- const urlObject = resolveImport(specifier, importer);
665
- if (!urlObject.href.startsWith("file:")) {
666
- return { id: specifier, external: true };
606
+ const resolvedUrlObject = resolveImport(specifier, importer);
607
+ const resolvedUrl = resolvedUrlObject.href;
608
+ if (!resolvedUrl.startsWith("file:")) {
609
+ return {
610
+ id: specifier,
611
+ external: true,
612
+ moduleSideEffects: getModuleSideEffects(resolvedUrl, importer),
613
+ };
667
614
  }
668
615
  const searchParamsToAdd =
669
- augmentDynamicImportUrlSearchParams(urlObject);
616
+ augmentDynamicImportUrlSearchParams(resolvedUrlObject);
670
617
  if (searchParamsToAdd) {
671
- injectQueryParams(urlObject, searchParamsToAdd);
618
+ injectQueryParams(resolvedUrlObject, searchParamsToAdd);
672
619
  }
673
- const id = urlObject.href;
674
- return { id, external: true };
620
+ const id = resolvedUrlObject.href;
621
+ return {
622
+ id,
623
+ external: true,
624
+ moduleSideEffects: getModuleSideEffects(id, importer),
625
+ };
675
626
  }
676
627
  if (isolateDynamicImports) {
677
628
  if (isFileSystemPath(importer)) {
678
629
  importer = PATH_AND_URL_CONVERTER.asFileUrl(importer);
679
630
  }
680
- const urlObject = resolveImport(specifier, importer);
681
- if (!urlObject.href.startsWith("file:")) {
682
- return { id: specifier, external: true };
631
+ const resolvedUrlObject = resolveImport(specifier, importer);
632
+ const resolvedUrl = resolvedUrlObject.href;
633
+ if (!resolvedUrl.startsWith("file:")) {
634
+ return {
635
+ id: specifier,
636
+ external: true,
637
+ moduleSideEffects: getModuleSideEffects(resolvedUrl, importer),
638
+ };
683
639
  }
684
- const importId = assignDynamicImportId(urlObject.href);
685
- injectQueryParams(urlObject, {
640
+ const importId = assignDynamicImportId(resolvedUrlObject.href);
641
+ injectQueryParams(resolvedUrlObject, {
686
642
  dynamic_import_id: importId,
687
643
  });
688
- const url = urlObject.href;
644
+ const url = resolvedUrlObject.href;
689
645
  const filePath = PATH_AND_URL_CONVERTER.asFilePath(url);
690
- return { id: filePath };
646
+ return {
647
+ id: filePath,
648
+ moduleSideEffects: getModuleSideEffects(url, importer),
649
+ };
691
650
  }
692
651
  return null;
693
652
  },