@package-verse/esmpack 1.0.7 → 1.0.9
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/.vscode/settings.json +2 -0
- package/empty.js +1 -0
- package/init.js +0 -12
- package/package.json +1 -1
- package/src/pack/FilePacker.ts +19 -2
- package/src/serve/send/packageInfo.ts +10 -0
- package/src/serve/send/sendJS.ts +24 -27
- package/src/serve/send/sendJSHost.ts +10 -1
- package/src/test/Clock2.svg +1 -0
- package/src/test/TestView.ts +6 -1
package/.vscode/settings.json
CHANGED
package/empty.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// this is just an empty module placeholder for CSS modules
|
package/init.js
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
const ESMPack = window.ESMPack ||= {};
|
|
2
|
-
ESMPack.installed ||= new Set();
|
|
3
|
-
|
|
4
|
-
ESMPack.markAsInstalled ||= (urls) => Array.isArray(urls)
|
|
5
|
-
? urls.forEach(url => ESMPack.installed.add(url))
|
|
6
|
-
: ESMPack.installed.add(urls);
|
|
7
|
-
|
|
8
2
|
ESMPack.installStyleSheet ||= (url) => {
|
|
9
|
-
|
|
10
3
|
const installCss = (url) => {
|
|
11
4
|
|
|
12
5
|
const link = document.createElement("link");
|
|
@@ -17,11 +10,6 @@ ESMPack.installStyleSheet ||= (url) => {
|
|
|
17
10
|
? "afterbegin"
|
|
18
11
|
: "beforeend", link);
|
|
19
12
|
};
|
|
20
|
-
|
|
21
|
-
if (ESMPack.installed.has(url)) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
ESMPack.installed.add(url);
|
|
25
13
|
if(document.readyState === "complete") {
|
|
26
14
|
installCss(url);
|
|
27
15
|
} else {
|
package/package.json
CHANGED
package/src/pack/FilePacker.ts
CHANGED
|
@@ -3,7 +3,23 @@
|
|
|
3
3
|
* 1. Visit every JavaScript file
|
|
4
4
|
* 2. Change all imports, redirect CSS imports as css.js file that will include CSS on the page ...
|
|
5
5
|
* 3. Create .pack.js with all nested imports if file imports `@web-atoms/core/dist/Pack`
|
|
6
|
-
* 4. Packed file must set all css as installed.
|
|
6
|
+
* 4. Packed file must set all css as installed. And other modules will simply return absolute paths.
|
|
7
|
+
*
|
|
8
|
+
* For App.js following packed scripts will be generated.
|
|
9
|
+
* 1. App.pack.js
|
|
10
|
+
* Push Import Maps script tag
|
|
11
|
+
* Add import map for non js modules as well, and for this
|
|
12
|
+
* every nested dependency must be inspected.
|
|
13
|
+
* Push empty module for CSS
|
|
14
|
+
* Imports all nested dependencies of App.js that should not contain fully qualified path
|
|
15
|
+
* Import dynamically loaded modules as well
|
|
16
|
+
* Loads App.pack.global.css
|
|
17
|
+
* Loads App.pack.local.css
|
|
18
|
+
* Imports App.js dynamically so CSS can be ready before hosting the User interface
|
|
19
|
+
* 2. App.pack.global.css
|
|
20
|
+
* 3. App.pack.local.css
|
|
21
|
+
* 4. App.pack.{hash-of-absolute-module-path}.js <-- this will be a dependency for non js module such as image or json etc.
|
|
22
|
+
* This will load an absolute path via resolve
|
|
7
23
|
*/
|
|
8
24
|
export default class FilePacker {
|
|
9
25
|
|
|
@@ -12,7 +28,8 @@ export default class FilePacker {
|
|
|
12
28
|
|
|
13
29
|
constructor(
|
|
14
30
|
public readonly root: string,
|
|
15
|
-
public readonly src: string
|
|
31
|
+
public readonly src: string,
|
|
32
|
+
public readonly prefix: string
|
|
16
33
|
) {
|
|
17
34
|
// empty
|
|
18
35
|
}
|
|
@@ -5,3 +5,13 @@ import { join } from "path";
|
|
|
5
5
|
const jsonText = readFileSync(join(ProcessOptions.cwd, "package.json"), "utf-8");
|
|
6
6
|
|
|
7
7
|
export const packageInfo = JSON.parse(jsonText);
|
|
8
|
+
|
|
9
|
+
const imports = {
|
|
10
|
+
[packageInfo.name + "/"]: "/"
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
for (const [key] of Object.entries(packageInfo.dependencies)) {
|
|
14
|
+
imports[key + "/"] = "/node_modules/" + key + "/";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const importMap = { imports };
|
package/src/serve/send/sendJS.ts
CHANGED
|
@@ -8,6 +8,9 @@ export default function sendJS(filePath: string, req: IncomingMessage, res: Serv
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
let text = Babel.transform({ file: filePath, resolve(url, sourceFile) {
|
|
11
|
+
|
|
12
|
+
const originalUrl = url;
|
|
13
|
+
|
|
11
14
|
// check if it has no extension...
|
|
12
15
|
const { ext } = parse(url);
|
|
13
16
|
if (!ext) {
|
|
@@ -20,39 +23,24 @@ export default function sendJS(filePath: string, req: IncomingMessage, res: Serv
|
|
|
20
23
|
if (packageName.startsWith("@")) {
|
|
21
24
|
packageName += "/" + tokens.shift();
|
|
22
25
|
}
|
|
26
|
+
|
|
23
27
|
const packageJsonPath = path.resolve(ProcessOptions.cwd, "node_modules", packageName, "package.json");
|
|
24
28
|
if (existsSync(packageJsonPath)) {
|
|
25
29
|
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
26
30
|
const start = packageJson["module"] || packageJson["main"];
|
|
27
|
-
return
|
|
31
|
+
return url + "/" + start;
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
}
|
|
31
35
|
|
|
32
36
|
|
|
33
37
|
if (!url.startsWith(".")) {
|
|
34
|
-
const resolved = path.resolve(ProcessOptions.cwd, "node_modules/" + url);
|
|
35
|
-
if (existsSync(resolved)) {
|
|
36
|
-
url = "/node_modules/" + url;
|
|
37
|
-
return url;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const tokens = url.split("/");
|
|
41
|
-
let packageName = tokens.shift();
|
|
42
|
-
if (packageName.startsWith("@")) {
|
|
43
|
-
packageName += "/" + tokens.shift();
|
|
44
|
-
}
|
|
45
38
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
url = tokens.join("/");
|
|
49
|
-
if (!url.endsWith(".js")) {
|
|
50
|
-
return "/" + url + ".js";
|
|
51
|
-
}
|
|
52
|
-
return "/" + url;
|
|
39
|
+
if (!url.endsWith(".js")) {
|
|
40
|
+
url += ".js";
|
|
53
41
|
}
|
|
54
42
|
|
|
55
|
-
return
|
|
43
|
+
return url;
|
|
56
44
|
}
|
|
57
45
|
|
|
58
46
|
const jsFile = path.resolve(path.dirname(filePath), url);
|
|
@@ -63,14 +51,23 @@ export default function sendJS(filePath: string, req: IncomingMessage, res: Serv
|
|
|
63
51
|
return url;
|
|
64
52
|
}
|
|
65
53
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
54
|
+
if (url.endsWith(".js")) {
|
|
55
|
+
return url;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// is it referenced from source...
|
|
59
|
+
const dir = path.dirname(filePath);
|
|
60
|
+
const absoluteSourcePath = path.resolve( dir, path.dirname(sourceFile));
|
|
61
|
+
console.log(`Absolute Path is ${absoluteSourcePath}`);
|
|
62
|
+
const referencedAbsolutePath = path.join(absoluteSourcePath, url);
|
|
63
|
+
if (existsSync(referencedAbsolutePath)) {
|
|
64
|
+
const relative = path.relative(dir, referencedAbsolutePath).replaceAll("\\", "/");
|
|
65
|
+
if(!relative.endsWith(".js")) {
|
|
66
|
+
return relative + ".js";
|
|
67
|
+
}
|
|
68
|
+
return relative;
|
|
72
69
|
}
|
|
73
|
-
return
|
|
70
|
+
return originalUrl;
|
|
74
71
|
|
|
75
72
|
}});
|
|
76
73
|
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { importMap, packageInfo } from "./packageInfo.js";
|
|
2
3
|
|
|
3
4
|
export default function sendJSHost(path: string, req: IncomingMessage, res: ServerResponse) {
|
|
5
|
+
|
|
6
|
+
// generate import maps
|
|
7
|
+
// for dynamic scripts
|
|
8
|
+
// this is to avoid path renaming and support dynamic module loading
|
|
9
|
+
|
|
4
10
|
const text = `
|
|
5
11
|
<!DOCTYPE html>
|
|
6
12
|
<html lang="en">
|
|
@@ -10,9 +16,12 @@ export default function sendJSHost(path: string, req: IncomingMessage, res: Serv
|
|
|
10
16
|
<title>Index of ${path}</title>
|
|
11
17
|
</head>
|
|
12
18
|
<body>
|
|
19
|
+
<script type="importmap">
|
|
20
|
+
${JSON.stringify(importMap, void 0, 2)}
|
|
21
|
+
</script>
|
|
13
22
|
<script>
|
|
14
23
|
const cs = document.currentScript;
|
|
15
|
-
import("/${path}").then((r) => ESMPack.render(r, cs), (error) => cs.replaceWith(document.createTextNode(error.stack || error)));
|
|
24
|
+
import("${packageInfo.name}/${path}").then((r) => ESMPack.render(r, cs), (error) => cs.replaceWith(document.createTextNode(error.stack || error)));
|
|
16
25
|
</script>
|
|
17
26
|
</body>
|
|
18
27
|
</html>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><!--!Font Awesome Pro v7.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2026 Fonticons, Inc.--><path opacity=".4" d="M96 352C96 402.3 112.6 448.8 140.6 486.2L105.4 521.4C92.9 533.9 92.9 554.2 105.4 566.7C117.9 579.2 138.2 579.2 150.7 566.7L185.9 531.5C223.3 559.5 269.7 576.1 320 576.1C370.3 576.1 416.8 559.5 454.2 531.5L489.4 566.7C501.9 579.2 522.2 579.2 534.7 566.7C547.2 554.2 547.2 533.9 534.7 521.4L499.5 486.2C527.5 448.8 544.1 402.4 544.1 352C544.1 228.3 443.8 128 320.1 128C196.4 128 96 228.3 96 352zM296 248C296 234.7 306.7 224 320 224C333.3 224 344 234.7 344 248L344 342.1L393 391.1C402.4 400.5 402.4 415.7 393 425C383.6 434.3 368.4 434.4 359.1 425L303.1 369C298.6 364.5 296.1 358.4 296.1 352L296.1 248z"/><path d="M543.5 196.9C550 206.3 564 207 568.4 196.5C573.3 185 576 172.4 576 159.2C576 106.6 533.4 64 480.8 64C461.5 64 443.5 69.8 428.5 79.7C418.9 86 422.2 99.6 432.6 104.3C477.4 124.7 515.7 156.9 543.6 196.9zM71.6 196.5C76.1 207 90 206.3 96.5 196.9C124.3 156.9 162.6 124.7 207.5 104.3C217.9 99.6 221.2 86 211.6 79.7C196.6 69.8 178.6 64 159.3 64C106.7 64 64.1 106.6 64.1 159.2C64.1 172.4 66.8 185 71.7 196.5zM344 248C344 234.7 333.3 224 320 224C306.7 224 296 234.7 296 248L296 352C296 358.4 298.5 364.5 303 369L359 425C368.4 434.4 383.6 434.4 392.9 425C402.2 415.6 402.3 400.4 392.9 391.1L343.9 342.1L343.9 248z"/></svg>
|
package/src/test/TestView.ts
CHANGED
|
@@ -10,16 +10,21 @@ import "./TestView.local.css";
|
|
|
10
10
|
import "./TestView.global.css";
|
|
11
11
|
import LogDecorator from "./LogDecorator.js";
|
|
12
12
|
|
|
13
|
+
import Clock2 from "./Clock2.svg";
|
|
14
|
+
|
|
13
15
|
@LogDecorator
|
|
14
16
|
export default class DateView extends HTMLElement {
|
|
15
17
|
|
|
16
18
|
connectedCallback() {
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
let img = document.createElement("img");
|
|
19
21
|
img.src = clock;
|
|
20
22
|
const span = document.createElement("span");
|
|
21
23
|
this.appendChild(img);
|
|
22
24
|
this.appendChild(span);
|
|
25
|
+
img = document.createElement("img");
|
|
26
|
+
img.src = Clock2;
|
|
27
|
+
this.appendChild(img);
|
|
23
28
|
setInterval(() => {
|
|
24
29
|
const now = DateTime.now;
|
|
25
30
|
span.textContent = now.toJSON();
|