@package-verse/esmpack 1.0.4 → 1.0.6

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.6",
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,11 +34,14 @@
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",
40
41
  "mime": "^4.1.0",
42
+ "node-forge": "^1.3.3",
41
43
  "portfinder": "^1.0.38",
44
+ "tslib": "^2.8.1",
42
45
  "ws": "^8.19.0"
43
46
  }
44
47
  }
@@ -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,83 @@
1
- import { readFileSync } from "node:fs";
2
1
  import { IncomingMessage, ServerResponse } from "node:http";
3
- import { RegExpExtra } from "../../core/RegExpExtra.js";
4
-
5
- export default function sendJS(path: string, req: IncomingMessage, res: ServerResponse) {
6
- let text = readFileSync(path, "utf-8");
2
+ import { Babel } from "../../parser/babel.js";
3
+ import path, { parse } from "node:path";
4
+ import { ProcessOptions } from "../../ProcessArgs.js";
5
+ import { readFileSync } from "node:fs";
7
6
 
8
- // change references....
9
- // text = text.replace(/from\s*\"([^\.][^\"]+)\"/gm, `from "/node_modules/$1"`);
7
+ export default function sendJS(filePath: string, req: IncomingMessage, res: ServerResponse) {
10
8
 
11
- // remap CSS
12
- text = RegExpExtra.replaceAll(text, /from\s*\"([^\"]+)\"/gm, (
13
- { text, match }
14
- ) => {
15
- if (text) {
16
- return text;
17
- }
18
- const [matched, g] = match;
19
- if (g.endsWith(".css")) {
20
- // we need to find source...
21
- return `from "/node_modules/${g}.js"`;
22
- }
23
- if (g.startsWith(".")) {
24
- return matched;
25
- }
26
- return `from "/node_modules/${g}"`;
27
- });
28
9
 
29
- text = RegExpExtra.replaceAll(text, /import\s*\"([^\"]+)\"/gm, (
30
- { text, match }
31
- ) => {
32
- if (text) {
33
- return text;
10
+ let text = Babel.transform({ file: filePath, resolve(url) {
11
+ if (url.endsWith(".css")) {
12
+ url += ".js";
34
13
  }
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;
14
+ // check if it has no extension...
15
+ const { ext } = parse(url);
16
+ if (!ext) {
17
+ // fetch module...
18
+ const tokens = url.split("/");
19
+ let [packageName] = tokens;
20
+ if (packageName.startsWith("@")) {
21
+ packageName += "/" + tokens.shift();
41
22
  }
42
- return `import "${cssPath}.js"`;
23
+ const packageJsonPath = path.resolve(ProcessOptions.cwd, "node_modules", packageName, "package.json");
24
+ const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
25
+ const start = packageJson["module"] || packageJson["main"];
26
+ return "/node_modules/" + url + "/" + start;
43
27
  }
44
- if (g.startsWith(".")) {
45
- return matched;
28
+ if (!url.startsWith(".")) {
29
+ url = "/node_modules/" + url;
46
30
  }
47
- return `import "/node_modules/${g}"`;
48
- });
31
+ return url;
32
+ }});
33
+
34
+ const { base } = parse(filePath);
35
+
36
+ text += `\n//# sourceMappingURL=${base}.map`;
37
+
38
+ // let text = readFileSync(path, "utf-8");
39
+
40
+ // // change references....
41
+ // // text = text.replace(/from\s*\"([^\.][^\"]+)\"/gm, `from "/node_modules/$1"`);
42
+
43
+ // // remap CSS
44
+ // text = RegExpExtra.replaceAll(text, /from\s*\"([^\"]+)\"/gm, (
45
+ // { text, match }
46
+ // ) => {
47
+ // if (text) {
48
+ // return text;
49
+ // }
50
+ // const [matched, g] = match;
51
+ // if (g.endsWith(".css")) {
52
+ // // we need to find source...
53
+ // return `from "/node_modules/${g}.js"`;
54
+ // }
55
+ // if (g.startsWith(".")) {
56
+ // return matched;
57
+ // }
58
+ // return `from "/node_modules/${g}"`;
59
+ // });
60
+
61
+ // text = RegExpExtra.replaceAll(text, /import\s*\"([^\"]+)\"/gm, (
62
+ // { text, match }
63
+ // ) => {
64
+ // if (text) {
65
+ // return text;
66
+ // }
67
+ // const [matched, g] = match;
68
+ // if (g.endsWith(".css")) {
69
+ // // we need to find source...
70
+ // let cssPath = g;
71
+ // if (!cssPath.startsWith(".")) {
72
+ // cssPath = "/node_modules/" + cssPath;
73
+ // }
74
+ // return `import "${cssPath}.js"`;
75
+ // }
76
+ // if (g.startsWith(".")) {
77
+ // return matched;
78
+ // }
79
+ // return `import "/node_modules/${g}"`;
80
+ // });
49
81
 
50
82
  res.writeHead(200, {
51
83
  "content-type": "text/javascript",
@@ -7,6 +7,8 @@ import https from "https";
7
7
  import url from 'url';
8
8
  import { WebSocketServer } from "ws";
9
9
  import fs from "fs";
10
+ import forge from "node-forge";
11
+
10
12
  var netFaces = os.networkInterfaces();
11
13
 
12
14
  function createCert() {
@@ -17,7 +19,6 @@ function createCert() {
17
19
  return JSON.parse(fs.readFileSync(certPath, { encoding: "utf8", flag: "r" }));
18
20
  }
19
21
 
20
- var forge = require('node-forge');
21
22
  var pki = forge.pki;
22
23
 
23
24
  // generate a key pair or use one you have already
@@ -0,0 +1,2 @@
1
+ export default function LogDecorator(... a: any[]) {
2
+ }
@@ -6,7 +6,9 @@ import DateTime from "@web-atoms/date-time/dist/DateTime.js";
6
6
  import "./TestView.local.css";
7
7
 
8
8
  import "./TestView.global.css";
9
+ import LogDecorator from "./LogDecorator.js";
9
10
 
11
+ @LogDecorator
10
12
  export default class DateView extends HTMLElement {
11
13
 
12
14
  connectedCallback() {
package/tsconfig.json CHANGED
@@ -11,6 +11,7 @@
11
11
  "experimentalDecorators": true,
12
12
  "emitDecoratorMetadata": true,
13
13
  "noUnusedLocals": true,
14
+ "importHelpers": true,
14
15
  "jsxFactory": "XNode.create",
15
16
  "lib": [
16
17
  "ES2018",