@jsenv/plugin-bundling 2.8.8 → 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.8",
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;
316
+ if (urlToExtension(url) === ".css") {
317
+ return true;
373
318
  }
374
- if (typeof closestPackageJsonSideEffects === "function") {
375
- const haveSideEffect = closestPackageJsonSideEffects(url);
376
- // if (haveSideEffect) {
377
- // console.log(`have side effect: ${url}`);
378
- // }
379
- return haveSideEffect;
319
+ const urlInfo = graph.getUrlInfo(url);
320
+ if (!urlInfo) {
321
+ return null; // we don't know
380
322
  }
381
- return closestPackageJsonSideEffects;
382
- };
383
- const getModuleSideEffects = (url, importer) => {
384
- if (!url.startsWith("ignore:")) {
385
- return inferSideEffectsFromResolvedUrl(url);
323
+ if (urlInfo.contentSideEffects.length === 0) {
324
+ return null; // we don't know
386
325
  }
387
- url = url.slice("ignore:".length);
388
- if (url.startsWith("file:")) {
389
- return inferSideEffectsFromResolvedUrl(url);
390
- }
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)
@@ -654,10 +584,7 @@ const rollupPluginJsenv = ({
654
584
  }
655
585
  if (chunkInfo.isDynamicEntry) {
656
586
  const originalFileUrl = getOriginalUrl(chunkInfo, true);
657
- const urlInfo =
658
- jsModuleUrlInfos[0].context.kitchen.graph.getUrlInfo(
659
- originalFileUrl,
660
- );
587
+ const urlInfo = kitchen.graph.getUrlInfo(originalFileUrl);
661
588
  if (urlInfo && urlInfo.filenameHint) {
662
589
  return urlInfo.filenameHint;
663
590
  }
@@ -676,33 +603,50 @@ const rollupPluginJsenv = ({
676
603
  if (isFileSystemPath(importer)) {
677
604
  importer = PATH_AND_URL_CONVERTER.asFileUrl(importer);
678
605
  }
679
- const urlObject = resolveImport(specifier, importer);
680
- if (!urlObject.href.startsWith("file:")) {
681
- 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
+ };
682
614
  }
683
615
  const searchParamsToAdd =
684
- augmentDynamicImportUrlSearchParams(urlObject);
616
+ augmentDynamicImportUrlSearchParams(resolvedUrlObject);
685
617
  if (searchParamsToAdd) {
686
- injectQueryParams(urlObject, searchParamsToAdd);
618
+ injectQueryParams(resolvedUrlObject, searchParamsToAdd);
687
619
  }
688
- const id = urlObject.href;
689
- return { id, external: true };
620
+ const id = resolvedUrlObject.href;
621
+ return {
622
+ id,
623
+ external: true,
624
+ moduleSideEffects: getModuleSideEffects(id, importer),
625
+ };
690
626
  }
691
627
  if (isolateDynamicImports) {
692
628
  if (isFileSystemPath(importer)) {
693
629
  importer = PATH_AND_URL_CONVERTER.asFileUrl(importer);
694
630
  }
695
- const urlObject = resolveImport(specifier, importer);
696
- if (!urlObject.href.startsWith("file:")) {
697
- 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
+ };
698
639
  }
699
- const importId = assignDynamicImportId(urlObject.href);
700
- injectQueryParams(urlObject, {
640
+ const importId = assignDynamicImportId(resolvedUrlObject.href);
641
+ injectQueryParams(resolvedUrlObject, {
701
642
  dynamic_import_id: importId,
702
643
  });
703
- const url = urlObject.href;
644
+ const url = resolvedUrlObject.href;
704
645
  const filePath = PATH_AND_URL_CONVERTER.asFilePath(url);
705
- return { id: filePath };
646
+ return {
647
+ id: filePath,
648
+ moduleSideEffects: getModuleSideEffects(url, importer),
649
+ };
706
650
  }
707
651
  return null;
708
652
  },