@jsenv/core 39.3.11 → 39.4.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.
@@ -14,7 +14,7 @@ import { Http2ServerResponse } from "node:http2";
14
14
  import { performance as performance$1 } from "node:perf_hooks";
15
15
  import { lookup } from "node:dns";
16
16
  import { parseJsUrls, parseHtml, visitHtmlNodes, getHtmlNodeAttribute, analyzeScriptNode, getHtmlNodeText, stringifyHtmlAst, setHtmlNodeAttributes, applyBabelPlugins, injectJsImport, visitJsAstUntil, injectHtmlNodeAsEarlyAsPossible, createHtmlNode, generateUrlForInlineContent, parseJsWithAcorn, getHtmlNodePosition, getUrlForContentInsideHtml, setHtmlNodeText, parseCssUrls, getHtmlNodeAttributePosition, parseSrcSet, removeHtmlNodeText, removeHtmlNode, getUrlForContentInsideJs, analyzeLinkNode, injectJsenvScript, findHtmlNode, insertHtmlNodeAfter } from "@jsenv/ast";
17
- import { sourcemapConverter, createMagicSource, composeTwoSourcemaps, SOURCEMAP, generateSourcemapFileUrl, generateSourcemapDataUrl } from "@jsenv/sourcemap";
17
+ import { sourcemapConverter, createMagicSource, composeTwoSourcemaps, generateSourcemapFileUrl, SOURCEMAP, generateSourcemapDataUrl } from "@jsenv/sourcemap";
18
18
  import { createRequire } from "node:module";
19
19
  import { systemJsClientFileUrlDefault, convertJsModuleToJsClassic } from "@jsenv/js-module-fallback";
20
20
  import { RUNTIME_COMPAT } from "@jsenv/runtime-compat";
