@jsenv/core 36.0.1 → 36.1.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.
@@ -12,7 +12,7 @@ import {
12
12
  } from "@jsenv/node-esm-resolution";
13
13
  import { CONTENT_TYPE } from "@jsenv/utils/src/content_type/content_type.js";
14
14
 
15
- export const jsenvPluginFileUrls = ({
15
+ export const jsenvPluginProtocolFile = ({
16
16
  magicExtensions = ["inherit", ".js"],
17
17
  magicDirectoryIndex = true,
18
18
  preserveSymlinks = false,
@@ -20,7 +20,7 @@ export const jsenvPluginFileUrls = ({
20
20
  }) => {
21
21
  return [
22
22
  {
23
- name: "jsenv:file_url_resolution",
23
+ name: "jsenv:fs_redirection",
24
24
  appliesDuring: "*",
25
25
  redirectReference: (reference) => {
26
26
  // http, https, data, about, ...
@@ -30,6 +30,23 @@ export const jsenvPluginFileUrls = ({
30
30
  if (reference.isInline) {
31
31
  return null;
32
32
  }
33
+ // ignore root file url
34
+ if (reference.url === "file:///" || reference.url === "file://") {
35
+ reference.leadsToADirectory = true;
36
+ return `ignore:file:///`;
37
+ }
38
+ // ignore "./" on new URL("./")
39
+ if (
40
+ reference.subtype === "new_url_first_arg" &&
41
+ reference.specifier === "./"
42
+ ) {
43
+ return `ignore:${reference.url}`;
44
+ }
45
+ // ignore all new URL second arg
46
+ if (reference.subtype === "new_url_second_arg") {
47
+ return `ignore:${reference.url}`;
48
+ }
49
+
33
50
  const urlObject = new URL(reference.url);
34
51
  let stat;
35
52
  try {
@@ -41,11 +58,13 @@ export const jsenvPluginFileUrls = ({
41
58
  throw e;
42
59
  }
43
60
  }
61
+
44
62
  const { search, hash } = urlObject;
45
63
  let { pathname } = urlObject;
46
64
  const pathnameUsesTrailingSlash = pathname.endsWith("/");
47
65
  urlObject.search = "";
48
66
  urlObject.hash = "";
67
+
49
68
  // force trailing slash on directories
50
69
  if (stat && stat.isDirectory() && !pathnameUsesTrailingSlash) {
51
70
  urlObject.pathname = `${pathname}/`;
@@ -57,46 +76,32 @@ export const jsenvPluginFileUrls = ({
57
76
  }
58
77
 
59
78
  let url = urlObject.href;
60
- const shouldPreserve =
61
- stat &&
62
- stat.isDirectory() &&
63
- // ignore new URL second arg
64
- (reference.subtype === "new_url_second_arg" ||
65
- // ignore root file url
66
- reference.url === "file:///" ||
67
- (reference.subtype === "new_url_first_arg" &&
68
- reference.specifier === "./"));
69
-
70
- if (shouldPreserve) {
71
- reference.shouldHandle = false;
72
- } else {
73
- const shouldApplyDilesystemMagicResolution =
74
- reference.type === "js_import";
75
- if (shouldApplyDilesystemMagicResolution) {
76
- const filesystemResolution = applyFileSystemMagicResolution(url, {
77
- fileStat: stat,
78
- magicDirectoryIndex,
79
- magicExtensions: getExtensionsToTry(
80
- magicExtensions,
81
- reference.parentUrl,
82
- ),
83
- });
84
- if (filesystemResolution.stat) {
85
- stat = filesystemResolution.stat;
86
- url = filesystemResolution.url;
87
- }
79
+ const shouldApplyDilesystemMagicResolution =
80
+ reference.type === "js_import";
81
+ if (shouldApplyDilesystemMagicResolution) {
82
+ const filesystemResolution = applyFileSystemMagicResolution(url, {
83
+ fileStat: stat,
84
+ magicDirectoryIndex,
85
+ magicExtensions: getExtensionsToTry(
86
+ magicExtensions,
87
+ reference.parentUrl,
88
+ ),
89
+ });
90
+ if (filesystemResolution.stat) {
91
+ stat = filesystemResolution.stat;
92
+ url = filesystemResolution.url;
88
93
  }
89
- if (stat && stat.isDirectory()) {
90
- const directoryAllowed =
91
- reference.type === "filesystem" ||
92
- (typeof directoryReferenceAllowed === "function" &&
93
- directoryReferenceAllowed(reference)) ||
94
- directoryReferenceAllowed;
95
- if (!directoryAllowed) {
96
- const error = new Error("Reference leads to a directory");
97
- error.code = "DIRECTORY_REFERENCE_NOT_ALLOWED";
98
- throw error;
99
- }
94
+ }
95
+ if (stat && stat.isDirectory()) {
96
+ const directoryAllowed =
97
+ reference.type === "filesystem" ||
98
+ (typeof directoryReferenceAllowed === "function" &&
99
+ directoryReferenceAllowed(reference)) ||
100
+ directoryReferenceAllowed;
101
+ if (!directoryAllowed) {
102
+ const error = new Error("Reference leads to a directory");
103
+ error.code = "DIRECTORY_REFERENCE_NOT_ALLOWED";
104
+ throw error;
100
105
  }
101
106
  }
102
107
  reference.leadsToADirectory = stat && stat.isDirectory();
@@ -109,7 +114,7 @@ export const jsenvPluginFileUrls = ({
109
114
  },
110
115
  },
111
116
  {
112
- name: "jsenv:filesystem_resolution",
117
+ name: "jsenv:fs_resolution",
113
118
  appliesDuring: "*",
114
119
  resolveReference: {
115
120
  filesystem: (reference, context) => {
@@ -124,10 +129,10 @@ export const jsenvPluginFileUrls = ({
124
129
  },
125
130
  },
126
131
  {
127
- name: "jsenv:@fs_resolution",
128
- // during dev and test it's a browser running the code
132
+ name: "jsenv:@fs",
133
+ // during build it's fine to use "file://"" urls
134
+ // but during dev it's a browser running the code
129
135
  // so absolute file urls needs to be relativized
130
- // during build it's fine to use file:// urls
131
136
  appliesDuring: "dev",
132
137
  resolveReference: (reference) => {
133
138
  if (reference.specifier.startsWith("/@fs/")) {
@@ -1,16 +1,17 @@
1
- export const jsenvPluginHttpUrls = () => {
1
+ export const jsenvPluginProtocolHttp = () => {
2
2
  return {
3
- name: "jsenv:http_urls",
3
+ name: "jsenv:protocol_http",
4
4
  appliesDuring: "*",
5
5
  redirectReference: (reference) => {
6
+ // TODO: according to some pattern matching jsenv could be allowed
7
+ // to fetch and transform http urls
6
8
  if (
7
9
  reference.url.startsWith("http:") ||
8
10
  reference.url.startsWith("https:")
9
11
  ) {
10
- reference.shouldHandle = false;
12
+ return `ignore:${reference.url}`;
11
13
  }
12
- // TODO: according to some pattern matching jsenv could be allowed
13
- // to fetch and transform http urls
14
+ return null;
14
15
  },
15
16
  };
16
17
  };
@@ -1,5 +1,3 @@
1
- import { URL_META } from "@jsenv/url-meta";
2
-
3
1
  import { jsenvPluginReferenceExpectedTypes } from "./jsenv_plugin_reference_expected_types.js";
4
2
  import { jsenvPluginDirectoryReferenceAnalysis } from "./directory/jsenv_plugin_directory_reference_analysis.js";
5
3
  import { jsenvPluginDataUrlsAnalysis } from "./data_urls/jsenv_plugin_data_urls_analysis.js";
@@ -9,19 +7,12 @@ import { jsenvPluginCssReferenceAnalysis } from "./css/jsenv_plugin_css_referenc
9
7
  import { jsenvPluginJsReferenceAnalysis } from "./js/jsenv_plugin_js_reference_analysis.js";
10
8
 
11
9
  export const jsenvPluginReferenceAnalysis = ({
12
- include,
13
- supportedProtocols = ["file:", "data:", "virtual:", "http:", "https:"],
14
-
15
10
  inlineContent = true,
16
11
  inlineConvertedScript = false,
17
12
  fetchInlineUrls = true,
18
13
  allowEscapeForVersioning = false,
19
14
  }) => {
20
15
  return [
21
- jsenvPluginReferenceAnalysisInclude({
22
- include,
23
- supportedProtocols,
24
- }),
25
16
  jsenvPluginDirectoryReferenceAnalysis(),
26
17
  jsenvPluginHtmlReferenceAnalysis({
27
18
  inlineContent,
@@ -41,66 +32,6 @@ export const jsenvPluginReferenceAnalysis = ({
41
32
  ];
42
33
  };
43
34
 
44
- const jsenvPluginReferenceAnalysisInclude = ({
45
- include,
46
- supportedProtocols,
47
- }) => {
48
- // eslint-disable-next-line no-unused-vars
49
- let getIncludeInfo = (url) => undefined;
50
-
51
- return {
52
- name: "jsenv:reference_analysis_include",
53
- appliesDuring: "*",
54
- init: ({ rootDirectoryUrl }) => {
55
- if (include) {
56
- const associations = URL_META.resolveAssociations(
57
- { include },
58
- rootDirectoryUrl,
59
- );
60
- getIncludeInfo = (url) => {
61
- const { include } = URL_META.applyAssociations({
62
- url,
63
- associations,
64
- });
65
- return include;
66
- };
67
- }
68
- },
69
- redirectReference: (reference) => {
70
- if (reference.shouldHandle !== undefined) {
71
- return;
72
- }
73
- if (
74
- reference.specifier[0] === "#" &&
75
- // For Html, css and in general "#" refer to a resource in the page
76
- // so that urls must be kept intact
77
- // However for js import specifiers they have a different meaning and we want
78
- // to resolve them (https://nodejs.org/api/packages.html#imports for instance)
79
- reference.type !== "js_import"
80
- ) {
81
- reference.shouldHandle = false;
82
- return;
83
- }
84
- const includeInfo = getIncludeInfo(reference.url);
85
- if (includeInfo === true) {
86
- reference.shouldHandle = true;
87
- return;
88
- }
89
- if (includeInfo === false) {
90
- reference.shouldHandle = false;
91
- return;
92
- }
93
- const { protocol } = new URL(reference.url);
94
- const protocolIsSupported = supportedProtocols.some(
95
- (supportedProtocol) => protocol === supportedProtocol,
96
- );
97
- if (protocolIsSupported) {
98
- reference.shouldHandle = true;
99
- }
100
- },
101
- };
102
- };
103
-
104
35
  const jsenvPluginInlineContentFetcher = () => {
105
36
  return {
106
37
  name: "jsenv:inline_content_fetcher",
@@ -1,4 +1,4 @@
1
- import { urlTypeFromReference } from "../url_type_from_reference.js";
1
+ import { urlTypeFromReference } from "./url_type_from_reference.js";
2
2
  import { createNodeEsmResolver } from "./node_esm_resolver.js";
3
3
 
4
4
  export const jsenvPluginNodeEsmResolution = (resolutionConfig = {}) => {
@@ -41,10 +41,10 @@ export const jsenvPluginNodeEsmResolution = (resolutionConfig = {}) => {
41
41
  runtimeCompat,
42
42
  preservesSymlink: true,
43
43
  });
44
- if (!resolvers.js_module) {
44
+ if (resolvers.js_module === undefined) {
45
45
  resolvers.js_module = nodeEsmResolverDefault;
46
46
  }
47
- if (!resolvers.js_classic) {
47
+ if (resolvers.js_classic === undefined) {
48
48
  resolvers.js_classic = (reference, context) => {
49
49
  if (reference.subtype === "self_import_scripts_arg") {
50
50
  return nodeEsmResolverDefault(reference, context);
@@ -1,45 +1,22 @@
1
- import { urlTypeFromReference } from "../url_type_from_reference.js";
2
-
3
- export const jsenvPluginWebResolution = (resolutionConfig = {}) => {
4
- const resolvers = {};
5
- const resolveUsingWebResolution = (reference, context) => {
6
- if (reference.specifier === "/") {
7
- const { mainFilePath, rootDirectoryUrl } = context;
8
- return String(new URL(mainFilePath, rootDirectoryUrl));
9
- }
10
- if (reference.specifier[0] === "/") {
11
- return new URL(reference.specifier.slice(1), context.rootDirectoryUrl)
12
- .href;
13
- }
14
- return new URL(
15
- reference.specifier,
16
- // baseUrl happens second argument to new URL() is different from
17
- // import.meta.url or document.currentScript.src
18
- reference.baseUrl || reference.parentUrl,
19
- ).href;
20
- };
21
- Object.keys(resolutionConfig).forEach((urlType) => {
22
- const config = resolutionConfig[urlType];
23
- if (config === true) {
24
- resolvers[urlType] = resolveUsingWebResolution;
25
- } else if (config === false) {
26
- resolvers[urlType] = () => null;
27
- } else {
28
- throw new TypeError(
29
- `config must be true or false, got ${config} on "${urlType}"`,
30
- );
31
- }
32
- });
33
-
1
+ export const jsenvPluginWebResolution = () => {
34
2
  return {
35
3
  name: "jsenv:web_resolution",
36
4
  appliesDuring: "*",
37
5
  resolveReference: (reference, context) => {
38
- const urlType = urlTypeFromReference(reference, context);
39
- const resolver = resolvers[urlType];
40
- return resolver
41
- ? resolver(reference, context)
42
- : resolveUsingWebResolution(reference, context);
6
+ if (reference.specifier === "/") {
7
+ const { mainFilePath, rootDirectoryUrl } = context;
8
+ return String(new URL(mainFilePath, rootDirectoryUrl));
9
+ }
10
+ if (reference.specifier[0] === "/") {
11
+ return new URL(reference.specifier.slice(1), context.rootDirectoryUrl)
12
+ .href;
13
+ }
14
+ return new URL(
15
+ reference.specifier,
16
+ // baseUrl happens second argument to new URL() is different from
17
+ // import.meta.url or document.currentScript.src
18
+ reference.baseUrl || reference.parentUrl,
19
+ ).href;
43
20
  },
44
21
  };
45
22
  };
@@ -47,5 +47,14 @@ export const injectRibbon = ({ text }) => {
47
47
 
48
48
  const node = document.createElement("div");
49
49
  node.innerHTML = html;
50
+
51
+ const toolbarStateInLocalStorage = localStorage.hasOwnProperty(
52
+ "jsenv_toolbar",
53
+ )
54
+ ? JSON.parse(localStorage.getItem("jsenv_toolbar"))
55
+ : {};
56
+ if (toolbarStateInLocalStorage.ribbonDisplayed === false) {
57
+ node.querySelector("#jsenv_ribbon_container").style.display = "none";
58
+ }
50
59
  document.body.appendChild(node.firstChild);
51
60
  };