@jsenv/plugin-supervisor 1.2.0 → 1.3.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.
- package/dist/jsenv_plugin_supervisor.js +20 -95
- package/package.json +4 -4
- package/src/html_supervisor.js +9 -16
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { fileURLToPath } from "node:url";
|
|
3
|
-
import { applyBabelPlugins, parseHtmlString, visitHtmlNodes, analyzeScriptNode, getHtmlNodeAttribute, getHtmlNodeText, injectHtmlNodeAsEarlyAsPossible, createHtmlNode, stringifyHtmlAst, getHtmlNodePosition, setHtmlNodeText, setHtmlNodeAttributes } from "@jsenv/ast";
|
|
3
|
+
import { applyBabelPlugins, parseHtmlString, visitHtmlNodes, analyzeScriptNode, getHtmlNodeAttribute, getHtmlNodeText, injectHtmlNodeAsEarlyAsPossible, createHtmlNode, stringifyHtmlAst, getHtmlNodePosition, getUrlForContentInsideHtml, setHtmlNodeText, setHtmlNodeAttributes } from "@jsenv/ast";
|
|
4
4
|
|
|
5
5
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
6
6
|
const intToChar = new Uint8Array(64); // 64 possible chars.
|
|
@@ -44,85 +44,6 @@ const getOriginalPosition = ({
|
|
|
44
44
|
return originalPosition;
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
-
const urlToScheme = url => {
|
|
48
|
-
const urlString = String(url);
|
|
49
|
-
const colonIndex = urlString.indexOf(":");
|
|
50
|
-
if (colonIndex === -1) {
|
|
51
|
-
return "";
|
|
52
|
-
}
|
|
53
|
-
const scheme = urlString.slice(0, colonIndex);
|
|
54
|
-
return scheme;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const urlToResource = url => {
|
|
58
|
-
const scheme = urlToScheme(url);
|
|
59
|
-
if (scheme === "file") {
|
|
60
|
-
const urlAsStringWithoutFileProtocol = String(url).slice("file://".length);
|
|
61
|
-
return urlAsStringWithoutFileProtocol;
|
|
62
|
-
}
|
|
63
|
-
if (scheme === "https" || scheme === "http") {
|
|
64
|
-
// remove origin
|
|
65
|
-
const afterProtocol = String(url).slice(scheme.length + "://".length);
|
|
66
|
-
const pathnameSlashIndex = afterProtocol.indexOf("/", "://".length);
|
|
67
|
-
const urlAsStringWithoutOrigin = afterProtocol.slice(pathnameSlashIndex);
|
|
68
|
-
return urlAsStringWithoutOrigin;
|
|
69
|
-
}
|
|
70
|
-
const urlAsStringWithoutProtocol = String(url).slice(scheme.length + 1);
|
|
71
|
-
return urlAsStringWithoutProtocol;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const urlToPathname = url => {
|
|
75
|
-
const resource = urlToResource(url);
|
|
76
|
-
const pathname = resourceToPathname(resource);
|
|
77
|
-
return pathname;
|
|
78
|
-
};
|
|
79
|
-
const resourceToPathname = resource => {
|
|
80
|
-
const searchSeparatorIndex = resource.indexOf("?");
|
|
81
|
-
if (searchSeparatorIndex > -1) {
|
|
82
|
-
return resource.slice(0, searchSeparatorIndex);
|
|
83
|
-
}
|
|
84
|
-
const hashIndex = resource.indexOf("#");
|
|
85
|
-
if (hashIndex > -1) {
|
|
86
|
-
return resource.slice(0, hashIndex);
|
|
87
|
-
}
|
|
88
|
-
return resource;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const urlToFilename = url => {
|
|
92
|
-
const pathname = urlToPathname(url);
|
|
93
|
-
const pathnameBeforeLastSlash = pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
|
|
94
|
-
const slashLastIndex = pathnameBeforeLastSlash.lastIndexOf("/");
|
|
95
|
-
const filename = slashLastIndex === -1 ? pathnameBeforeLastSlash : pathnameBeforeLastSlash.slice(slashLastIndex + 1);
|
|
96
|
-
return filename;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const generateInlineContentUrl = ({
|
|
100
|
-
url,
|
|
101
|
-
extension,
|
|
102
|
-
basename,
|
|
103
|
-
line,
|
|
104
|
-
column,
|
|
105
|
-
lineEnd,
|
|
106
|
-
columnEnd
|
|
107
|
-
}) => {
|
|
108
|
-
let generatedName = "";
|
|
109
|
-
if (basename !== undefined) {
|
|
110
|
-
generatedName += basename;
|
|
111
|
-
}
|
|
112
|
-
if (line !== undefined && column !== undefined) {
|
|
113
|
-
generatedName = "L".concat(line, "C").concat(column);
|
|
114
|
-
if (lineEnd !== undefined && columnEnd !== undefined) {
|
|
115
|
-
generatedName += "-L".concat(lineEnd, "C").concat(columnEnd);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
const filenameRaw = urlToFilename(url);
|
|
119
|
-
const filename = "".concat(filenameRaw, "@").concat(generatedName).concat(extension);
|
|
120
|
-
// ideally we should keep query params from url
|
|
121
|
-
// maybe we could use a custom scheme like "inline:"
|
|
122
|
-
const inlineContentUrl = new URL(filename, url).href;
|
|
123
|
-
return inlineContentUrl;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
47
|
// consider switching to https://babeljs.io/docs/en/babel-code-frame
|
|
127
48
|
// https://github.com/postcss/postcss/blob/fd30d3df5abc0954a0ec642a3cdc644ab2aacf9c/lib/css-syntax-error.js#L43
|
|
128
49
|
// https://github.com/postcss/postcss/blob/fd30d3df5abc0954a0ec642a3cdc644ab2aacf9c/lib/terminal-highlight.js#L50
|
|
@@ -397,6 +318,16 @@ const SOURCEMAP = {
|
|
|
397
318
|
}[contentType];
|
|
398
319
|
return read ? read(content) : null;
|
|
399
320
|
},
|
|
321
|
+
removeComment: ({
|
|
322
|
+
contentType,
|
|
323
|
+
content
|
|
324
|
+
}) => {
|
|
325
|
+
return SOURCEMAP.writeComment({
|
|
326
|
+
contentType,
|
|
327
|
+
content,
|
|
328
|
+
specifier: ""
|
|
329
|
+
});
|
|
330
|
+
},
|
|
400
331
|
writeComment: ({
|
|
401
332
|
contentType,
|
|
402
333
|
content,
|
|
@@ -471,8 +402,12 @@ const javascriptSourceMappingUrlCommentRegexp = /\/\/ ?# ?sourceMappingURL=([^\s
|
|
|
471
402
|
const cssSourceMappingUrlCommentRegExp = /\/\*# ?sourceMappingURL=([^\s'"]+) \*\//g;
|
|
472
403
|
|
|
473
404
|
// ${"//#"} is to avoid a parser thinking there is a sourceMappingUrl for this file
|
|
474
|
-
const writeJavaScriptSourceMappingURL = value =>
|
|
475
|
-
|
|
405
|
+
const writeJavaScriptSourceMappingURL = value => {
|
|
406
|
+
return "//#".concat(" sourceMappingURL=", value);
|
|
407
|
+
};
|
|
408
|
+
const writeCssSourceMappingUrl = value => {
|
|
409
|
+
return "/*# sourceMappingURL=".concat(value, " */");
|
|
410
|
+
};
|
|
476
411
|
const replaceSourceMappingUrl = (source, regexp, callback) => {
|
|
477
412
|
let lastSourceMappingUrl;
|
|
478
413
|
let matchSourceMappingUrl;
|
|
@@ -777,25 +712,17 @@ const injectSupervisorIntoHTML = async ({
|
|
|
777
712
|
{
|
|
778
713
|
const handleInlineScript = (scriptNode, {
|
|
779
714
|
type,
|
|
780
|
-
extension,
|
|
781
715
|
textContent
|
|
782
716
|
}) => {
|
|
783
717
|
const {
|
|
784
718
|
line,
|
|
785
719
|
column,
|
|
786
|
-
lineEnd,
|
|
787
|
-
columnEnd,
|
|
788
720
|
isOriginal
|
|
789
721
|
} = getHtmlNodePosition(scriptNode, {
|
|
790
722
|
preferOriginal: true
|
|
791
723
|
});
|
|
792
|
-
const inlineScriptUrl =
|
|
793
|
-
url
|
|
794
|
-
extension: extension || ".js",
|
|
795
|
-
line,
|
|
796
|
-
column,
|
|
797
|
-
lineEnd,
|
|
798
|
-
columnEnd
|
|
724
|
+
const inlineScriptUrl = getUrlForContentInsideHtml(scriptNode, {
|
|
725
|
+
htmlUrl: url
|
|
799
726
|
});
|
|
800
727
|
const inlineScriptSrc = generateInlineScriptSrc({
|
|
801
728
|
type,
|
|
@@ -898,8 +825,7 @@ const injectSupervisorIntoHTML = async ({
|
|
|
898
825
|
visitHtmlNodes(htmlAst, {
|
|
899
826
|
script: scriptNode => {
|
|
900
827
|
const {
|
|
901
|
-
type
|
|
902
|
-
extension
|
|
828
|
+
type
|
|
903
829
|
} = analyzeScriptNode(scriptNode);
|
|
904
830
|
if (type !== "js_classic" && type !== "js_module") {
|
|
905
831
|
return;
|
|
@@ -915,7 +841,6 @@ const injectSupervisorIntoHTML = async ({
|
|
|
915
841
|
if (scriptNodeText) {
|
|
916
842
|
handleInlineScript(scriptNode, {
|
|
917
843
|
type,
|
|
918
|
-
extension,
|
|
919
844
|
textContent: scriptNodeText
|
|
920
845
|
});
|
|
921
846
|
return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/plugin-supervisor",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"/dist/"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@jsenv/ast": "5.
|
|
30
|
-
"@jsenv/urls": "2.
|
|
31
|
-
"@jsenv/sourcemap": "1.
|
|
29
|
+
"@jsenv/ast": "5.1.0",
|
|
30
|
+
"@jsenv/urls": "2.2.0",
|
|
31
|
+
"@jsenv/sourcemap": "1.2.0",
|
|
32
32
|
"launch-editor": "2.6.0"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
package/src/html_supervisor.js
CHANGED
|
@@ -57,8 +57,9 @@ import {
|
|
|
57
57
|
getHtmlNodePosition,
|
|
58
58
|
getHtmlNodeText,
|
|
59
59
|
setHtmlNodeText,
|
|
60
|
+
getUrlForContentInsideHtml,
|
|
60
61
|
} from "@jsenv/ast";
|
|
61
|
-
import {
|
|
62
|
+
import { urlToRelativeUrl } from "@jsenv/urls";
|
|
62
63
|
|
|
63
64
|
import { injectSupervisorIntoJs } from "./js_supervisor.js";
|
|
64
65
|
|
|
@@ -86,19 +87,12 @@ export const injectSupervisorIntoHTML = async (
|
|
|
86
87
|
const scriptInfos = [];
|
|
87
88
|
// 1. Find inline and remote scripts
|
|
88
89
|
{
|
|
89
|
-
const handleInlineScript = (
|
|
90
|
-
scriptNode,
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
const inlineScriptUrl = generateInlineContentUrl({
|
|
96
|
-
url,
|
|
97
|
-
extension: extension || ".js",
|
|
98
|
-
line,
|
|
99
|
-
column,
|
|
100
|
-
lineEnd,
|
|
101
|
-
columnEnd,
|
|
90
|
+
const handleInlineScript = (scriptNode, { type, textContent }) => {
|
|
91
|
+
const { line, column, isOriginal } = getHtmlNodePosition(scriptNode, {
|
|
92
|
+
preferOriginal: true,
|
|
93
|
+
});
|
|
94
|
+
const inlineScriptUrl = getUrlForContentInsideHtml(scriptNode, {
|
|
95
|
+
htmlUrl: url,
|
|
102
96
|
});
|
|
103
97
|
const inlineScriptSrc = generateInlineScriptSrc({
|
|
104
98
|
type,
|
|
@@ -187,7 +181,7 @@ export const injectSupervisorIntoHTML = async (
|
|
|
187
181
|
};
|
|
188
182
|
visitHtmlNodes(htmlAst, {
|
|
189
183
|
script: (scriptNode) => {
|
|
190
|
-
const { type
|
|
184
|
+
const { type } = analyzeScriptNode(scriptNode);
|
|
191
185
|
if (type !== "js_classic" && type !== "js_module") {
|
|
192
186
|
return;
|
|
193
187
|
}
|
|
@@ -203,7 +197,6 @@ export const injectSupervisorIntoHTML = async (
|
|
|
203
197
|
if (scriptNodeText) {
|
|
204
198
|
handleInlineScript(scriptNode, {
|
|
205
199
|
type,
|
|
206
|
-
extension,
|
|
207
200
|
textContent: scriptNodeText,
|
|
208
201
|
});
|
|
209
202
|
return;
|