@package-verse/esmpack 1.0.3 → 1.0.5

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 ADDED
@@ -0,0 +1,32 @@
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
+ ESMPack.installStyleSheet ||= (url) => {
9
+
10
+ const installCss = (url) => {
11
+
12
+ const link = document.createElement("link");
13
+ link.rel = "stylesheet";
14
+ link.href = url;
15
+ document.head.insertAdjacentElement(
16
+ /\.global\./i.test(url)
17
+ ? "afterbegin"
18
+ : "beforeend", link);
19
+ };
20
+
21
+ if (ESMPack.installed.has(url)) {
22
+ return;
23
+ }
24
+ ESMPack.installed.add(url);
25
+ if(document.readyState === "complete") {
26
+ installCss(url);
27
+ } else {
28
+ document.addEventListener("DOMContentLoaded", () => {
29
+ installCss(url);
30
+ }, { once: true });
31
+ }
32
+ };
package/pack.js ADDED
@@ -0,0 +1 @@
1
+ import "./dist/pack/pack.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@package-verse/esmpack",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "ESM Pack Packer and Web Server with PostCSS and ESM Loader",
5
5
  "homepage": "https://github.com/package-verse/esmpack#readme",
6
6
  "bugs": {
@@ -34,6 +34,7 @@
34
34
  "postcss-url": "^10.1.3"
35
35
  },
36
36
  "dependencies": {
37
+ "@babel/core": "^7.29.0",
37
38
  "@web-atoms/date-time": "^3.0.3",
38
39
  "colors": "^1.4.0",
39
40
  "http-proxy-middleware": "^3.0.5",
@@ -0,0 +1,24 @@
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 all css as installed.
7
+ */
8
+ export default class FilePacker {
9
+
10
+ globalCss = [];
11
+ localCss = [];
12
+
13
+ constructor(
14
+ public readonly root: string,
15
+ public readonly src: string
16
+ ) {
17
+ // empty
18
+ }
19
+
20
+ async pack() {
21
+
22
+ }
23
+
24
+ }
File without changes
@@ -0,0 +1,45 @@
1
+ import { transform } from "@babel/core";
2
+ import { readFileSync } from "fs";
3
+
4
+ export class Babel {
5
+
6
+ static transform({ file, resolve }: { file: string, resolve: (url: string) => string }) {
7
+
8
+ const presets = {
9
+ sourceType: "module",
10
+ sourceMaps: true,
11
+ inputSourceMap: true,
12
+ compact: false,
13
+ comments: false,
14
+ getModuleId: () => "v",
15
+ "plugins": [
16
+ [
17
+ function(babel) {
18
+ return {
19
+ name: "Import Transformer",
20
+ visitor: {
21
+ ImportDeclaration(node) {
22
+ const e = node.node;
23
+ let source = e.source?.value;
24
+ if (!source) {
25
+ return node;
26
+ }
27
+ source = resolve(source);
28
+ e.source.value = source;
29
+ return node;
30
+ },
31
+ }
32
+ };
33
+ }
34
+ ]
35
+
36
+ ]
37
+ };
38
+
39
+ const p = { ... presets, filename: file };
40
+ const code = readFileSync(file, "utf8");
41
+ const result = transform(code, p);
42
+ return result.code;
43
+ }
44
+
45
+ }
@@ -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, { "content-type": "text/javascript"});
17
+ res.writeHead(200, {
18
+ "content-type": "text/javascript",
19
+ "cache-control": "no-cache"
20
+ });
21
21
  res.end(text);
22
22
  }
@@ -1,52 +1,71 @@
1
- import { readFileSync } from "node:fs";
2
1
  import { IncomingMessage, ServerResponse } from "node:http";
3
- import { RegExpExtra } from "../../core/RegExpExtra.js";
2
+ import { Babel } from "../../parser/babel.js";
3
+ import { parse } from "node:path";
4
4
 
