@jsenv/core 40.3.3 → 40.5.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.
@@ -4,18 +4,18 @@ import { jsenvPluginMinification } from "@jsenv/plugin-minification";
4
4
  import { jsenvPluginTranspilation, jsenvPluginJsModuleFallback } from "@jsenv/plugin-transpilation";
5
5
  import { memoryUsage } from "node:process";
6
6
  import { readFileSync, existsSync, readdirSync, lstatSync, realpathSync } from "node:fs";
7
- import { lookupPackageDirectory$1 as lookupPackageDirectory, registerDirectoryLifecycle$1 as registerDirectoryLifecycle, urlToRelativeUrl$1 as urlToRelativeUrl, createDetailedMessage$1 as createDetailedMessage, stringifyUrlSite$1 as stringifyUrlSite, generateContentFrame$1 as generateContentFrame, validateResponseIntegrity$1 as validateResponseIntegrity, urlIsInsideOf$1 as urlIsInsideOf, ensureWindowsDriveLetter$1 as ensureWindowsDriveLetter, setUrlFilename$1 as setUrlFilename, moveUrl$1 as moveUrl, getCallerPosition$1 as getCallerPosition, urlToBasename$1 as urlToBasename, urlToExtension$1 as urlToExtension, asSpecifierWithoutSearch$1 as asSpecifierWithoutSearch, asUrlWithoutSearch$1 as asUrlWithoutSearch, injectQueryParamsIntoSpecifier$1 as injectQueryParamsIntoSpecifier, bufferToEtag$1 as bufferToEtag, isFileSystemPath$1 as isFileSystemPath, urlToPathname$1 as urlToPathname, setUrlBasename$1 as setUrlBasename, urlToFileSystemPath$1 as urlToFileSystemPath, writeFileSync$1 as writeFileSync, createLogger$1 as createLogger, URL_META$1 as URL_META, applyNodeEsmResolution$1 as applyNodeEsmResolution, RUNTIME_COMPAT$1 as RUNTIME_COMPAT, normalizeUrl$1 as normalizeUrl, ANSI$1 as ANSI, CONTENT_TYPE$1 as CONTENT_TYPE, urlToFilename$1 as urlToFilename, DATA_URL$1 as DATA_URL, errorToHTML$1 as errorToHTML, normalizeImportMap$1 as normalizeImportMap, composeTwoImportMaps$1 as composeTwoImportMaps, resolveImport$1 as resolveImport, JS_QUOTES$1 as JS_QUOTES, defaultLookupPackageScope$1 as defaultLookupPackageScope, defaultReadPackageJson$1 as defaultReadPackageJson, readCustomConditionsFromProcessArgs$1 as readCustomConditionsFromProcessArgs, readEntryStatSync$1 as readEntryStatSync, ensurePathnameTrailingSlash$1 as ensurePathnameTrailingSlash, compareFileUrls$1 as compareFileUrls, applyFileSystemMagicResolution$1 as applyFileSystemMagicResolution, getExtensionsToTry$1 as getExtensionsToTry, setUrlExtension$1 as setUrlExtension, isSpecifierForNodeBuiltin$1 as isSpecifierForNodeBuiltin, renderDetails, humanizeDuration, humanizeFileSize, renderTable, renderBigSection, distributePercentages, humanizeMemory, comparePathnames, UNICODE, escapeRegexpSpecialChars, injectQueryParamIntoSpecifierWithoutEncoding, renderUrlOrRelativeUrlFilename, assertAndNormalizeDirectoryUrl$1 as assertAndNormalizeDirectoryUrl, Abort, raceProcessTeardownEvents, startMonitoringCpuUsage, startMonitoringMemoryUsage, createLookupPackageDirectory, readPackageAtOrNull$1 as readPackageAtOrNull, inferRuntimeCompatFromClosestPackage, browserDefaultRuntimeCompat, nodeDefaultRuntimeCompat, clearDirectorySync, createTaskLog$1 as createTaskLog, ensureEmptyDirectory, updateJsonFileSync, createDynamicLog } from "../jsenv_core_packages.js";
7
+ import { lookupPackageDirectory, registerDirectoryLifecycle, urlToRelativeUrl, createDetailedMessage, stringifyUrlSite, generateContentFrame, validateResponseIntegrity, urlIsInsideOf, ensureWindowsDriveLetter, setUrlFilename, moveUrl, getCallerPosition, urlToBasename, urlToExtension, asSpecifierWithoutSearch, asUrlWithoutSearch, injectQueryParamsIntoSpecifier, bufferToEtag, isFileSystemPath, urlToPathname, setUrlBasename, urlToFileSystemPath, writeFileSync, createLogger, URL_META, applyNodeEsmResolution, RUNTIME_COMPAT, normalizeUrl, ANSI, CONTENT_TYPE, urlToFilename, DATA_URL, errorToHTML, normalizeImportMap, composeTwoImportMaps, resolveImport, JS_QUOTES, defaultLookupPackageScope, defaultReadPackageJson, readCustomConditionsFromProcessArgs, readEntryStatSync, ensurePathnameTrailingSlash, compareFileUrls, applyFileSystemMagicResolution, getExtensionsToTry, setUrlExtension, isSpecifierForNodeBuiltin, renderDetails, humanizeDuration, humanizeFileSize, renderTable, renderBigSection, distributePercentages, humanizeMemory, comparePathnames, UNICODE, escapeRegexpSpecialChars, injectQueryParamIntoSpecifierWithoutEncoding, renderUrlOrRelativeUrlFilename, injectQueryParams, assertAndNormalizeDirectoryUrl, Abort, raceProcessTeardownEvents, startMonitoringCpuUsage, startMonitoringMemoryUsage, createLookupPackageDirectory, readPackageAtOrNull, inferRuntimeCompatFromClosestPackage, browserDefaultRuntimeCompat, nodeDefaultRuntimeCompat, clearDirectorySync, createTaskLog, ensureEmptyDirectory, updateJsonFileSync, createDynamicLog } from "./jsenv_core_packages.js";
8
8
  import { pathToFileURL } from "node:url";
