@jsenv/core 40.8.2 → 40.8.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.
@@ -5847,11 +5847,13 @@ const createNodeEsmResolver = ({
5847
5847
  runtimeCompat,
5848
5848
  rootDirectoryUrl,
5849
5849
  packageConditions = {},
5850
+ packageConditionsConfig,
5850
5851
  preservesSymlink,
5851
5852
  }) => {
5852
5853
  const buildPackageConditions = createBuildPackageConditions(
5853
5854
  packageConditions,
5854
5855
  {
5856
+ packageConditionsConfig,
5855
5857
  rootDirectoryUrl,
5856
5858
  runtimeCompat,
5857
5859
  },
@@ -5883,26 +5885,43 @@ const createNodeEsmResolver = ({
5883
5885
  const webResolutionFallback =
5884
5886
  ownerUrlInfo.type !== "js_module" ||
5885
5887
  reference.type === "sourcemap_comment";
5888
+
5889
+ const resolveNodeEsmFallbackOnWeb = createResolverWithFallbackOnError(
5890
+ applyNodeEsmResolution,
5891
+ ({ specifier, parentUrl }) => {
5892
+ const url = new URL(specifier, parentUrl).href;
5893
+ return { url };
5894
+ },
5895
+ );
5896
+ const DELEGATE_TO_WEB_RESOLUTION_PLUGIN = {};
5897
+ const resolveNodeEsmFallbackNullToDelegateToWebPlugin =
5898
+ createResolverWithFallbackOnError(
5899
+ applyNodeEsmResolution,
5900
+
5901
+ () => DELEGATE_TO_WEB_RESOLUTION_PLUGIN,
5902
+ );
5903
+
5886
5904
  const conditions = buildPackageConditions(specifier, parentUrl, {
5887
5905
  webResolutionFallback,
5906
+ resolver: webResolutionFallback
5907
+ ? resolveNodeEsmFallbackOnWeb
5908
+ : applyNodeEsmResolution,
5888
5909
  });
5889
- let resolution;
5890
- const nodeEsmResolutionParams = {
5910
+ const resolver = webResolutionFallback
5911
+ ? resolveNodeEsmFallbackNullToDelegateToWebPlugin
5912
+ : applyNodeEsmResolution;
5913
+
5914
+ const result = resolver({
5891
5915
  conditions,
5892
5916
  parentUrl,
5893
5917
  specifier,
5894
5918
  preservesSymlink,
5895
- };
5896
- if (webResolutionFallback) {
5897
- try {
5898
- resolution = applyNodeEsmResolution(nodeEsmResolutionParams);
5899
- } catch {
5900
- return null; // delegate to web_resolution plugin
5901
- }
5902
- } else {
5903
- resolution = applyNodeEsmResolution(nodeEsmResolutionParams);
5919
+ });
5920
+ if (result === DELEGATE_TO_WEB_RESOLUTION_PLUGIN) {
5921
+ return null;
5904
5922
  }
5905
- const { url, type, isMain, packageDirectoryUrl } = resolution;
5923
+
5924
+ const { url, type, isMain, packageDirectoryUrl } = result;
5906
5925
  // try to give a more meaningful filename after build
5907
5926
  if (isMain && packageDirectoryUrl) {
5908
5927
  const basename = urlToBasename(url);
@@ -5966,147 +5985,232 @@ const createNodeEsmResolver = ({
5966
5985
 
5967
5986
  const createBuildPackageConditions = (
5968
5987
  packageConditions,
5969
- { rootDirectoryUrl, runtimeCompat },
5988
+ { packageConditionsConfig, rootDirectoryUrl, runtimeCompat },
5970
5989
  ) => {
5971
- const nodeRuntimeEnabled = Object.keys(runtimeCompat).includes("node");
5972
- // https://nodejs.org/api/esm.html#resolver-algorithm-specification
5973
- const processArgConditions = readCustomConditionsFromProcessArgs();
5974
- const devResolver = (specifier, importer, { webResolutionFallback }) => {
5975
- if (isBareSpecifier$1(specifier)) {
5976
- let url;
5977
- if (webResolutionFallback) {
5978
- try {
5979
- const resolution = applyNodeEsmResolution({
5980
- specifier,
5981
- parentUrl: importer,
5990
+ let resolveConditionsFromSpecifier = () => null;
5991
+ let resolveConditionsFromContext = () => [];
5992
+ from_specifier: {
5993
+ if (!packageConditionsConfig) {
5994
+ break from_specifier;
5995
+ }
5996
+ const keys = Object.keys(packageConditionsConfig);
5997
+ if (keys.length === 0) {
5998
+ break from_specifier;
5999
+ }
6000
+
6001
+ const associationsRaw = {};
6002
+ for (const key of keys) {
6003
+ const associatedValue = packageConditionsConfig[key];
6004
+
6005
+ if (!isBareSpecifier$1(key)) {
6006
+ const url = new URL(key, rootDirectoryUrl);
6007
+ associationsRaw[url] = associatedValue;
6008
+ continue;
6009
+ }
6010
+ try {
6011
+ if (key.endsWith("/")) {
6012
+ const { packageDirectoryUrl } = applyNodeEsmResolution({
6013
+ specifier: key.slice(0, -1), // avoid package path not exported
6014
+ parentUrl: rootDirectoryUrl,
5982
6015
  });
5983
- url = resolution.url;
5984
- } catch {
5985
- url = new URL(specifier, importer).href;
6016
+ const url = packageDirectoryUrl;
6017
+ associationsRaw[url] = associatedValue;
6018
+ continue;
5986
6019
  }
6020
+ const { url } = applyNodeEsmResolution({
6021
+ specifier: key,
6022
+ parentUrl: rootDirectoryUrl,
6023
+ });
6024
+ associationsRaw[url] = associatedValue;
6025
+ } catch {
6026
+ const url = new URL(key, rootDirectoryUrl);
6027
+ associationsRaw[url] = associatedValue;
6028
+ }
6029
+ }
6030
+ const associations = URL_META.resolveAssociations(
6031
+ {
6032
+ conditions: associationsRaw,
6033
+ },
6034
+ rootDirectoryUrl,
6035
+ );
6036
+ resolveConditionsFromSpecifier = (specifier, importer, { resolver }) => {
6037
+ let associatedValue;
6038
+ if (isBareSpecifier$1(specifier)) {
6039
+ const { url } = resolver({
6040
+ specifier,
6041
+ parentUrl: importer,
6042
+ });
6043
+ associatedValue = URL_META.applyAssociations({ url, associations });
5987
6044
  } else {
5988
- const resolution = applyNodeEsmResolution({
6045
+ associatedValue = URL_META.applyAssociations({
6046
+ url: importer,
6047
+ associations,
6048
+ });
6049
+ }
6050
+ if (!associatedValue) {
6051
+ return undefined;
6052
+ }
6053
+ if (associatedValue.conditions) {
6054
+ return associatedValue.conditions;
6055
+ }
6056
+ return undefined;
6057
+ };
6058
+ }
6059
+ {
6060
+ const nodeRuntimeEnabled = Object.keys(runtimeCompat).includes("node");
6061
+ // https://nodejs.org/api/esm.html#resolver-algorithm-specification
6062
+ const devResolver = (specifier, importer, { resolver }) => {
6063
+ if (isBareSpecifier$1(specifier)) {
6064
+ const { url } = resolver({
5989
6065
  specifier,
5990
6066
  parentUrl: importer,
5991
6067
  });
5992
- url = resolution.url;
6068
+ return !url.includes("/node_modules/");
5993
6069
  }
5994
- return !url.includes("/node_modules/");
5995
- }
5996
- return !importer.includes("/node_modules/");
5997
- };
6070
+ return !importer.includes("/node_modules/");
6071
+ };
5998
6072
 
5999
- const conditionDefaultResolvers = {
6000
- "dev:*": devResolver,
6001
- "development": devResolver,
6002
- "node": nodeRuntimeEnabled,
6003
- "browser": !nodeRuntimeEnabled,
6004
- "import": true,
6005
- };
6006
- const conditionResolvers = {
6007
- ...conditionDefaultResolvers,
6008
- };
6073
+ const conditionDefaultResolvers = {
6074
+ "dev:*": devResolver,
6075
+ "development": devResolver,
6076
+ "node": nodeRuntimeEnabled,
6077
+ "browser": !nodeRuntimeEnabled,
6078
+ "import": true,
6079
+ };
6080
+ const conditionResolvers = {
6081
+ ...conditionDefaultResolvers,
6082
+ };
6009
6083
 
6010
- let wildcardToRemoveSet = new Set();
6011
- const addCustomResolver = (condition, customResolver) => {
6012
- for (const conditionCandidate of Object.keys(conditionDefaultResolvers)) {
6013
- if (conditionCandidate.includes("*")) {
6014
- const conditionRegex = new RegExp(
6015
- `^${conditionCandidate.replace(/\*/g, "(.*)")}$`,
6016
- );
6017
- if (conditionRegex.test(condition)) {
6018
- const existingResolver =
6019
- conditionDefaultResolvers[conditionCandidate];
6020
- wildcardToRemoveSet.add(conditionCandidate);
6021
- conditionResolvers[condition] = combineTwoPackageConditionResolvers(
6022
- existingResolver,
6023
- customResolver,
6084
+ let wildcardToRemoveSet = new Set();
6085
+ const addCustomResolver = (condition, customResolver) => {
6086
+ for (const conditionCandidate of Object.keys(conditionDefaultResolvers)) {
6087
+ if (conditionCandidate.includes("*")) {
6088
+ const conditionRegex = new RegExp(
6089
+ `^${conditionCandidate.replace(/\*/g, "(.*)")}$`,
6024
6090
  );
6025
- return;
6091
+ if (conditionRegex.test(condition)) {
6092
+ const existingResolver =
6093
+ conditionDefaultResolvers[conditionCandidate];
6094
+ wildcardToRemoveSet.add(conditionCandidate);
6095
+ conditionResolvers[condition] = combineTwoPackageConditionResolvers(
6096
+ existingResolver,
6097
+ customResolver,
6098
+ );
6099
+ return;
6100
+ }
6026
6101
  }
6027
6102
  }
6103
+ const existingResolver = conditionDefaultResolvers[condition];
6104
+ if (existingResolver) {
6105
+ conditionResolvers[condition] = combineTwoPackageConditionResolvers(
6106
+ existingResolver,
6107
+ customResolver,
6108
+ );
6109
+ return;
6110
+ }
6111
+ conditionResolvers[condition] = customResolver;
6112
+ };
6113
+ {
6114
+ const processArgConditions = readCustomConditionsFromProcessArgs();
6115
+ for (const processArgCondition of processArgConditions) {
6116
+ addCustomResolver(processArgCondition, true);
6117
+ }
6028
6118
  }
6029
- const existingResolver = conditionDefaultResolvers[condition];
6030
- if (existingResolver) {
6031
- conditionResolvers[condition] = combineTwoPackageConditionResolvers(
6032
- existingResolver,
6033
- customResolver,
6034
- );
6035
- return;
6036
- }
6037
- conditionResolvers[condition] = customResolver;
6038
- };
6039
-
6040
- for (const processArgCondition of processArgConditions) {
6041
- addCustomResolver(processArgCondition, true);
6042
- }
6043
- for (const customCondition of Object.keys(packageConditions)) {
6044
- const value = packageConditions[customCondition];
6045
- let customResolver;
6046
- if (typeof value === "object") {
6047
- const associations = URL_META.resolveAssociations(
6048
- { applies: value },
6049
- (pattern) => {
6050
- if (isBareSpecifier$1(pattern)) {
6051
- try {
6052
- if (pattern.endsWith("/")) {
6053
- // avoid package path not exported
6054
- const { packageDirectoryUrl } = applyNodeEsmResolution({
6055
- specifier: pattern.slice(0, -1),
6056
- parentUrl: rootDirectoryUrl,
6057
- });
6058
- return packageDirectoryUrl;
6119
+ {
6120
+ for (const key of Object.keys(packageConditions)) {
6121
+ const value = packageConditions[key];
6122
+ let customResolver;
6123
+ if (typeof value === "object") {
6124
+ const associations = URL_META.resolveAssociations(
6125
+ { applies: value },
6126
+ (pattern) => {
6127
+ if (isBareSpecifier$1(pattern)) {
6128
+ try {
6129
+ if (pattern.endsWith("/")) {
6130
+ // avoid package path not exported
6131
+ const { packageDirectoryUrl } = applyNodeEsmResolution({
6132
+ specifier: pattern.slice(0, -1),
6133
+ parentUrl: rootDirectoryUrl,
6134
+ });
6135
+ return packageDirectoryUrl;
6136
+ }
6137
+ const { url } = applyNodeEsmResolution({
6138
+ specifier: pattern,
6139
+ parentUrl: rootDirectoryUrl,
6140
+ });
6141
+ return url;
6142
+ } catch {
6143
+ return new URL(pattern, rootDirectoryUrl);
6144
+ }
6059
6145
  }
6060
- const { url } = applyNodeEsmResolution({
6061
- specifier: pattern,
6062
- parentUrl: rootDirectoryUrl,
6063
- });
6064
- return url;
6065
- } catch {
6066
6146
  return new URL(pattern, rootDirectoryUrl);
6147
+ },
6148
+ );
6149
+ customResolver = (specifier, importer, { resolver }) => {
6150
+ if (isBareSpecifier$1(specifier)) {
6151
+ const { url } = resolver({
6152
+ specifier,
6153
+ parentUrl: importer,
6154
+ });
6155
+ const { applies } = URL_META.applyAssociations({
6156
+ url,
6157
+ associations,
6158
+ });
6159
+ return applies;
6067
6160
  }
6068
- }
6069
- return new URL(pattern, rootDirectoryUrl);
6070
- },
6071
- );
6072
- customResolver = (specifier, importer) => {
6073
- if (isBareSpecifier$1(specifier)) {
6074
- const { url } = applyNodeEsmResolution({
6075
- specifier,
6076
- parentUrl: importer,
6077
- });
6078
- const { applies } = URL_META.applyAssociations({ url, associations });
6079
- return applies;
6161
+ const { applies } = URL_META.applyAssociations({
6162
+ url: importer,
6163
+ associations,
6164
+ });
6165
+ return applies;
6166
+ };
6167
+ } else if (typeof value === "function") {
6168
+ customResolver = value;
6169
+ } else {
6170
+ customResolver = value;
6080
6171
  }
6081
- return URL_META.applyAssociations({ url: importer, associations })
6082
- .applies;
6083
- };
6084
- } else if (typeof value === "function") {
6085
- customResolver = value;
6086
- } else {
6087
- customResolver = value;
6172
+ addCustomResolver(key, customResolver);
6173
+ }
6174
+ }
6175
+ for (const wildcardToRemove of wildcardToRemoveSet) {
6176
+ delete conditionResolvers[wildcardToRemove];
6088
6177
  }
6089
- addCustomResolver(customCondition, customResolver);
6090
- }
6091
-
6092
- for (const wildcardToRemove of wildcardToRemoveSet) {
6093
- delete conditionResolvers[wildcardToRemove];
6094
- }
6095
6178
 
6096
- const conditionCandidateArray = Object.keys(conditionResolvers);
6097
- return (specifier, importer, params) => {
6098
- const conditions = [];
6099
- for (const conditionCandidate of conditionCandidateArray) {
6100
- const conditionResolver = conditionResolvers[conditionCandidate];
6101
- if (typeof conditionResolver === "function") {
6102
- if (conditionResolver(specifier, importer, params)) {
6179
+ const conditionCandidateArray = Object.keys(conditionResolvers);
6180
+ resolveConditionsFromContext = (specifier, importer, params) => {
6181
+ const conditions = [];
6182
+ for (const conditionCandidate of conditionCandidateArray) {
6183
+ const conditionResolver = conditionResolvers[conditionCandidate];
6184
+ if (typeof conditionResolver === "function") {
6185
+ if (conditionResolver(specifier, importer, params)) {
6186
+ conditions.push(conditionCandidate);
6187
+ }
6188
+ } else if (conditionResolver) {
6103
6189
  conditions.push(conditionCandidate);
6104
6190
  }
6105
- } else if (conditionResolver) {
6106
- conditions.push(conditionCandidate);
6107
6191
  }
6192
+ return conditions;
6193
+ };
6194
+ }
6195
+
6196
+ return (specifier, importer, params) => {
6197
+ const conditionsForThisSpecifier = resolveConditionsFromSpecifier(
6198
+ specifier,
6199
+ importer,
6200
+ params,
6201
+ );
6202
+ if (conditionsForThisSpecifier) {
6203
+ return conditionsForThisSpecifier;
6108
6204
  }
6109
- return conditions;
6205
+ const conditionsFromContext = resolveConditionsFromContext(
6206
+ specifier,
6207
+ importer,
6208
+ params,
6209
+ );
6210
+ if (conditionsFromContext) {
6211
+ return conditionsFromContext;
6212
+ }
6213
+ return [];
6110
6214
  };
6111
6215
  };
6112
6216
 
@@ -6160,6 +6264,16 @@ const addRelationshipWithPackageJson = ({
6160
6264
  }
6161
6265
  };
6162
6266
 
6267
+ const createResolverWithFallbackOnError = (mainResolver, fallbackResolver) => {
6268
+ return (params) => {
6269
+ try {
6270
+ return mainResolver(params);
6271
+ } catch {
6272
+ return fallbackResolver(params);
6273
+ }
6274
+ };
6275
+ };
6276
+
6163
6277
  const isBareSpecifier$1 = (specifier) => {
6164
6278
  if (
6165
6279
  specifier[0] === "/" ||
@@ -6180,6 +6294,7 @@ const isBareSpecifier$1 = (specifier) => {
6180
6294
  const jsenvPluginNodeEsmResolution = (
6181
6295
  resolutionConfig = {},
6182
6296
  packageConditions,
6297
+ packageConditionsConfig = {},
6183
6298
  ) => {
6184
6299
  let nodeEsmResolverDefault;
6185
6300
  const resolverMap = new Map();
@@ -6202,6 +6317,10 @@ const jsenvPluginNodeEsmResolution = (
6202
6317
  runtimeCompat: kitchenContext.runtimeCompat,
6203
6318
  rootDirectoryUrl: kitchenContext.rootDirectoryUrl,
6204
6319
  packageConditions,
6320
+ packageConditionsConfig: {
6321
+ ...kitchenContext.packageConditionsConfig,
6322
+ ...packageConditionsConfig,
6323
+ },
6205
6324
  preservesSymlink,
6206
6325
  });
6207
6326
  };
@@ -6216,6 +6335,10 @@ const jsenvPluginNodeEsmResolution = (
6216
6335
  rootDirectoryUrl: kitchenContext.rootDirectoryUrl,
6217
6336
  preservesSymlink: true,
6218
6337
  packageConditions,
6338
+ packageConditionsConfig: {
6339
+ ...kitchenContext.packageConditionsConfig,
6340
+ ...packageConditionsConfig,
6341
+ },
6219
6342
  });
6220
6343
  for (const urlType of Object.keys(resolutionConfig)) {
6221
6344
  let resolver;
@@ -8967,6 +9090,7 @@ const getCorePlugins = ({
8967
9090
  referenceAnalysis = {},
8968
9091
  nodeEsmResolution = {},
8969
9092
  packageConditions,
9093
+ packageConditionsConfig,
8970
9094
  magicExtensions,
8971
9095
  magicDirectoryIndex,
8972
9096
  directoryListing = true,
@@ -9042,7 +9166,13 @@ const getCorePlugins = ({
9042
9166
  },
9043
9167
  },
9044
9168
  ...(nodeEsmResolution
9045
- ? [jsenvPluginNodeEsmResolution(nodeEsmResolution, packageConditions)]
9169
+ ? [
9170
+ jsenvPluginNodeEsmResolution(
9171
+ nodeEsmResolution,
9172
+ packageConditions,
9173
+ packageConditionsConfig,
9174
+ ),
9175
+ ]
9046
9176
  : []),
9047
9177
  jsenvPluginWebResolution(),
9048
9178
  jsenvPluginDirectoryReferenceEffect(directoryReferenceEffect, {
@@ -11937,6 +12067,7 @@ const entryPointDefaultParams = {
11937
12067
  referenceAnalysis: {},
11938
12068
  nodeEsmResolution: undefined,
11939
12069
  packageConditions: undefined,
12070
+ packageConditionsConfig: undefined,
11940
12071
  magicExtensions: undefined,
11941
12072
  magicDirectoryIndex: undefined,
11942
12073
  directoryReferenceEffect: undefined,
@@ -11989,6 +12120,7 @@ const prepareEntryPointBuild = async (
11989
12120
  referenceAnalysis,
11990
12121
  nodeEsmResolution,
11991
12122
  packageConditions,
12123
+ packageConditionsConfig,
11992
12124
  magicExtensions,
11993
12125
  magicDirectoryIndex,
11994
12126
  directoryReferenceEffect,
@@ -12140,6 +12272,7 @@ const prepareEntryPointBuild = async (
12140
12272
  referenceAnalysis,
12141
12273
  nodeEsmResolution,
12142
12274
  packageConditions,
12275
+ packageConditionsConfig,
12143
12276
  magicExtensions,
12144
12277
  magicDirectoryIndex,
12145
12278
  directoryReferenceEffect,
@@ -6142,7 +6142,7 @@ const mainLegacyResolvers = {
6142
6142
  };
6143
6143
  },
6144
6144
  node: ({ packageJson, conditions }) => {
6145
- if (conditions.includes("import")) {
6145
+ if (conditions.includes("import") && !conditions.includes("require")) {
6146
6146
  if (typeof packageJson.module === "string") {
6147
6147
  return { type: "field:module", isMain: true, path: packageJson.module };
6148
6148
  }
@@ -6156,6 +6156,7 @@ const mainLegacyResolvers = {
6156
6156
  return null;
6157
6157
  },
6158
6158
  };
6159
+ mainLegacyResolvers.require = mainLegacyResolvers.node;
6159
6160
 
6160
6161
  const comparePatternKeys = (keyA, keyB) => {
6161
6162
  if (!keyA.endsWith("/") && !keyA.includes("*")) {
@@ -4776,7 +4776,7 @@ const mainLegacyResolvers = {
4776
4776
  };
4777
4777
  },
4778
4778
  node: ({ packageJson, conditions }) => {
4779
- if (conditions.includes("import")) {
4779
+ if (conditions.includes("import") && !conditions.includes("require")) {
4780
4780
  if (typeof packageJson.module === "string") {
4781
4781
  return { type: "field:module", isMain: true, path: packageJson.module };
4782
4782
  }
@@ -4790,6 +4790,7 @@ const mainLegacyResolvers = {
4790
4790
  return null;
4791
4791
  },
4792
4792
  };
4793
+ mainLegacyResolvers.require = mainLegacyResolvers.node;
4793
4794
 
4794
4795
  const comparePatternKeys = (keyA, keyB) => {
4795
4796
  if (!keyA.endsWith("/") && !keyA.includes("*")) {