5
5
  export default function sendJS(path: string, req: IncomingMessage, res: ServerResponse) {
6
- let text = readFileSync(path, "utf-8");
7
6
 
8
- // change references....
9
- // text = text.replace(/from\s*\"([^\.][^\"]+)\"/gm, `from "/node_modules/$1"`);
10
7
 
11
- // remap CSS
12
- text = RegExpExtra.replaceAll(text, /from\s*\"([^\"]+)\"/gm, (
13
- { text, match }
14
- ) => {
15
- if (text) {
16
- return text;
8
+ let text = Babel.transform({ file: path, resolve(url) {
9
+ if (url.endsWith(".css")) {
10
+ url += ".js";
17
11
  }
18
- const [matched, g] = match;
19
- if (g.endsWith(".css")) {
20
- // we need to find source...
21
- return `from "/node_modules/${g}.js"`;
12
+ if (!url.startsWith(".")) {
13
+ url = "/node_modules/" + url;
22
14
  }
23
- if (g.startsWith(".")) {
24
- return matched;
25
- }
26
- return `from "/node_modules/${g}"`;
27
- });
15
+ return url;
16
+ }});
28
17
 
29
- text = RegExpExtra.replaceAll(text, /import\s*\"([^\"]+)\"/gm, (
30
- { text, match }
31
- ) => {
32
- if (text) {
33
- return text;
34
- }
35
- const [matched, g] = match;
36
- if (g.endsWith(".css")) {
37
- // we need to find source...
38
- let cssPath = g;
39
- if (!cssPath.startsWith(".")) {
40
- cssPath = "/node_modules/" + cssPath;
41
- }
42
- return `import "${cssPath}.js"`;
43
- }
44
- if (g.startsWith(".")) {
45
- return matched;
46
- }
47
- return `import "/node_modules/${g}"`;
48
- });
18
+ const { base } = parse(path);
49
19
 
50
- res.writeHead(200, { "content-type": "text/javascript "});
20
+ text += `\n//# sourceMappingURL=${base}.map`;
21
+
22
+ // let text = readFileSync(path, "utf-8");
23
+
24
+ // // change references....
25
+ // // text = text.replace(/from\s*\"([^\.][^\"]+)\"/gm, `from "/node_modules/$1"`);
26
+
27
+ // // remap CSS
28
+ // text = RegExpExtra.replaceAll(text, /from\s*\"([^\"]+)\"/gm, (
29
+ // { text, match }
30
+ // ) => {
31
+ // if (text) {
32
+ // return text;
33
+ // }
34
+ // const [matched, g] = match;
35
+ // if (g.endsWith(".css")) {
36
+ // // we need to find source...
37
+ // return `from "/node_modules/${g}.js"`;
38
+ // }
39
+ // if (g.startsWith(".")) {
40
+ // return matched;
41
+ // }
42
+ // return `from "/node_modules/${g}"`;
43
+ // });
44
+
45
+ // text = RegExpExtra.replaceAll(text, /import\s*\"([^\"]+)\"/gm, (
46
+ // { text, match }
47
+ // ) => {
48
+ // if (text) {
49
+ // return text;
50
+ // }
51
+ // const [matched, g] = match;
52
+ // if (g.endsWith(".css")) {
53
+ // // we need to find source...
54
+ // let cssPath = g;
55
+ // if (!cssPath.startsWith(".")) {
56
+ // cssPath = "/node_modules/" + cssPath;
57
+ // }
58
+ // return `import "${cssPath}.js"`;
59
+ // }
60
+ // if (g.startsWith(".")) {
61
+ // return matched;
62
+ // }
63
+ // return `import "/node_modules/${g}"`;
64
+ // });
65
+
66
+ res.writeHead(200, {
67
+ "content-type": "text/javascript",
68
+ "cache-control": "no-cache"
69
+ });
51
70
  res.end(text);
52
71
  }
@@ -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, { "content-type": "text/html"});
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, {"Content-Type": "text/html"});
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, { "Content-Type": mime.getType(filePath) });
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
  }
@@ -0,0 +1,3 @@
1
+ date-view {
2
+ color: red;
3
+ }
@@ -1,5 +1,11 @@
1
1
  import DateTime from "@web-atoms/date-time/dist/DateTime.js";
2
- import "./TestView.css";
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