@sealcode/sealgen 0.12.10 → 0.12.11

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.
@@ -1,4 +1,5 @@
1
- export namespace remove_named_imports {
1
+ export function removeNamedImports(content: any): any;
2
+ export namespace remove_named_imports_plugin {
2
3
  const name: string;
3
4
  function setup(build: any): void;
4
5
  }
package/dist/build.js CHANGED
@@ -9,8 +9,11 @@ import { generateRoutes } from "./generate-routes.js";
9
9
  import { generateComponents } from "./generate-components.js";
10
10
  import { make_notifier } from "./notifier.js";
11
11
  import getPort from "get-port";
12
- import { remove_named_imports } from "../remove-named-imports.js";
12
+ import { removeNamedImports } from "../remove-named-imports.js";
13
+ import md5 from "md5";
13
14
  import _locreq from "locreq";
15
+ import path, { basename, extname } from "path";
16
+ import { copyFile } from "fs/promises";
14
17
  const target_locreq = _locreq(process.cwd());
15
18
  async function get_notifier_port() {
16
19
  return getPort({ port: 4e3 });
@@ -25,15 +28,30 @@ async function write_notifier_config(watch, port) {
25
28
  JSON.stringify(config)
26
29
  );
27
30
  }
31
+ function addSuffix(filename, suffix) {
32
+ return filename.replace(/(\.[a-zA-Z0-9]+)?$/, suffix + "$1");
33
+ }
34
+ async function makeResourceURL(file_path) {
35
+ const hash_base = file_path + (await fs.stat(file_path)).mtimeMs;
36
+ const hash = md5(hash_base);
37
+ const public_url = "dist/" + addSuffix(basename(file_path), "-" + hash.slice(0, 8));
38
+ const public_dir = "public";
39
+ await copyFile(
40
+ file_path,
41
+ target_locreq.resolve(path.resolve(public_dir, public_url))
42
+ );
43
+ return "/" + public_url;
44
+ }
28
45
  async function build_ts() {
29
46
  await Promise.all([
30
47
  generateCollections(),
31
48
  generateRoutes(),
32
49
  generateComponents()
33
50
  ]);
34
- const entryPoints = (await glob("./src/back/**/*.{ts,tsx}")).filter(
35
- (path) => !path.includes(".#")
36
- );
51
+ const embeddable_file_extensions = ["svg", "png", "jpg", "jpeg", "webp"];
52
+ const entryPoints = (await glob(
53
+ `./src/back/**/*.{ts,tsx,${embeddable_file_extensions.join(",")}}`
54
+ )).filter((path2) => !path2.includes(".#"));
37
55
  try {
38
56
  await build({
39
57
  entryPoints,
@@ -45,7 +63,72 @@ async function build_ts() {
45
63
  platform: "node",
46
64
  target: "es2022",
47
65
  format: "esm",
48
- plugins: [remove_named_imports]
66
+ loader: Object.fromEntries(
67
+ embeddable_file_extensions.map((ext) => ["." + ext, "js"])
68
+ ),
69
+ plugins: [
70
+ {
71
+ name: "sealgen-rewrite-asset-imports",
72
+ setup(build2) {
73
+ build2.onLoad(
74
+ { filter: /\.tsx?$/ },
75
+ async (args) => {
76
+ let contents = await fs.readFile(
77
+ args.path,
78
+ "utf8"
79
+ );
80
+ for (const ext of embeddable_file_extensions) {
81
+ contents = contents.replaceAll(
82
+ new RegExp(
83
+ `^import (w+|{[^}]+}) from "([^"]+.${ext})";`,
84
+ "gm"
85
+ ),
86
+ (line) => line.replace(`.${ext}"`, '.js"')
87
+ );
88
+ }
89
+ contents = removeNamedImports(contents);
90
+ return {
91
+ contents,
92
+ loader: extname(args.path) === ".tsx" ? "tsx" : "ts"
93
+ };
94
+ }
95
+ );
96
+ }
97
+ },
98
+ {
99
+ name: "sealgen-load-assets",
100
+ setup(build2) {
101
+ build2.onLoad(
102
+ {
103
+ filter: new RegExp(
104
+ `\\.(${embeddable_file_extensions.join(
105
+ "|"
106
+ )})$`
107
+ )
108
+ },
109
+ async (args) => {
110
+ const mode = "url";
111
+ let contents = `export const url = "${await makeResourceURL(
112
+ args.path
113
+ )}";
114
+
115
+ import fs from "fs";
116
+ let content_promise = null;
117
+
118
+ export const content = function(){
119
+ if(content_promise) return content_promise;
120
+ content_promise = fs.promises.readFile("${args.path}", "utf-8");
121
+ return content_promise;
122
+ }
123
+ `;
124
+ return {
125
+ contents
126
+ };
127
+ }
128
+ );
129
+ }
130
+ }
131
+ ]
49
132
  });
50
133
  } catch (e) {
51
134
  console.error(e.message);
@@ -74,12 +157,12 @@ async function complex_build(watch) {
74
157
  const port = await get_notifier_port();
75
158
  const notifier = make_notifier(port);
76
159
  await write_notifier_config(watch, port);
77
- watcher.on("all", async (_, path) => {
160
+ watcher.on("all", async (_, path2) => {
78
161
  if (!css_build)
79
162
  return;
80
- if (path.includes(".#"))
163
+ if (path2.includes(".#"))
81
164
  return;
82
- if (path.endsWith(".css") && !path.endsWith("/includes.css")) {
165
+ if (path2.endsWith(".css") && !path2.endsWith("/includes.css")) {
83
166
  if (ongoing_css) {
84
167
  return;
85
168
  }
@@ -87,7 +170,7 @@ async function complex_build(watch) {
87
170
  await generate_css_includes();
88
171
  try {
89
172
  await css_build?.rebuild?.();
90
- console.log(`Built main.css [on ${path}]`);
173
+ console.log(`Built main.css [on ${path2}]`);
91
174
  } catch (e) {
92
175
  console.error(e.message);
93
176
  await sleep(200);
@@ -99,7 +182,7 @@ async function complex_build(watch) {
99
182
  notifier("css");
100
183
  ongoing_css = false;
101
184
  }
102
- if ((path.endsWith(".ts") || path.endsWith(".tsx")) && !path.endsWith("src/back/collections/collections.ts") && !path.endsWith("src/back/routes/routes.ts") && !path.endsWith("src/back/routes/urls.ts")) {
185
+ if ((path2.endsWith(".ts") || path2.endsWith(".tsx")) && !path2.endsWith("src/back/collections/collections.ts") && !path2.endsWith("src/back/routes/routes.ts") && !path2.endsWith("src/back/routes/urls.ts")) {
103
186
  if (ongoing_ts) {
104
187
  return;
105
188
  }
@@ -108,7 +191,7 @@ async function complex_build(watch) {
108
191
  await ongoing_ts_promise;
109
192
  notifier("ts");
110
193
  ongoing_ts_promise = null;
111
- console.log(`Finished TS build [on ${path}]`);
194
+ console.log(`Finished TS build [on ${path2}]`);
112
195
  ongoing_ts = false;
113
196
  }
114
197
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.12.10",
3
+ "version": "0.12.11",
4
4
  "description": "Module to automate adding routes and collections to a sealious application",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -24,6 +24,7 @@
24
24
  "devDependencies": {
25
25
  "@playwright/test": "^1.30.0",
26
26
  "@types/koa__router": "^12.0.4",
27
+ "@types/md5": "^2.3.5",
27
28
  "@types/node": "^20.8.4",
28
29
  "@types/prettier": "^2.7.0",
29
30
  "@types/prompts": "^2.0.14",
@@ -56,6 +57,7 @@
56
57
  "esbuild": "^0.20.0",
57
58
  "js-convert-case": "^4.2.0",
58
59
  "locreq": "^3.0.0",
60
+ "md5": "^2.3.0",
59
61
  "merge": "^2.1.1",
60
62
  "prettier": "^2.7.1",
61
63
  "prompts": "^2.4.2",
@@ -10,54 +10,57 @@ const generate_names = (function* generate_names() {
10
10
  }
11
11
  })();
12
12
 
13
- export const remove_named_imports = {
13
+ export function removeNamedImports(content) {
14
+ const named_and_default_regex =
15
+ /^import\s+((\S+),\s+)?({\s*[^}]+})\s+from\s+("[^"]+");\s*$/gm;
16
+ return content.replace(
17
+ named_and_default_regex,
18
+ (_, default_import_name, __, all_named, module_path) => {
19
+ /* eslint-disable */
20
+ const new_name = generate_names.next().value;
21
+ const result =
22
+ `import * as ${new_name} from ${module_path}${
23
+ default_import_name
24
+ ? `; const ${default_import_name.replace(
25
+ ",",
26
+ ""
27
+ )} = ${new_name}.default || ${new_name};`
28
+ : ""
29
+ };` +
30
+ all_named
31
+ .match(/[a-zA-Z_$][^,}]+/g)
32
+ .map((importname) => {
33
+ if (importname.startsWith("default as")) {
34
+ return `const ${importname.replace(
35
+ "default as ",
36
+ ""
37
+ )} = ${new_name}?.default || ${new_name};`;
38
+ }
39
+ if (importname.match(/([^{]*)\sas\s([^}]*)/)) {
40
+ // is "import {asdfas as somethong_else}
41
+ const [_, original, named] =
42
+ importname.match(/([^{]*)\sas\s([^}]*)/);
43
+ return `const ${named} = ${new_name}?.default?.${original} || ${new_name}?.${original}`;
44
+ }
45
+ return `const ${importname} = ${new_name}?.default?.${importname} || ${new_name}?.${importname};`;
46
+ })
47
+ .join("; ");
48
+ return result;
49
+ /* eslint-enable */
50
+ }
51
+ );
52
+ }
53
+
54
+ export const remove_named_imports_plugin = {
14
55
  name: "remove-named-imports", // named imports sometime cause some esm incompatibilities, like the "is a CommonJS module, which may not support all module.exports as named exports." error
15
56
  setup(build) {
16
- build.onLoad({ filter: /.*/ }, async ({ path }) => {
17
- const named_and_default_regex =
18
- /^import\s+((\S+),\s+)?({\s*[^}]+})\s+from\s+("[^"]+");\s*$/gm;
19
-
20
- const old_content = await fs.readFile(path, "utf-8");
21
- const contents = old_content.replace(
22
- named_and_default_regex,
23
- (_, default_import_name, __, all_named, module_path) => {
24
- /* eslint-disable */
25
- const new_name = generate_names.next().value;
26
- const result =
27
- `import * as ${new_name} from ${module_path}${
28
- default_import_name
29
- ? `; const ${default_import_name.replace(
30
- ",",
31
- ""
32
- )} = ${new_name}.default || ${new_name};`
33
- : ""
34
- };` +
35
- all_named
36
- .match(/[a-zA-Z_$][^,}]+/g)
37
- .map((importname) => {
38
- if (importname.startsWith("default as")) {
39
- return `const ${importname.replace(
40
- "default as ",
41
- ""
42
- )} = ${new_name}?.default || ${new_name};`;
43
- }
44
- if (importname.match(/([^{]*)\sas\s([^}]*)/)) {
45
- // is "import {asdfas as somethong_else}
46
- const [_, original, named] =
47
- importname.match(
48
- /([^{]*)\sas\s([^}]*)/
49
- );
50
- return `const ${named} = ${new_name}?.default.${original} || ${new_name}?.${original}`;
51
- }
52
- return `const ${importname} = ${new_name}?.default?.${importname} || ${new_name}?.${importname};`;
53
- })
54
- .join("; ");
55
- return result;
56
- /* eslint-enable */
57
- }
58
- );
57
+ build.onLoad({ filter: /\.[t]sx?$/ }, async ({ path }) => {
58
+ const content = await fs.readFile(path, "utf-8");
59
+ const contents = removeNamedImports(content);
59
60
  return {
60
- contents,
61
+ pluginData: {
62
+ contents,
63
+ },
61
64
  loader: extname(path) === ".tsx" ? "tsx" : "ts",
62
65
  };
63
66
  });
package/src/build.ts CHANGED
@@ -9,9 +9,12 @@ import { generateRoutes } from "./generate-routes.js";
9
9
  import { generateComponents } from "./generate-components.js";
10
10
  import { make_notifier } from "./notifier.js";
11
11
  import getPort from "get-port";
12
- import { remove_named_imports } from "../remove-named-imports.js";
12
+ import { removeNamedImports } from "../remove-named-imports.js";
13
+ import md5 from "md5";
13
14
 
14
15
  import _locreq from "locreq";
16
+ import path, { basename, extname } from "path";
17
+ import { copyFile, writeFile } from "fs/promises";
15
18
  const target_locreq = _locreq(process.cwd());
16
19
 
17
20
  async function get_notifier_port() {
@@ -29,15 +32,35 @@ async function write_notifier_config(watch: boolean, port?: number) {
29
32
  );
30
33
  }
31
34
 
35
+ function addSuffix(filename: string, suffix: string) {
36
+ return filename.replace(/(\.[a-zA-Z0-9]+)?$/, suffix + "$1");
37
+ }
38
+
39
+ async function makeResourceURL(file_path: string) {
40
+ const hash_base = file_path + (await fs.stat(file_path)).mtimeMs;
41
+ const hash = md5(hash_base);
42
+ const public_url =
43
+ "dist/" + addSuffix(basename(file_path), "-" + hash.slice(0, 8));
44
+ const public_dir = "public";
45
+ await copyFile(
46
+ file_path,
47
+ target_locreq.resolve(path.resolve(public_dir, public_url))
48
+ );
49
+ return "/" + public_url;
50
+ }
51
+
32
52
  async function build_ts() {
33
53
  await Promise.all([
34
54
  generateCollections(),
35
55
  generateRoutes(),
36
56
  generateComponents(),
37
57
  ]);
38
- const entryPoints = (await glob("./src/back/**/*.{ts,tsx}")).filter(
39
- (path) => !path.includes(".#")
40
- );
58
+ const embeddable_file_extensions = ["svg", "png", "jpg", "jpeg", "webp"];
59
+ const entryPoints = (
60
+ await glob(
61
+ `./src/back/**/*.{ts,tsx,${embeddable_file_extensions.join(",")}}`
62
+ )
63
+ ).filter((path) => !path.includes(".#"));
41
64
  try {
42
65
  await build({
43
66
  entryPoints,
@@ -49,7 +72,78 @@ async function build_ts() {
49
72
  platform: "node",
50
73
  target: "es2022",
51
74
  format: "esm",
52
- plugins: [remove_named_imports],
75
+ loader: Object.fromEntries(
76
+ embeddable_file_extensions.map((ext) => ["." + ext, "js"])
77
+ ),
78
+ plugins: [
79
+ {
80
+ name: "sealgen-rewrite-asset-imports",
81
+ setup(build) {
82
+ build.onLoad(
83
+ { filter: /\.tsx?$/ },
84
+ async (args: any) => {
85
+ let contents = await fs.readFile(
86
+ args.path,
87
+ "utf8"
88
+ );
89
+ for (const ext of embeddable_file_extensions) {
90
+ contents = contents.replaceAll(
91
+ new RegExp(
92
+ `^import (\w+|{[^}]+}) from "([^"]+.${ext})";`,
93
+ "gm"
94
+ ),
95
+ (line) =>
96
+ line.replace(`.${ext}"`, '.js"')
97
+ );
98
+ }
99
+
100
+ contents = removeNamedImports(contents);
101
+
102
+ return {
103
+ contents,
104
+ loader:
105
+ extname(args.path) === ".tsx"
106
+ ? "tsx"
107
+ : "ts",
108
+ };
109
+ }
110
+ );
111
+ },
112
+ },
113
+ {
114
+ name: "sealgen-load-assets",
115
+ setup(build) {
116
+ build.onLoad(
117
+ {
118
+ filter: new RegExp(
119
+ `\\.(${embeddable_file_extensions.join(
120
+ "|"
121
+ )})$`
122
+ ),
123
+ },
124
+ async (args: any) => {
125
+ const mode = "url";
126
+ let contents = `export const url = "${await makeResourceURL(
127
+ args.path
128
+ )}";
129
+
130
+ import fs from "fs";
131
+ let content_promise = null;
132
+
133
+ export const content = function(){
134
+ if(content_promise) return content_promise;
135
+ content_promise = fs.promises.readFile("${args.path}", "utf-8");
136
+ return content_promise;
137
+ }
138
+ `;
139
+ return {
140
+ contents,
141
+ };
142
+ }
143
+ );
144
+ },
145
+ },
146
+ ],
53
147
  });
54
148
  } catch (e) {
55
149
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access