@jsenv/node-esm-resolution 1.1.0 → 1.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/node-esm-resolution",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,5 +22,6 @@
22
22
  "/dist/",
23
23
  "/src/",
24
24
  "/main.js"
25
- ]
25
+ ],
26
+ "sideEffects": false
26
27
  }
@@ -1,5 +1,8 @@
1
1
  // https://nodejs.org/api/packages.html#resolving-user-conditions
2
2
  export const readCustomConditionsFromProcessArgs = () => {
3
+ if (process.env.IGNORE_PACKAGE_CONDITIONS) {
4
+ return [];
5
+ }
3
6
  const packageConditions = [];
4
7
  for (const arg of process.execArgv) {
5
8
  if (arg.includes("-C=")) {
@@ -17,6 +17,7 @@ const NODE_BUILTIN_MODULE_SPECIFIERS = [
17
17
  "constants",
18
18
  "crypto",
19
19
  "_debugger",
20
+ "diagnostics_channel",
20
21
  "dgram",
21
22
  "dns",
22
23
  "domain",
@@ -50,6 +51,7 @@ const NODE_BUILTIN_MODULE_SPECIFIERS = [
50
51
  "readline",
51
52
  "repl",
52
53
  "smalloc",
54
+ "sqlite",
53
55
  "_stream_duplex",
54
56
  "_stream_transform",
55
57
  "_stream_wrap",
@@ -154,6 +154,7 @@ const applyBrowserFieldResolution = (specifier, resolutionContext) => {
154
154
  if (url) {
155
155
  return {
156
156
  type: "field:browser",
157
+ isMain: true,
157
158
  packageDirectoryUrl,
158
159
  packageJson,
159
160
  url,
@@ -197,7 +198,7 @@ const applyPackageImportsResolution = (
197
198
  };
198
199
 
199
200
  const applyPackageResolve = (packageSpecifier, resolutionContext) => {
200
- const { parentUrl, conditions, readPackageJson, preservesSymlink } =
201
+ const { parentUrl, conditions, readPackageJson, preservesSymlink, isImport } =
201
202
  resolutionContext;
202
203
  if (packageSpecifier === "") {
203
204
  throw new Error("invalid module specifier");
@@ -223,7 +224,7 @@ const applyPackageResolve = (packageSpecifier, resolutionContext) => {
223
224
  resolutionContext,
224
225
  );
225
226
  }
226
- if (packageSubpath.endsWith("/")) {
227
+ if (isImport && packageSubpath.endsWith("/")) {
227
228
  throw new Error("invalid module specifier");
228
229
  }
229
230
  const questionCharIndex = packageName.indexOf("?");
@@ -404,6 +405,7 @@ const applyPackageTargetResolution = (target, resolutionContext) => {
404
405
  }
405
406
  return {
406
407
  type: isImport ? "field:imports" : "field:exports",
408
+ isMain: subpath === "" || subpath === ".",
407
409
  packageDirectoryUrl,
408
410
  packageJson,
409
411
  url: pattern
@@ -468,7 +470,27 @@ const applyPackageTargetResolution = (target, resolutionContext) => {
468
470
  if (Number.isInteger(key)) {
469
471
  throw new Error("Invalid package configuration");
470
472
  }
471
- if (key === "default" || conditions.includes(key)) {
473
+ let matched;
474
+ if (key === "default") {
475
+ matched = true;
476
+ } else {
477
+ for (const conditionCandidate of conditions) {
478
+ if (conditionCandidate === key) {
479
+ matched = true;
480
+ break;
481
+ }
482
+ if (conditionCandidate.includes("*")) {
483
+ const conditionCandidateRegex = new RegExp(
484
+ `^${conditionCandidate.replace(/\*/g, "(.*)")}$`,
485
+ );
486
+ if (conditionCandidateRegex.test(key)) {
487
+ matched = true;
488
+ break;
489
+ }
490
+ }
491
+ }
492
+ }
493
+ if (matched) {
472
494
  const targetValue = target[key];
473
495
  const resolved = applyPackageTargetResolution(targetValue, {
474
496
  ...resolutionContext,
@@ -566,6 +588,12 @@ const parsePackageSpecifier = (packageSpecifier) => {
566
588
  }
567
589
  const packageName = packageSpecifier.slice(0, firstSlashIndex);
568
590
  const afterFirstSlash = packageSpecifier.slice(firstSlashIndex + 1);
591
+ if (afterFirstSlash === "") {
592
+ return {
593
+ packageName,
594
+ packageSubpath: "/",
595
+ };
596
+ }
569
597
  const packageSubpath = `./${afterFirstSlash}`;
570
598
  return {
571
599
  packageName,
@@ -606,6 +634,7 @@ const applyLegacySubpathResolution = (packageSubpath, resolutionContext) => {
606
634
  }
607
635
  return {
608
636
  type: "subpath",
637
+ isMain: packageSubpath === ".",
609
638
  packageDirectoryUrl,
610
639
  packageJson,
611
640
  url: new URL(packageSubpath, packageDirectoryUrl).href,
@@ -623,6 +652,7 @@ const applyLegacyMainResolution = (packageSubpath, resolutionContext) => {
623
652
  if (resolved) {
624
653
  return {
625
654
  type: resolved.type,
655
+ isMain: resolved.isMain,
626
656
  packageDirectoryUrl,
627
657
  packageJson,
628
658
  url: new URL(resolved.path, packageDirectoryUrl).href,
@@ -631,6 +661,7 @@ const applyLegacyMainResolution = (packageSubpath, resolutionContext) => {
631
661
  }
632
662
  return {
633
663
  type: "field:main", // the absence of "main" field
664
+ isMain: true,
634
665
  packageDirectoryUrl,
635
666
  packageJson,
636
667
  url: new URL("index.js", packageDirectoryUrl).href,
@@ -639,13 +670,13 @@ const applyLegacyMainResolution = (packageSubpath, resolutionContext) => {
639
670
  const mainLegacyResolvers = {
640
671
  import: ({ packageJson }) => {
641
672
  if (typeof packageJson.module === "string") {
642
- return { type: "field:module", path: packageJson.module };
673
+ return { type: "field:module", isMain: true, path: packageJson.module };
643
674
  }
644
675
  if (typeof packageJson.jsnext === "string") {
645
- return { type: "field:jsnext", path: packageJson.jsnext };
676
+ return { type: "field:jsnext", isMain: true, path: packageJson.jsnext };
646
677
  }
647
678
  if (typeof packageJson.main === "string") {
648
- return { type: "field:main", path: packageJson.main };
679
+ return { type: "field:main", isMain: true, path: packageJson.main };
649
680
  }
650
681
  return null;
651
682
  },
@@ -667,6 +698,7 @@ const mainLegacyResolvers = {
667
698
  if (typeof packageJson.module === "string") {
668
699
  return {
669
700
  type: "field:module",
701
+ isMain: true,
670
702
  path: packageJson.module,
671
703
  };
672
704
  }
@@ -678,6 +710,7 @@ const mainLegacyResolvers = {
678
710
  ) {
679
711
  return {
680
712
  type: "field:browser",
713
+ isMain: true,
681
714
  path: browserMain,
682
715
  };
683
716
  }
@@ -690,20 +723,27 @@ const mainLegacyResolvers = {
690
723
  ) {
691
724
  return {
692
725
  type: "field:module",
726
+ isMain: true,
693
727
  path: packageJson.module,
694
728
  };
695
729
  }
696
730
  return {
697
731
  type: "field:browser",
732
+ isMain: true,
698
733
  path: browserMain,
699
734
  };
700
735
  },
701
- node: ({ packageJson }) => {
736
+ node: ({ packageJson, conditions }) => {
737
+ if (conditions.includes("import")) {
738
+ if (typeof packageJson.module === "string") {
739
+ return { type: "field:module", isMain: true, path: packageJson.module };
740
+ }
741
+ if (typeof packageJson.jsnext === "string") {
742
+ return { type: "field:jsnext", isMain: true, path: packageJson.jsnext };
743
+ }
744
+ }
702
745
  if (typeof packageJson.main === "string") {
703
- return {
704
- type: "field:main",
705
- path: packageJson.main,
706
- };
746
+ return { type: "field:main", isMain: true, path: packageJson.main };
707
747
  }
708
748
  return null;
709
749
  },