@@ -1178,15 +1178,13 @@ const createDynamicLog = ({
1178
1178
  // is that node.js will later throw error if stream gets closed
1179
1179
  // while something listening data on it
1180
1180
  const spyStreamOutput = (stream, callback) => {
1181
- const originalWrite = stream.write;
1182
-
1183
1181
  let output = "";
1184
1182
  let installed = true;
1185
-
1183
+ const originalWrite = stream.write;
1186
1184
  stream.write = function (...args /* chunk, encoding, callback */) {
1187
1185
  output += args;
1188
1186
  callback(output);
1189
- return originalWrite.call(stream, ...args);
1187
+ return originalWrite.call(this, ...args);
1190
1188
  };
1191
1189
 
1192
1190
  const uninstall = () => {
@@ -1465,7 +1463,7 @@ const isValidUrl$1 = (url) => {
1465
1463
  // eslint-disable-next-line no-new
1466
1464
  new URL(url);
1467
1465
  return true;
1468
- } catch (e) {
1466
+ } catch {
1469
1467
  return false;
1470
1468
  }
1471
1469
  };
@@ -1826,7 +1824,7 @@ const validateDirectoryUrl = (value) => {
1826
1824
  } else {
1827
1825
  try {
1828
1826
  urlString = String(new URL(value));
1829
- } catch (e) {
1827
+ } catch {
1830
1828
  return {
1831
1829
  valid: false,
1832
1830
  value,
@@ -1834,6 +1832,12 @@ const validateDirectoryUrl = (value) => {
1834
1832
  };
1835
1833
  }
1836
1834
  }
1835
+ } else if (
1836
+ value &&
1837
+ typeof value === "object" &&
1838
+ typeof value.href === "string"
1839
+ ) {
1840
+ value = value.href;
1837
1841
  } else {
1838
1842
  return {
1839
1843
  valid: false,
@@ -1876,7 +1880,7 @@ const validateFileUrl = (value, baseUrl) => {
1876
1880
  } else {
1877
1881
  try {
1878
1882
  urlString = String(new URL(value, baseUrl));
1879
- } catch (e) {
1883
+ } catch {
1880
1884
  return {
1881
1885
  valid: false,
1882
1886
  value,
@@ -1987,7 +1991,7 @@ const baseUrlFallback = fileSystemPathToUrl$1(process.cwd());
1987
1991
  const ensureWindowsDriveLetter = (url, baseUrl) => {
1988
1992
  try {
1989
1993
  url = String(new URL(url));
1990
- } catch (e) {
1994
+ } catch {
1991
1995
  throw new Error(`absolute url expect but got ${url}`);
1992
1996
  }
1993
1997
 
@@ -1997,7 +2001,7 @@ const ensureWindowsDriveLetter = (url, baseUrl) => {
1997
2001
 
1998
2002
  try {
1999
2003
  baseUrl = String(new URL(baseUrl));
2000
- } catch (e) {
2004
+ } catch {
2001
2005
  throw new Error(
2002
2006
  `absolute baseUrl expect but got ${baseUrl} to ensure windows drive letter on ${url}`,
2003
2007
  );
@@ -2847,7 +2851,7 @@ const resolveAssociations = (associations, baseUrl) => {
2847
2851
  let patternResolved;
2848
2852
  try {
2849
2853
  patternResolved = String(new URL(pattern, baseUrl));
2850
- } catch (e) {
2854
+ } catch {
2851
2855
  // it's not really an url, no need to perform url resolution nor encoding
2852
2856
  patternResolved = pattern;
2853
2857
  }
@@ -7826,7 +7830,7 @@ const getMtimeResponse = async ({ headers, sourceStat }) => {
7826
7830
  let cachedModificationDate;
7827
7831
  try {
7828
7832
  cachedModificationDate = new Date(headers["if-modified-since"]);
7829
- } catch (e) {
7833
+ } catch {
7830
7834
  return {
7831
7835
  status: 400,
7832
7836
  statusText: "if-modified-since header is not a valid date",
@@ -7928,7 +7932,7 @@ const asUrlString = (value) => {
7928
7932
  try {
7929
7933
  const urlObject = new URL(value);
7930
7934
  return String(urlObject);
7931
- } catch (e) {
7935
+ } catch {
7932
7936
  return null;
7933
7937
  }
7934
7938
  }
@@ -9606,7 +9610,6 @@ const babelPluginBabelHelpersAsJsenvImports = (
9606
9610
  };
9607
9611
  };
9608
9612
 
9609
- /* eslint-disable camelcase */
9610
9613
  // copied from
9611
9614
  // https://github.com/babel/babel/blob/e498bee10f0123bb208baa228ce6417542a2c3c4/packages/babel-compat-data/data/plugins.json#L1
9612
9615
  // https://github.com/babel/babel/blob/master/packages/babel-compat-data/data/plugins.json#L1
@@ -12010,6 +12013,48 @@ const assertFetchedContentCompliance = ({ urlInfo, content }) => {
12010
12013
  }
12011
12014
  };
12012
12015
 
12016
+ const determineFileUrlForOutDirectory = (urlInfo) => {
12017
+ let { url, filenameHint } = urlInfo;
12018
+ const { rootDirectoryUrl, outDirectoryUrl } = urlInfo.context;
12019
+ if (!outDirectoryUrl) {
12020
+ return url;
12021
+ }
12022
+ if (!url.startsWith("file:")) {
12023
+ return url;
12024
+ }
12025
+ if (!urlIsInsideOf(url, rootDirectoryUrl)) {
12026
+ const fsRootUrl = ensureWindowsDriveLetter("file:///", url);
12027
+ url = `${rootDirectoryUrl}@fs/${url.slice(fsRootUrl.length)}`;
12028
+ }
12029
+ if (filenameHint) {
12030
+ url = setUrlFilename(url, filenameHint);
12031
+ }
12032
+ return moveUrl({
12033
+ url,
12034
+ from: rootDirectoryUrl,
12035
+ to: outDirectoryUrl,
12036
+ });
12037
+ };
12038
+
12039
+ const determineSourcemapFileUrl = (urlInfo) => {
12040
+ // sourcemap is a special kind of reference:
12041
+ // It's a reference to a content generated dynamically the content itself.
12042
+ // when jsenv is done cooking the file
12043
+ // during build it's urlInfo.url to be inside the build
12044
+ // but otherwise it's generatedUrl to be inside .jsenv/ directory
12045
+ const generatedUrlObject = new URL(urlInfo.generatedUrl);
12046
+ generatedUrlObject.searchParams.delete("js_module_fallback");
12047
+ generatedUrlObject.searchParams.delete("as_js_module");
12048
+ generatedUrlObject.searchParams.delete("as_js_classic");
12049
+ generatedUrlObject.searchParams.delete("as_css_module");
12050
+ generatedUrlObject.searchParams.delete("as_json_module");
12051
+ generatedUrlObject.searchParams.delete("as_text_module");
12052
+ generatedUrlObject.searchParams.delete("dynamic_import");
12053
+ generatedUrlObject.searchParams.delete("cjs_as_js_module");
12054
+ const urlForSourcemap = generatedUrlObject.href;
12055
+ return generateSourcemapFileUrl(urlForSourcemap);
12056
+ };
12057
+
12013
12058
  const createEventEmitter = () => {
12014
12059
  const callbackSet = new Set();
12015
12060
  const on = (callback) => {
@@ -12480,7 +12525,9 @@ const createReference = ({
12480
12525
  if (specifier instanceof URL) {
12481
12526
  specifier = specifier.href;
12482
12527
  } else {
12483
- throw new TypeError(`"specifier" must be a string, got ${specifier}`);
12528
+ throw new TypeError(
12529
+ `"specifier" must be a string, got ${specifier} in ${ownerUrlInfo.url}`,
12530
+ );
12484
12531
  }
12485
12532
  }
12486
12533
  const reference = {
@@ -12885,7 +12932,7 @@ const applyReferenceEffectsOnUrlInfo = (reference) => {
12885
12932
  }
12886
12933
  referencedUrlInfo.firstReference = reference;
12887
12934
  referencedUrlInfo.originalUrl =
12888
- referencedUrlInfo.originalUrl || reference.url;
12935
+ referencedUrlInfo.originalUrl || (reference.original || reference).url;
12889
12936
 
12890
12937
  if (reference.isEntryPoint || isWebWorkerEntryPointReference(reference)) {
12891
12938
  referencedUrlInfo.isEntryPoint = true;
@@ -13786,22 +13833,6 @@ const createUrlInfoTransformer = ({
13786
13833
  if (!may || !shouldHandle) {
13787
13834
  return;
13788
13835
  }
13789
- // sourcemap is a special kind of reference:
13790
- // It's a reference to a content generated dynamically the content itself.
13791
- // when jsenv is done cooking the file
13792
- // during build it's urlInfo.url to be inside the build
13793
- // but otherwise it's generatedUrl to be inside .jsenv/ directory
13794
- const generatedUrlObject = new URL(urlInfo.generatedUrl);
13795
- generatedUrlObject.searchParams.delete("js_module_fallback");
13796
- generatedUrlObject.searchParams.delete("as_js_module");
13797
- generatedUrlObject.searchParams.delete("as_js_classic");
13798
- generatedUrlObject.searchParams.delete("as_css_module");
13799
- generatedUrlObject.searchParams.delete("as_json_module");
13800
- generatedUrlObject.searchParams.delete("as_text_module");
13801
- generatedUrlObject.searchParams.delete("dynamic_import");
13802
- generatedUrlObject.searchParams.delete("cjs_as_js_module");
13803
- const urlForSourcemap = generatedUrlObject.href;
13804
- urlInfo.sourcemapGeneratedUrl = generateSourcemapFileUrl(urlForSourcemap);
13805
13836
 
13806
13837
  // case #1: already loaded during "load" hook
13807
13838
  // - happens during build
@@ -14223,7 +14254,11 @@ const createKitchen = ({
14223
14254
  reference.type !== "js_import"
14224
14255
  ) {
14225
14256
  referenceUrl = `ignore:${referenceUrl}`;
14226
- } else if (isIgnored(referenceUrl)) {
14257
+ } else if (
14258
+ reference.url && reference.original
14259
+ ? isIgnored(reference.original.url)
14260
+ : isIgnored(referenceUrl)
14261
+ ) {
14227
14262
  referenceUrl = `ignore:${referenceUrl}`;
14228
14263
  }
14229
14264
 
@@ -14315,13 +14350,16 @@ ${ANSI.color(normalizedReturnValue, ANSI.YELLOW)}
14315
14350
  kitchenContext.resolveReference = resolveReference;
14316
14351
 
14317
14352
  const finalizeReference = (reference) => {
14353
+ const urlInfo = reference.urlInfo;
14354
+ urlInfo.generatedUrl = determineFileUrlForOutDirectory(urlInfo);
14355
+ urlInfo.sourcemapGeneratedUrl = determineSourcemapFileUrl(urlInfo);
14356
+
14318
14357
  if (reference.isImplicit && reference.isWeak) {
14319
14358
  // not needed for implicit references that are not rendered anywhere
14320
14359
  // this condition excludes:
14321
14360
  // - side_effect_file references injected in entry points or at the top of files
14322
14361
  return;
14323
14362
  }
14324
-
14325
14363
  {
14326
14364
  // This hook must touch reference.generatedUrl, NOT reference.url
14327
14365
  // And this is because this hook inject query params used to:
@@ -14428,7 +14466,9 @@ ${ANSI.color(normalizedReturnValue, ANSI.YELLOW)}
14428
14466
  urlInfo.subtypeHint ||
14429
14467
  "";
14430
14468
  // during build urls info are reused and load returns originalUrl/originalContent
14431
- urlInfo.originalUrl = originalUrl || urlInfo.originalUrl;
14469
+ urlInfo.originalUrl = originalUrl
14470
+ ? String(originalUrl)
14471
+ : urlInfo.originalUrl;
14432
14472
  if (data) {
14433
14473
  Object.assign(urlInfo.data, data);
14434
14474
  }
@@ -14439,7 +14479,6 @@ ${ANSI.color(normalizedReturnValue, ANSI.YELLOW)}
14439
14479
  urlInfo,
14440
14480
  content,
14441
14481
  });
14442
- urlInfo.generatedUrl = determineFileUrlForOutDirectory(urlInfo);
14443
14482
 
14444
14483
  // we wait here to read .contentAst and .originalContentAst
14445
14484
  // so that we don't trigger lazy getters
@@ -14776,30 +14815,6 @@ const inferUrlInfoType = (urlInfo) => {
14776
14815
  return "other";
14777
14816
  };
14778
14817
 
14779
- const determineFileUrlForOutDirectory = (urlInfo) => {
14780
- if (!urlInfo.context.outDirectoryUrl) {
14781
- return urlInfo.url;
14782
- }
14783
- if (!urlInfo.url.startsWith("file:")) {
14784
- return urlInfo.url;
14785
- }
14786
- let url = urlInfo.url;
14787
- if (!urlIsInsideOf(urlInfo.url, urlInfo.context.rootDirectoryUrl)) {
14788
- const fsRootUrl = ensureWindowsDriveLetter("file:///", urlInfo.url);
14789
- url = `${urlInfo.context.rootDirectoryUrl}@fs/${url.slice(
14790
- fsRootUrl.length,
14791
- )}`;
14792
- }
14793
- if (urlInfo.filenameHint) {
14794
- url = setUrlFilename(url, urlInfo.filenameHint);
14795
- }
14796
- return moveUrl({
14797
- url,
14798
- from: urlInfo.context.rootDirectoryUrl,
14799
- to: urlInfo.context.outDirectoryUrl,
14800
- });
14801
- };
14802
-
14803
14818
  const createUrlGraphSummary = (
14804
14819
  urlGraph,
14805
14820
  { title = "graph summary" } = {},
@@ -17227,7 +17242,7 @@ const isValidUrl = (url) => {
17227
17242
  // eslint-disable-next-line no-new
17228
17243
  new URL(url);
17229
17244
  return true;
17230
- } catch (e) {
17245
+ } catch {
17231
17246
  return false;
17232
17247
  }
17233
17248
  };
@@ -17275,7 +17290,7 @@ const defaultReadPackageJson = (packageUrl) => {
17275
17290
  const string = String(buffer);
17276
17291
  try {
17277
17292
  return JSON.parse(string);
17278
- } catch (e) {
17293
+ } catch {
17279
17294
  throw new Error(`Invalid package configuration`);
17280
17295
  }
17281
17296
  };
@@ -17523,7 +17538,7 @@ const applyPackageSpecifierResolution = (specifier, resolutionContext) => {
17523
17538
  type: "absolute_specifier",
17524
17539
  url: urlObject.href,
17525
17540
  };
17526
- } catch (e) {
17541
+ } catch {
17527
17542
  // bare specifier
17528
17543
  const browserFieldResolution = applyBrowserFieldResolution(
17529
17544
  specifier,
@@ -18300,17 +18315,27 @@ const createNodeEsmResolver = ({
18300
18315
  const { ownerUrlInfo } = reference;
18301
18316
  if (reference.specifier === "/") {
18302
18317
  const { mainFilePath, rootDirectoryUrl } = ownerUrlInfo.context;
18303
- return String(new URL(mainFilePath, rootDirectoryUrl));
18318
+ const url = new URL(mainFilePath, rootDirectoryUrl);
18319
+ return url;
18304
18320
  }
18305
18321
  if (reference.specifier[0] === "/") {
18306
- return new URL(
18322
+ const url = new URL(
18307
18323
  reference.specifier.slice(1),
18308
18324
  ownerUrlInfo.context.rootDirectoryUrl,
18309
- ).href;
18325
+ );
18326
+ return url;
18327
+ }
18328
+ let parentUrl;
18329
+ if (reference.baseUrl) {
18330
+ parentUrl = reference.baseUrl;
18331
+ } else if (ownerUrlInfo.originalUrl?.startsWith("http")) {
18332
+ parentUrl = ownerUrlInfo.originalUrl;
18333
+ } else {
18334
+ parentUrl = ownerUrlInfo.url;
18310
18335
  }
18311
- const parentUrl = reference.baseUrl || ownerUrlInfo.url;
18312
18336
  if (!parentUrl.startsWith("file:")) {
18313
- return new URL(reference.specifier, parentUrl).href;
18337
+ const url = new URL(reference.specifier, parentUrl);
18338
+ return url;
18314
18339
  }
18315
18340
  const { url, type, packageDirectoryUrl } = applyNodeEsmResolution({
18316
18341
  conditions: packageConditions,
@@ -18497,20 +18522,22 @@ const jsenvPluginWebResolution = () => {
18497
18522
  const { ownerUrlInfo } = reference;
18498
18523
  if (reference.specifier === "/") {
18499
18524
  const { mainFilePath, rootDirectoryUrl } = ownerUrlInfo.context;
18500
- return String(new URL(mainFilePath, rootDirectoryUrl));
18525
+ const url = new URL(mainFilePath, rootDirectoryUrl);
18526
+ return url;
18501
18527
  }
18502
18528
  if (reference.specifier[0] === "/") {
18503
- return new URL(
18529
+ const url = new URL(
18504
18530
  reference.specifier.slice(1),
18505
18531
  ownerUrlInfo.context.rootDirectoryUrl,
18506
- ).href;
18532
+ );
18533
+ return url;
18507
18534
  }
18508
- return new URL(
18509
- reference.specifier,
18510
- // baseUrl happens second argument to new URL() is different from
18511
- // import.meta.url or document.currentScript.src
18512
- reference.baseUrl || ownerUrlInfo.url,
18513
- ).href;
18535
+ // baseUrl happens second argument to new URL() is different from
18536
+ // import.meta.url or document.currentScript.src
18537
+ const parentUrl =
18538
+ reference.baseUrl || ownerUrlInfo.originalUrl || ownerUrlInfo.url;
18539
+ const url = new URL(reference.specifier, parentUrl);
18540
+ return url;
18514
18541
  },
18515
18542
  };
18516
18543
  };
@@ -18618,10 +18645,18 @@ const jsenvPluginFsRedirection = ({
18618
18645
  if (!stat) {
18619
18646
  return null;
18620
18647
  }
18621
- const urlRaw = preserveSymlinks
18622
- ? urlObject.href
18623
- : resolveSymlink(urlObject.href);
18624
- const resolvedUrl = `${urlRaw}${search}${hash}`;
18648
+ const urlBeforeSymlinkResolution = urlObject.href;
18649
+ if (preserveSymlinks) {
18650
+ return `${urlBeforeSymlinkResolution}${search}${hash}`;
18651
+ }
18652
+ const urlAfterSymlinkResolution = resolveSymlink(
18653
+ urlBeforeSymlinkResolution,
18654
+ );
18655
+ if (urlAfterSymlinkResolution !== urlBeforeSymlinkResolution) {
18656
+ reference.leadsToASymlink = true;
18657
+ // reference.baseUrl = urlBeforeSymlinkResolution;
18658
+ }
18659
+ const resolvedUrl = `${urlAfterSymlinkResolution}${search}${hash}`;
18625
18660
  return resolvedUrl;
18626
18661
  },
18627
18662
  };
@@ -18699,17 +18734,17 @@ const jsenvPluginProtocolFile = ({
18699
18734
  return null;
18700
18735
  },
18701
18736
  formatReference: (reference) => {
18702
- if (!reference.generatedUrl.startsWith("file:")) {
18737
+ const { generatedUrl } = reference;
18738
+ if (!generatedUrl.startsWith("file:")) {
18703
18739
  return null;
18704
18740
  }
18705
18741
  const { rootDirectoryUrl } = reference.ownerUrlInfo.context;
18706
- if (urlIsInsideOf(reference.generatedUrl, rootDirectoryUrl)) {
18707
- return `/${urlToRelativeUrl(
18708
- reference.generatedUrl,
18709
- rootDirectoryUrl,
18710
- )}`;
18742
+ if (urlIsInsideOf(generatedUrl, rootDirectoryUrl)) {
18743
+ const result = `/${urlToRelativeUrl(generatedUrl, rootDirectoryUrl)}`;
18744
+ return result;
18711
18745
  }
18712
- return `/@fs/${reference.generatedUrl.slice("file:///".length)}`;
18746
+ const result = `/@fs/${generatedUrl.slice("file:///".length)}`;
18747
+ return result;
18713
18748
  },
18714
18749
  },
18715
18750
  {
@@ -18940,24 +18975,96 @@ const replacePlaceholders$1 = (html, replacers) => {
18940
18975
  });
18941
18976
  };
18942
18977
 
18943
- const jsenvPluginProtocolHttp = () => {
18978
+ const jsenvPluginProtocolHttp = ({ include }) => {
18979
+ if (include === false) {
18980
+ return {
18981
+ name: "jsenv:protocol_http",
18982
+ appliesDuring: "*",
18983
+ redirectReference: (reference) => {
18984
+ if (!reference.url.startsWith("http")) {
18985
+ return null;
18986
+ }
18987
+ return `ignore:${reference.url}`;
18988
+ },
18989
+ };
18990
+ }
18991
+ const shouldInclude =
18992
+ include === true
18993
+ ? () => true
18994
+ : URL_META.createFilter(include, "http://jsenv.com");
18995
+
18944
18996
  return {
18945
18997
  name: "jsenv:protocol_http",
18946
- appliesDuring: "*",
18998
+ appliesDuring: "build",
18999
+ // resolveReference: (reference) => {
19000
+ // if (reference.original && reference.original.url.startsWith("http")) {
19001
+ // return new URL(reference.specifier, reference.original.url);
19002
+ // }
19003
+ // return null;
19004
+ // },
18947
19005
  redirectReference: (reference) => {
18948
- // TODO: according to some pattern matching jsenv could be allowed
18949
- // to fetch and transform http urls
18950
- if (
18951
- reference.url.startsWith("http:") ||
18952
- reference.url.startsWith("https:")
18953
- ) {
19006
+ if (!reference.url.startsWith("http")) {
19007
+ return null;
19008
+ }
19009
+ if (!shouldInclude(reference.url)) {
18954
19010
  return `ignore:${reference.url}`;
18955
19011
  }
18956
- return null;
19012
+ const outDirectoryUrl = reference.ownerUrlInfo.context.outDirectoryUrl;
19013
+ const urlObject = new URL(reference.url);
19014
+ const { host, pathname, search } = urlObject;
19015
+ let fileUrl = String(outDirectoryUrl);
19016
+ if (reference.url.startsWith("http:")) {
19017
+ fileUrl += "@http/";
19018
+ } else {
19019
+ fileUrl += "@https/";
19020
+ }
19021
+ fileUrl += asValidFilename(host);
19022
+ if (pathname) {
19023
+ fileUrl += "/";
19024
+ fileUrl += asValidFilename(pathname);
19025
+ }
19026
+ if (search) {
19027
+ fileUrl += search;
19028
+ }
19029
+ return fileUrl;
19030
+ },
19031
+ fetchUrlContent: async (urlInfo) => {
19032
+ if (!urlInfo.originalUrl.startsWith("http")) {
19033
+ return null;
19034
+ }
19035
+ const response = await fetch(urlInfo.originalUrl);
19036
+ const responseStatus = response.status;
19037
+ if (responseStatus < 200 || responseStatus > 299) {
19038
+ throw new Error(`unexpected response status ${responseStatus}`);
19039
+ }
19040
+ const responseHeaders = response.headers;
19041
+ const responseContentType = responseHeaders.get("content-type");
19042
+ const contentType = responseContentType || "application/octet-stream";
19043
+ const isTextual = CONTENT_TYPE.isTextual(contentType);
19044
+ let content;
19045
+ if (isTextual) {
19046
+ content = await response.text();
19047
+ } else {
19048
+ content = await response.buffer;
19049
+ }
19050
+ return {
19051
+ content,
19052
+ contentType,
19053
+ contentLength: responseHeaders.get("content-length") || undefined,
19054
+ };
18957
19055
  },
18958
19056
  };
18959
19057
  };
18960
19058
 
19059
+ // see https://github.com/parshap/node-sanitize-filename/blob/master/index.js
19060
+ const asValidFilename = (string) => {
19061
+ string = string.trim().toLowerCase();
19062
+ if (string === ".") return "_";
19063
+ if (string === "..") return "__";
19064
+ string = string.replace(/[ ,]/g, "_").replace(/["/?<>\\:*|]/g, "");
19065
+ return string;
19066
+ };
19067
+
18961
19068
  const jsenvPluginInjections = (rawAssociations) => {
18962
19069
  let resolvedAssociations;
18963
19070
 
@@ -20343,6 +20450,7 @@ const getCorePlugins = ({
20343
20450
  injections,
20344
20451
  transpilation = true,
20345
20452
  inlining = true,
20453
+ http = false,
20346
20454
 
20347
20455
  clientAutoreload,
20348
20456
  cacheControl,
@@ -20358,6 +20466,12 @@ const getCorePlugins = ({
20358
20466
  if (ribbon === true) {
20359
20467
  ribbon = {};
20360
20468
  }
20469
+ if (http === true) {
20470
+ http = { include: true };
20471
+ }
20472
+ if (http === false) {
20473
+ http = { include: false };
20474
+ }
20361
20475
 
20362
20476
  return [
20363
20477
  jsenvPluginReferenceAnalysis(referenceAnalysis),
@@ -20372,11 +20486,12 @@ const getCorePlugins = ({
20372
20486
  - reference inside a js module -> resolved by node esm
20373
20487
  - All the rest uses web standard url resolution
20374
20488
  */
20489
+ jsenvPluginProtocolHttp(http),
20375
20490
  jsenvPluginProtocolFile({
20376
20491
  magicExtensions,
20377
20492
  magicDirectoryIndex,
20378
20493
  }),
20379
- jsenvPluginProtocolHttp(),
20494
+
20380
20495
  ...(nodeEsmResolution
20381
20496
  ? [jsenvPluginNodeEsmResolution(nodeEsmResolution)]
20382
20497
  : []),
@@ -20513,7 +20628,6 @@ ${ANSI.color(buildUrl, ANSI.MAGENTA)}
20513
20628
  extension = extensionMappings[extension] || extension;
20514
20629
  let nameCandidate = `${basename}${extension}`; // reconstruct name in case extension was normalized
20515
20630
  let integer = 1;
20516
- // eslint-disable-next-line no-constant-condition
20517
20631
  while (true) {
20518
20632
  if (!names.includes(nameCandidate)) {
20519
20633
  names.push(nameCandidate);
@@ -20840,6 +20954,42 @@ const createBuildSpecifierManager = ({
20840
20954
  const url = new URL(reference.specifier, parentUrl).href;
20841
20955
  return url;
20842
20956
  },
20957
+ redirectReference: (reference) => {
20958
+ let referenceBeforeInlining = reference;
20959
+ if (
20960
+ referenceBeforeInlining.isInline &&
20961
+ referenceBeforeInlining.prev &&
20962
+ !referenceBeforeInlining.prev.isInline
20963
+ ) {
20964
+ referenceBeforeInlining = referenceBeforeInlining.prev;
20965
+ }
20966
+ const rawUrl = referenceBeforeInlining.url;
20967
+ const rawUrlInfo = rawKitchen.graph.getUrlInfo(rawUrl);
20968
+ if (rawUrlInfo) {
20969
+ reference.filenameHint = rawUrlInfo.filenameHint;
20970
+ return null;
20971
+ }
20972
+ if (referenceBeforeInlining.injected) {
20973
+ return null;
20974
+ }
20975
+ if (
20976
+ referenceBeforeInlining.isInline &&
20977
+ referenceBeforeInlining.ownerUrlInfo.url ===
20978
+ referenceBeforeInlining.ownerUrlInfo.originalUrl
20979
+ ) {
20980
+ const rawUrlInfo = findRawUrlInfoWhenInline(
20981
+ referenceBeforeInlining,
20982
+ rawKitchen,
20983
+ );
20984
+ if (rawUrlInfo) {
20985
+ reference.rawUrl = rawUrlInfo.url;
20986
+ reference.filenameHint = rawUrlInfo.filenameHint;
20987
+ return null;
20988
+ }
20989
+ }
20990
+ reference.filenameHint = referenceBeforeInlining.filenameHint;
20991
+ return null;
20992
+ },
20843
20993
  transformReferenceSearchParams: () => {
20844
20994
  // those search params are reflected into the build file name
20845
20995
  // moreover it create cleaner output
@@ -20884,13 +21034,10 @@ const createBuildSpecifierManager = ({
20884
21034
  ) {
20885
21035
  firstReference = firstReference.prev;
20886
21036
  }
20887
- const rawUrl = firstReference.url;
21037
+ const rawUrl = firstReference.rawUrl || firstReference.url;
20888
21038
  const rawUrlInfo = rawKitchen.graph.getUrlInfo(rawUrl);
20889
21039
  const bundleInfo = bundleInfoMap.get(rawUrl);
20890
21040
  if (bundleInfo) {
20891
- if (rawUrlInfo && !finalUrlInfo.filenameHint) {
20892
- finalUrlInfo.filenameHint = rawUrlInfo.filenameHint;
20893
- }
20894
21041
  finalUrlInfo.remapReference = bundleInfo.remapReference;
20895
21042
  return {
20896
21043
  // url: bundleInfo.url,
@@ -20903,9 +21050,6 @@ const createBuildSpecifierManager = ({
20903
21050
  };
20904
21051
  }
20905
21052
  if (rawUrlInfo) {
20906
- if (rawUrlInfo && !finalUrlInfo.filenameHint) {
20907
- finalUrlInfo.filenameHint = rawUrlInfo.filenameHint;
20908
- }
20909
21053
  return rawUrlInfo;
20910
21054
  }
20911
21055
  // reference injected during "shape":
@@ -20927,9 +21071,6 @@ const createBuildSpecifierManager = ({
20927
21071
  content: reference.content,
20928
21072
  contentType: reference.contentType,
20929
21073
  });
20930
- if (!finalUrlInfo.filenameHint) {
20931
- finalUrlInfo.filenameHint = reference.filenameHint;
20932
- }
20933
21074
  const rawUrlInfo = rawReference.urlInfo;
20934
21075
  await rawUrlInfo.cook();
20935
21076
  return {
@@ -20946,48 +21087,17 @@ const createBuildSpecifierManager = ({
20946
21087
  firstReference.ownerUrlInfo.url ===
20947
21088
  firstReference.ownerUrlInfo.originalUrl
20948
21089
  ) {
20949
- const rawUrlInfo = GRAPH_VISITOR.find(
20950
- rawKitchen.graph,
20951
- (rawUrlInfoCandidate) => {
20952
- const { inlineUrlSite } = rawUrlInfoCandidate;
20953
- if (!inlineUrlSite) {
20954
- return false;
20955
- }
20956
- if (
20957
- inlineUrlSite.url === firstReference.ownerUrlInfo.url &&
20958
- inlineUrlSite.line === firstReference.specifierLine &&
20959
- inlineUrlSite.column === firstReference.specifierColumn
20960
- ) {
20961
- return true;
20962
- }
20963
- if (rawUrlInfoCandidate.content === firstReference.content) {
20964
- return true;
20965
- }
20966
- if (
20967
- rawUrlInfoCandidate.originalContent === firstReference.content
20968
- ) {
20969
- return true;
20970
- }
20971
- return false;
20972
- },
20973
- );
20974
21090
  if (rawUrlInfo) {
20975
- if (!finalUrlInfo.filenameHint) {
20976
- finalUrlInfo.filenameHint = rawUrlInfo.filenameHint;
20977
- }
20978
21091
  return rawUrlInfo;
20979
21092
  }
20980
21093
  }
20981
- if (!finalUrlInfo.filenameHint) {
20982
- finalUrlInfo.filenameHint = firstReference.filenameHint;
20983
- }
20984
21094
  return {
20985
21095
  originalContent: finalUrlInfo.originalContent,
20986
21096
  content: firstReference.content,
20987
21097
  contentType: firstReference.contentType,
20988
21098
  };
20989
21099
  }
20990
- throw new Error(createDetailedMessage$1(`Cannot fetch ${rawUrl}`));
21100
+ throw new Error(createDetailedMessage$1(`${rawUrl} not found in graph`));
20991
21101
  },
20992
21102
  };
20993
21103
 
@@ -21417,10 +21527,15 @@ const createBuildSpecifierManager = ({
21417
21527
  if (urlInfo.isEntryPoint) {
21418
21528
  generateReplacement(urlInfo.firstReference);
21419
21529
  }
21420
- if (urlInfo.isInline) {
21421
- generateReplacement(urlInfo.firstReference);
21422
- }
21423
21530
  if (urlInfo.type === "sourcemap") {
21531
+ const { referenceFromOthersSet } = urlInfo;
21532
+ let lastRef;
21533
+ for (const ref of referenceFromOthersSet) {
21534
+ lastRef = ref;
21535
+ }
21536
+ generateReplacement(lastRef);
21537
+ }
21538
+ if (urlInfo.isInline) {
21424
21539
  generateReplacement(urlInfo.firstReference);
21425
21540
  }
21426
21541
  if (urlInfo.firstReference.type === "side_effect_file") {
@@ -21657,10 +21772,10 @@ const createBuildSpecifierManager = ({
21657
21772
  GRAPH_VISITOR.forEachUrlInfoStronglyReferenced(
21658
21773
  finalKitchen.graph.rootUrlInfo,
21659
21774
  (urlInfo) => {
21660
- if (!urlInfo.url.startsWith("file:")) {
21775
+ const buildUrl = urlInfoToBuildUrlMap.get(urlInfo);
21776
+ if (!buildUrl) {
21661
21777
  return;
21662
21778
  }
21663
- const buildUrl = urlInfoToBuildUrlMap.get(urlInfo);
21664
21779
  const buildSpecifier = buildUrlToBuildSpecifierMap.get(buildUrl);
21665
21780
  const buildSpecifierVersioned = versioning
21666
21781
  ? buildSpecifierToBuildSpecifierVersionedMap.get(buildSpecifier)
@@ -21712,6 +21827,33 @@ const createBuildSpecifierManager = ({
21712
21827
  };
21713
21828
  };
21714
21829
 
21830
+ const findRawUrlInfoWhenInline = (reference, rawKitchen) => {
21831
+ const rawUrlInfo = GRAPH_VISITOR.find(
21832
+ rawKitchen.graph,
21833
+ (rawUrlInfoCandidate) => {
21834
+ const { inlineUrlSite } = rawUrlInfoCandidate;
21835
+ if (!inlineUrlSite) {
21836
+ return false;
21837
+ }
21838
+ if (
21839
+ inlineUrlSite.url === reference.ownerUrlInfo.url &&
21840
+ inlineUrlSite.line === reference.specifierLine &&
21841
+ inlineUrlSite.column === reference.specifierColumn
21842
+ ) {
21843
+ return true;
21844
+ }
21845
+ if (rawUrlInfoCandidate.content === reference.content) {
21846
+ return true;
21847
+ }
21848
+ if (rawUrlInfoCandidate.originalContent === reference.content) {
21849
+ return true;
21850
+ }
21851
+ return false;
21852
+ },
21853
+ );
21854
+ return rawUrlInfo;
21855
+ };
21856
+
21715
21857
  // see https://github.com/rollup/rollup/blob/ce453507ab8457dd1ea3909d8dd7b117b2d14fab/src/utils/hashPlaceholders.ts#L1
21716
21858
  // see also "New hashing algorithm that "fixes (nearly) everything"
21717
21859
  // at https://github.com/rollup/rollup/pull/4543
@@ -22013,6 +22155,10 @@ const build = async ({
22013
22155
  signal = new AbortController().signal,
22014
22156
  handleSIGINT = true,
22015
22157
  logLevel = "info",
22158
+ logs = {
22159
+ disabled: false,
22160
+ animation: true,
22161
+ },
22016
22162
  sourceDirectoryUrl,
22017
22163
  buildDirectoryUrl,
22018
22164
  entryPoints = {},
@@ -22041,6 +22187,7 @@ const build = async ({
22041
22187
  sourceFilesConfig = {},
22042
22188
  cooldownBetweenFileEvents,
22043
22189
  watch = false,
22190
+ http = false,
22044
22191
 
22045
22192
  directoryToClean,
22046
22193
  sourcemaps = "none",
@@ -22151,8 +22298,9 @@ const build = async ({
22151
22298
  const logger = createLogger({ logLevel });
22152
22299
  const createBuildTask = (label) => {
22153
22300
  return createTaskLog(label, {
22154
- disabled: !logger.levels.debug && !logger.levels.info,
22155
- animated: !logger.levels.debug,
22301
+ disabled:
22302
+ logs.disabled || (!logger.levels.debug && !logger.levels.info),
22303
+ animated: logs.animation && !logger.levels.debug,
22156
22304
  });
22157
22305
  };
22158
22306
 
@@ -22227,6 +22375,7 @@ build ${entryPointKeys.length} entry points`);
22227
22375
  jsModuleFallback: false,
22228
22376
  },
22229
22377
  inlining: false,
22378
+ http,
22230
22379
  scenarioPlaceholders,
22231
22380
  }),
22232
22381
  ],