@package-verse/esmpack 1.0.3 → 1.0.4
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/init.js +30 -0
- package/package.json +1 -1
- package/src/pack/FilePacker.ts +20 -0
- package/src/pack/pack.ts +0 -0
- package/src/serve/send/sendCSSJS.ts +13 -13
- package/src/serve/send/sendJS.ts +4 -1
- package/src/serve/send/sendJSHost.ts +4 -1
- package/src/serve/send/sendList.ts +4 -1
- package/src/serve/send/sendLocalFile.ts +4 -1
- package/src/test/{TestView.css → TestView.global.css} +3 -3
- package/src/test/TestView.local.css +3 -0
- package/src/test/TestView.ts +7 -1
package/init.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const ESMPack = window.ESMPack ||= {};
|
|
2
|
+
ESMPack.installed ||= new Set();
|
|
3
|
+
|
|
4
|
+
ESMPack.markAsInstalled ||= (url) => ESMPack.installed.add(url);
|
|
5
|
+
|
|
6
|
+
ESMPack.installStyleSheet ||= (url) => {
|
|
7
|
+
|
|
8
|
+
const installCss = (url) => {
|
|
9
|
+
|
|
10
|
+
const link = document.createElement("link");
|
|
11
|
+
link.rel = "stylesheet";
|
|
12
|
+
link.href = url;
|
|
13
|
+
document.head.insertAdjacentElement(
|
|
14
|
+
/\.global\./i.test(url)
|
|
15
|
+
? "afterbegin"
|
|
16
|
+
: "beforeend", link);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
if (ESMPack.installed.has(url)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
ESMPack.installed.add(url);
|
|
23
|
+
if(document.readyState === "complete") {
|
|
24
|
+
installCss(url);
|
|
25
|
+
} else {
|
|
26
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
27
|
+
installCss(url);
|
|
28
|
+
}, { once: true });
|
|
29
|
+
}
|
|
30
|
+
};
|
package/package.json
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Packer must do following tasks...
|
|
3
|
+
* 1. Visit every JavaScript file
|
|
4
|
+
* 2. Change all imports, redirect CSS imports as css.js file that will include CSS on the page ...
|
|
5
|
+
* 3. Create .pack.js with all nested imports if file imports `@web-atoms/core/dist/Pack`
|
|
6
|
+
* 4. Packed file must set ignore for all imported CSS so dynamically loaded CSS can still be imported
|
|
7
|
+
*/
|
|
8
|
+
export default class FilePacker {
|
|
9
|
+
|
|
10
|
+
constructor(
|
|
11
|
+
public readonly root: string
|
|
12
|
+
) {
|
|
13
|
+
// empty
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async pack() {
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
}
|
package/src/pack/pack.ts
ADDED
|
File without changes
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
1
2
|
import { IncomingMessage, ServerResponse } from "node:http";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
2
4
|
|
|
3
5
|
export default function sendCSSJS(path: string, req: IncomingMessage, res: ServerResponse) {
|
|
4
|
-
const text = `
|
|
5
|
-
(function () {
|
|
6
|
-
|
|
7
|
-
function loadCSS() {
|
|
8
|
-
if (typeof ESMPack === "undefined") {
|
|
9
|
-
setTimeout(loadCSS, 100);
|
|
10
|
-
return;
|
|
11
|
-
}
|
|
12
|
-
ESMPack.installStyleSheet("/${path}");
|
|
13
|
-
}
|
|
14
|
-
loadCSS();
|
|
15
6
|
|
|
7
|
+
const init = fileURLToPath(import.meta.resolve("../../../init.js"));
|
|
8
|
+
const initText = readFileSync(init, "utf-8");
|
|
16
9
|
|
|
17
|
-
|
|
10
|
+
const text = `
|
|
11
|
+
(function (link) {
|
|
12
|
+
${initText};
|
|
13
|
+
ESMPack.installStyleSheet(link);
|
|
14
|
+
}("/${path}"))
|
|
18
15
|
`;
|
|
19
16
|
|
|
20
|
-
res.writeHead(200, {
|
|
17
|
+
res.writeHead(200, {
|
|
18
|
+
"content-type": "text/javascript",
|
|
19
|
+
"cache-control": "no-cache"
|
|
20
|
+
});
|
|
21
21
|
res.end(text);
|
|
22
22
|
}
|
package/src/serve/send/sendJS.ts
CHANGED
|
@@ -47,6 +47,9 @@ export default function sendJS(path: string, req: IncomingMessage, res: ServerRe
|
|
|
47
47
|
return `import "/node_modules/${g}"`;
|
|
48
48
|
});
|
|
49
49
|
|
|
50
|
-
res.writeHead(200, {
|
|
50
|
+
res.writeHead(200, {
|
|
51
|
+
"content-type": "text/javascript",
|
|
52
|
+
"cache-control": "no-cache"
|
|
53
|
+
});
|
|
51
54
|
res.end(text);
|
|
52
55
|
}
|
|
@@ -18,6 +18,9 @@ export default function sendJSHost(path: string, req: IncomingMessage, res: Serv
|
|
|
18
18
|
</html>
|
|
19
19
|
`;
|
|
20
20
|
|
|
21
|
-
res.writeHead(200, {
|
|
21
|
+
res.writeHead(200, {
|
|
22
|
+
"content-type": "text/html",
|
|
23
|
+
"cache-control": "no-cache"
|
|
24
|
+
});
|
|
22
25
|
res.end(text);
|
|
23
26
|
}
|
|
@@ -24,7 +24,10 @@ export default function sendList(reqPath, path: string, req: IncomingMessage, re
|
|
|
24
24
|
|
|
25
25
|
const text = renderDirectoryListing(reqPath, entries);
|
|
26
26
|
|
|
27
|
-
res.writeHead(200, {
|
|
27
|
+
res.writeHead(200, {
|
|
28
|
+
"Content-Type": "text/html",
|
|
29
|
+
"cache-control": "no-cache"
|
|
30
|
+
});
|
|
28
31
|
res.end(text);
|
|
29
32
|
}
|
|
30
33
|
|
|
@@ -28,6 +28,9 @@ function sendFile(filePath, res) {
|
|
|
28
28
|
res.end(`500 Internal Server Error\n\n${err.message}`);
|
|
29
29
|
}
|
|
30
30
|
});
|
|
31
|
-
res.writeHead(200, {
|
|
31
|
+
res.writeHead(200, {
|
|
32
|
+
"Content-Type": mime.getType(filePath),
|
|
33
|
+
"cache-control": "no-cache"
|
|
34
|
+
});
|
|
32
35
|
stream.pipe(res);
|
|
33
36
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
date-view {
|
|
2
|
-
display: flex;
|
|
3
|
-
color: green;
|
|
1
|
+
date-view {
|
|
2
|
+
display: flex;
|
|
3
|
+
color: green;
|
|
4
4
|
}
|
package/src/test/TestView.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import DateTime from "@web-atoms/date-time/dist/DateTime.js";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
// irrespective of loading order
|
|
4
|
+
// global must be loaded first and
|
|
5
|
+
// local must override the global style
|
|
6
|
+
import "./TestView.local.css";
|
|
7
|
+
|
|
8
|
+
import "./TestView.global.css";
|
|
3
9
|
|
|
4
10
|
export default class DateView extends HTMLElement {
|
|
5
11
|
|