@package-verse/esmpack 1.0.4 → 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 CHANGED
@@ -1,7 +1,9 @@
1
1
  const ESMPack = window.ESMPack ||= {};
2
2
  ESMPack.installed ||= new Set();
3
3
 
4
- ESMPack.markAsInstalled ||= (url) => ESMPack.installed.add(url);
4
+ ESMPack.markAsInstalled ||= (urls) => Array.isArray(urls)
5
+ ? urls.forEach(url => ESMPack.installed.add(url))
6
+ : ESMPack.installed.add(urls);
5
7
 
6
8
  ESMPack.installStyleSheet ||= (url) => {
7
9
 
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.4",
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",
@@ -3,12 +3,16 @@
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 ignore for all imported CSS so dynamically loaded CSS can still be imported
6
+ * 4. Packed file must set all css as installed.
7
7
  */
8
8
  export default class FilePacker {
9
9
 
10
+ globalCss = [];
11
+ localCss = [];
12
+
10
13
  constructor(
11
- public readonly root: string
14
+ public readonly root: string,
15
+ public readonly src: string
12
16
  ) {
13
17
  // empty
14
18
  }
@@ -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,51 +1,67 @@
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);
19
+
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
+ // });
49
65
 
50
66
  res.writeHead(200, {
51
67
  "content-type": "text/javascript",