9
9
  import { generateSourcemapFileUrl, createMagicSource, composeTwoSourcemaps, generateSourcemapDataUrl, SOURCEMAP } from "@jsenv/sourcemap";
10
10
  import { performance } from "node:perf_hooks";
11
11
  import { jsenvPluginSupervisor } from "@jsenv/plugin-supervisor";
12
12
  import { WebSocketResponse, pickContentType } from "@jsenv/server";
13
13
  import { randomUUID, createHash } from "node:crypto";
14
- import "../jsenv_core_node_modules.js";
14
+ import "./jsenv_core_node_modules.js";
15
15
  import "node:os";
16
16
  import "node:tty";
17
- import "node:util";
18
17
  import "node:path";
18
+ import "node:util";
19
19
 
20
20
  const getDirectoryWatchPatterns = (
21
21
  directoryUrl,
@@ -5669,28 +5669,63 @@ const createBuildPackageConditions = (
5669
5669
  const nodeRuntimeEnabled = Object.keys(runtimeCompat).includes("node");
5670
5670
  // https://nodejs.org/api/esm.html#resolver-algorithm-specification
5671
5671
  const processArgConditions = readCustomConditionsFromProcessArgs();
5672
- const packageConditionsDefaultResolvers = {};
5673
- for (const processArgCondition of processArgConditions) {
5674
- packageConditionsDefaultResolvers[processArgCondition] = true;
5675
- }
5676
- const packageConditionResolvers = {
5677
- ...packageConditionsDefaultResolvers,
5678
- development: (specifier, importer) => {
5679
- if (isBareSpecifier$1(specifier)) {
5680
- const { url } = applyNodeEsmResolution({
5681
- specifier,
5682
- parentUrl: importer,
5683
- });
5684
- return !url.includes("/node_modules/");
5672
+ const devResolver = (specifier, importer) => {
5673
+ if (isBareSpecifier$1(specifier)) {
5674
+ const { url } = applyNodeEsmResolution({
5675
+ specifier,
5676
+ parentUrl: importer,
5677
+ });
5678
+ return !url.includes("/node_modules/");
5679
+ }
5680
+ return !importer.includes("/node_modules/");
5681
+ };
5682
+
5683
+ const conditionDefaultResolvers = {
5684
+ "dev:*": devResolver,
5685
+ "development": devResolver,
5686
+ "node": nodeRuntimeEnabled,
5687
+ "browser": !nodeRuntimeEnabled,
5688
+ "import": true,
5689
+ };
5690
+ const conditionResolvers = {
5691
+ ...conditionDefaultResolvers,
5692
+ };
5693
+
5694
+ let wildcardToRemoveSet = new Set();
5695
+ const addCustomResolver = (condition, customResolver) => {
5696
+ for (const conditionCandidate of Object.keys(conditionDefaultResolvers)) {
5697
+ if (conditionCandidate.includes("*")) {
5698
+ const conditionRegex = new RegExp(
5699
+ `^${conditionCandidate.replace(/\*/g, "(.*)")}$`,
5700
+ );
5701
+ if (conditionRegex.test(condition)) {
5702
+ const existingResolver =
5703
+ conditionDefaultResolvers[conditionCandidate];
5704
+ wildcardToRemoveSet.add(conditionCandidate);
5705
+ conditionResolvers[condition] = combineTwoPackageConditionResolvers(
5706
+ existingResolver,
5707
+ customResolver,
5708
+ );
5709
+ return;
5710
+ }
5685
5711
  }
5686
- return !importer.includes("/node_modules/");
5687
- },
5688
- node: nodeRuntimeEnabled,
5689
- browser: !nodeRuntimeEnabled,
5690
- import: true,
5712
+ }
5713
+ const existingResolver = conditionDefaultResolvers[condition];
5714
+ if (existingResolver) {
5715
+ conditionResolvers[condition] = combineTwoPackageConditionResolvers(
5716
+ existingResolver,
5717
+ customResolver,
5718
+ );
5719
+ return;
5720
+ }
5721
+ conditionResolvers[condition] = customResolver;
5691
5722
  };
5692
- for (const condition of Object.keys(packageConditions)) {
5693
- const value = packageConditions[condition];
5723
+
5724
+ for (const processArgCondition of processArgConditions) {
5725
+ addCustomResolver(processArgCondition, true);
5726
+ }
5727
+ for (const customCondition of Object.keys(packageConditions)) {
5728
+ const value = packageConditions[customCondition];
5694
5729
  let customResolver;
5695
5730
  if (typeof value === "object") {
5696
5731
  const associations = URL_META.resolveAssociations(
@@ -5733,29 +5768,25 @@ const createBuildPackageConditions = (
5733
5768
  } else if (typeof value === "function") {
5734
5769
  customResolver = value;
5735
5770
  } else {
5736
- customResolver = () => value;
5737
- }
5738
- const existing = packageConditionResolvers[condition];
5739
- if (existing) {
5740
- packageConditionResolvers[condition] = (...args) => {
5741
- const customResult = customResolver(...args);
5742
- return customResult === undefined ? existing(...args) : customResult;
5743
- };
5744
- } else {
5745
- packageConditionResolvers[condition] = customResolver;
5771
+ customResolver = value;
5746
5772
  }
5773
+ addCustomResolver(customCondition, customResolver);
5774
+ }
5775
+
5776
+ for (const wildcardToRemove of wildcardToRemoveSet) {
5777
+ delete conditionResolvers[wildcardToRemove];
5747
5778
  }
5748
5779
 
5780
+ const conditionCandidateArray = Object.keys(conditionResolvers);
5749
5781
  return (specifier, importer) => {
5750
5782
  const conditions = [];
5751
- for (const conditionCandidate of Object.keys(packageConditionResolvers)) {
5752
- const packageConditionResolver =
5753
- packageConditionResolvers[conditionCandidate];
5754
- if (typeof packageConditionResolver === "function") {
5755
- if (packageConditionResolver(specifier, importer)) {
5783
+ for (const conditionCandidate of conditionCandidateArray) {
5784
+ const conditionResolver = conditionResolvers[conditionCandidate];
5785
+ if (typeof conditionResolver === "function") {
5786
+ if (conditionResolver(specifier, importer)) {
5756
5787
  conditions.push(conditionCandidate);
5757
5788
  }
5758
- } else if (packageConditionResolver) {
5789
+ } else if (conditionResolver) {
5759
5790
  conditions.push(conditionCandidate);
5760
5791
  }
5761
5792
  }
@@ -5763,6 +5794,22 @@ const createBuildPackageConditions = (
5763
5794
  };
5764
5795
  };
5765
5796
 
5797
+ const combineTwoPackageConditionResolvers = (first, second) => {
5798
+ if (typeof second !== "function") {
5799
+ return second;
5800
+ }
5801
+ return (...args) => {
5802
+ const secondResult = second(...args);
5803
+ if (secondResult !== undefined) {
5804
+ return secondResult;
5805
+ }
5806
+ if (typeof first === "function") {
5807
+ return first(...args);
5808
+ }
5809
+ return first;
5810
+ };
5811
+ };
5812
+
5766
5813
  const addRelationshipWithPackageJson = ({
5767
5814
  reference,
5768
5815
  packageJsonUrl,
@@ -9020,7 +9067,6 @@ const createBuildSpecifierManager = ({
9020
9067
  buildUrl = generateSourcemapFileUrl(parentBuildUrl);
9021
9068
  reference.generatedSpecifier = buildUrl;
9022
9069
  } else {
9023
- const url = reference.generatedUrl;
9024
9070
  const rawUrlInfo = rawKitchen.graph.getUrlInfo(reference.url);
9025
9071
  let urlInfo;
9026
9072
  if (rawUrlInfo) {
@@ -9030,6 +9076,7 @@ const createBuildSpecifierManager = ({
9030
9076
  buildUrlInfo.subtype = reference.expectedSubtype;
9031
9077
  urlInfo = buildUrlInfo;
9032
9078
  }
9079
+ const url = reference.generatedUrl;
9033
9080
  buildUrl = buildUrlsGenerator.generate(url, {
9034
9081
  urlInfo,
9035
9082
  ownerUrlInfo: reference.ownerUrlInfo,
@@ -9207,7 +9254,7 @@ const createBuildSpecifierManager = ({
9207
9254
  js_classic: undefined, // TODO: add comment to explain who is using this
9208
9255
  entry_point: undefined,
9209
9256
  dynamic_import: undefined,
9210
- dynamic_import_id: undefined,
9257
+ // dynamic_import_id: undefined,
9211
9258
  };
9212
9259
  },
9213
9260
  formatReference: (reference) => {
@@ -10349,6 +10396,20 @@ const createBuildUrlsGenerator = ({
10349
10396
  return buildUrlFromMap;
10350
10397
  }
10351
10398
  if (urlIsInsideOf(url, buildDirectoryUrl)) {
10399
+ if (ownerUrlInfo.searchParams.has("dynamic_import_id")) {
10400
+ const ownerDirectoryPath = determineDirectoryPath({
10401
+ sourceDirectoryUrl,
10402
+ assetsDirectory,
10403
+ urlInfo: ownerUrlInfo,
10404
+ });
10405
+ const buildRelativeUrl = urlToRelativeUrl(url, buildDirectoryUrl);
10406
+ let buildUrl = `${buildDirectoryUrl}${ownerDirectoryPath}${buildRelativeUrl}`;
10407
+ buildUrl = injectQueryParams(buildUrl, {
10408
+ dynamic_import_id: undefined,
10409
+ });
10410
+ associateBuildUrl(url, buildUrl);
10411
+ return buildUrl;
10412
+ }
10352
10413
  associateBuildUrl(url, url);
10353
10414
  return url;
10354
10415
  }
@@ -10369,7 +10430,8 @@ const createBuildUrlsGenerator = ({
10369
10430
  } else {
10370
10431
  directoryPath = urlToRelativeUrl(url, sourceDirectoryUrl);
10371
10432
  }
10372
- const { search } = new URL(url);
10433
+ const urlObject = new URL(url);
10434
+ const { search } = urlObject;
10373
10435
  const buildUrl = `${buildDirectoryUrl}${directoryPath}${search}`;
10374
10436
  associateBuildUrl(url, buildUrl);
10375
10437
  return buildUrl;
@@ -10387,6 +10449,7 @@ const createBuildUrlsGenerator = ({
10387
10449
  nameSetPerDirectoryMap.set(directoryPath, nameSet);
10388
10450
  }
10389
10451
  const urlObject = new URL(url);
10452
+ injectQueryParams(urlObject, { dynamic_import_id: undefined });
10390
10453
  let { search, hash } = urlObject;
10391
10454
  let urlName = getUrlName(url, urlInfo);
10392
10455
  let [basename, extension] = splitFileExtension(urlName);
@@ -10433,7 +10496,7 @@ const determineDirectoryPath = ({
10433
10496
  sourceDirectoryUrl,
10434
10497
  assetsDirectory,
10435
10498
  urlInfo,
10436
- ownerUrlInfo,
10499
+ ownerUrlInfo = urlInfo.firstReference.ownerUrlInfo,
10437
10500
  }) => {
10438
10501
  if (urlInfo.dirnameHint) {
10439
10502
  return urlInfo.dirnameHint;
@@ -10445,13 +10508,29 @@ const determineDirectoryPath = ({
10445
10508
  const parentDirectoryPath = determineDirectoryPath({
10446
10509
  sourceDirectoryUrl,
10447
10510
  assetsDirectory,
10448
- urlInfo: ownerUrlInfo || urlInfo.firstReference.ownerUrlInfo,
10511
+ urlInfo: ownerUrlInfo,
10449
10512
  });
10450
10513
  return parentDirectoryPath;
10451
10514
  }
10452
10515
  const dynamicImportId = urlInfo.searchParams.get("dynamic_import_id");
10453
10516
  if (dynamicImportId) {
10454
- return `${assetsDirectory}${dynamicImportId}/`;
10517
+ const ancestorImportIds = [];
10518
+ let ancestorUrlInfo = ownerUrlInfo;
10519
+ let currentImportId = dynamicImportId;
10520
+ while (ancestorUrlInfo) {
10521
+ const ancestorDynamicImportId =
10522
+ ancestorUrlInfo.searchParams.get("dynamic_import_id");
10523
+ if (!ancestorDynamicImportId) {
10524
+ break;
10525
+ }
10526
+ if (ancestorDynamicImportId !== currentImportId) {
10527
+ ancestorImportIds.push(ancestorDynamicImportId);
10528
+ currentImportId = ancestorDynamicImportId;
10529
+ }
10530
+ ancestorUrlInfo = ancestorUrlInfo.firstReference?.ownerUrlInfo;
10531
+ }
10532
+ const importIdPath = [...ancestorImportIds, dynamicImportId].join("/");
10533
+ return `${assetsDirectory}${importIdPath}/`;
10455
10534
  }
10456
10535
  if (urlInfo.isEntryPoint && !urlInfo.isDynamicEntryPoint) {
10457
10536
  return "";
@@ -10694,7 +10773,12 @@ const build = async ({
10694
10773
  let runtimeType;
10695
10774
  {
10696
10775
  if (isBareSpecifier(key)) {
10697
- const packageConditions = ["development", "node", "import"];
10776
+ const packageConditions = [
10777
+ "development",
10778
+ "dev:*",
10779
+ "node",
10780
+ "import",
10781
+ ];
10698
10782
  try {
10699
10783
  const { url, type } = applyNodeEsmResolution({
10700
10784
  conditions: packageConditions,
@@ -10870,7 +10954,42 @@ const build = async ({
10870
10954
  { sourceUrlToLog, buildUrlToLog },
10871
10955
  ) => {
10872
10956
  let content = "";
10873
- content += `${UNICODE.OK} ${ANSI.color(sourceUrlToLog, ANSI.GREY)} ${ANSI.color("->", ANSI.GREY)} ${ANSI.color(buildUrlToLog, "")}`;
10957
+
10958
+ const applyColorOnFileRelativeUrl = (fileRelativeUrl, color) => {
10959
+ const fileUrl = new URL(fileRelativeUrl, rootPackageDirectoryUrl);
10960
+ const packageDirectoryUrl = lookupPackageDirectory(fileUrl);
10961
+ if (
10962
+ !packageDirectoryUrl ||
10963
+ packageDirectoryUrl === rootPackageDirectoryUrl
10964
+ ) {
10965
+ return ANSI.color(fileRelativeUrl, color);
10966
+ }
10967
+ const parentDirectoryUrl = new URL("../", packageDirectoryUrl).href;
10968
+ const beforePackageDirectoryName = urlToRelativeUrl(
10969
+ parentDirectoryUrl,
10970
+ rootPackageDirectoryUrl,
10971
+ );
10972
+ const packageDirectoryName = urlToFilename(packageDirectoryUrl);
10973
+ const afterPackageDirectoryUrl = urlToRelativeUrl(
10974
+ fileUrl,
10975
+ packageDirectoryUrl,
10976
+ );
10977
+ const beforePackageNameStylized = ANSI.color(
10978
+ beforePackageDirectoryName,
10979
+ color,
10980
+ );
10981
+ const packageNameStylized = ANSI.color(
10982
+ ANSI.effect(packageDirectoryName, ANSI.UNDERLINE),
10983
+ color,
10984
+ );
10985
+ const afterPackageNameStylized = ANSI.color(
10986
+ `/${afterPackageDirectoryUrl}`,
10987
+ color,
10988
+ );
10989
+ return `${beforePackageNameStylized}${packageNameStylized}${afterPackageNameStylized}`;
10990
+ };
10991
+
10992
+ content += `${UNICODE.OK} ${applyColorOnFileRelativeUrl(sourceUrlToLog, ANSI.GREY)} ${ANSI.color("->", ANSI.GREY)} ${applyColorOnFileRelativeUrl(buildUrlToLog, "")}`;
10874
10993
  // content += " ";
10875
10994
  // content += ANSI.color("(", ANSI.GREY);
10876
10995
  // content += ANSI.color(