@sealcode/sealgen 0.12.10 → 0.12.12

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,90 @@ 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
+ const regex = new RegExp(
82
+ `^import (\\w+|{[^}]+}) from "([^"]+.${ext})";`,
83
+ "gm"
84
+ );
85
+ contents = contents.replaceAll(
86
+ regex,
87
+ (line) => {
88
+ return line.replace(
89
+ `.${ext}"`,
90
+ '.js"'
91
+ );
92
+ }
93
+ );
94
+ }
95
+ contents = removeNamedImports(contents);
96
+ return {
97
+ contents,
98
+ loader: extname(args.path) === ".tsx" ? "tsx" : "ts"
99
+ };
100
+ }
101
+ );
102
+ }
103
+ },
104
+ {
105
+ name: "sealgen-load-assets",
106
+ setup(build2) {
107
+ build2.onLoad(
108
+ {
109
+ filter: new RegExp(
110
+ `\\.(${embeddable_file_extensions.join(
111
+ "|"
112
+ )})$`
113
+ )
114
+ },
115
+ async (args) => {
116
+ const mode = "url";
117
+ let contents = `export const url = "${await makeResourceURL(
118
+ args.path
119
+ )}";
120
+
121
+ import fs from "fs";
122
+ let buffer_promise = null;
123
+
124
+ export const getBuffer = function(){
125
+ if(buffer_promise) return buffer_promise;
126
+ buffer_promise = fs.promises.readFile("${args.path}");
127
+ return buffer_promise;
128
+ }
129
+
130
+ export const getContent = async function(){
131
+ const buffer = await getBuffer();
132
+ return buffer.toString("utf-8");
133
+ }
134
+
135
+ export const getBase64 = async function(){
136
+ const buffer = await getBuffer();
137
+ return buffer.toString("base64");
138
+ }
139
+
140
+ export default { getContent, url, getBase64, getBuffer };
141
+ `;
142
+ return {
143
+ contents
144
+ };
145
+ }
146
+ );
147
+ }
148
+ }
149
+ ]
49
150
  });
50
151
  } catch (e) {
51
152
  console.error(e.message);
@@ -74,12 +175,12 @@ async function complex_build(watch) {
74
175
  const port = await get_notifier_port();
75
176
  const notifier = make_notifier(port);
76
177
  await write_notifier_config(watch, port);
77
- watcher.on("all", async (_, path) => {
178
+ watcher.on("all", async (_, path2) => {
78
179
  if (!css_build)
79
180
  return;
80
- if (path.includes(".#"))
181
+ if (path2.includes(".#"))
81
182
  return;
82
- if (path.endsWith(".css") && !path.endsWith("/includes.css")) {
183
+ if (path2.endsWith(".css") && !path2.endsWith("/includes.css")) {
83
184
  if (ongoing_css) {
84
185
  return;
85
186
  }
@@ -87,7 +188,7 @@ async function complex_build(watch) {
87
188
  await generate_css_includes();
88
189
  try {
89
190
  await css_build?.rebuild?.();
90
- console.log(`Built main.css [on ${path}]`);
191
+ console.log(`Built main.css [on ${path2}]`);
91
192
  } catch (e) {
92
193
  console.error(e.message);
93
194
  await sleep(200);
@@ -99,7 +200,7 @@ async function complex_build(watch) {
99
200
  notifier("css");
100
201
  ongoing_css = false;
101
202
  }
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")) {
203
+ 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
204
  if (ongoing_ts) {
104
205
  return;
105
206
  }
@@ -108,7 +209,7 @@ async function complex_build(watch) {
108
209
  await ongoing_ts_promise;
109
210
  notifier("ts");
110
211
  ongoing_ts_promise = null;
111
- console.log(`Finished TS build [on ${path}]`);
212
+ console.log(`Finished TS build [on ${path2}]`);
112
213
  ongoing_ts = false;
113
214
  }
114
215
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sealcode/sealgen",
3
- "version": "0.12.10",
3
+ "version": "0.12.12",
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,95 @@ 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
+ const regex = new RegExp(
91
+ `^import (\\w+|{[^}]+}) from "([^"]+.${ext})";`,
92
+ "gm"
93
+ );
94
+ contents = contents.replaceAll(
95
+ regex,
96
+ (line) => {
97
+ return line.replace(
98
+ `.${ext}"`,
99
+ '.js"'
100
+ );
101
+ }
102
+ );
103
+ }
104
+
105
+ contents = removeNamedImports(contents);
106
+
107
+ return {
108
+ contents,
109
+ loader:
110
+ extname(args.path) === ".tsx"
111
+ ? "tsx"
112
+ : "ts",
113
+ };
114
+ }
115
+ );
116
+ },
117
+ },
118
+ {
119
+ name: "sealgen-load-assets",
120
+ setup(build) {
121
+ build.onLoad(
122
+ {
123
+ filter: new RegExp(
124
+ `\\.(${embeddable_file_extensions.join(
125
+ "|"
126
+ )})$`
127
+ ),
128
+ },
129
+ async (args: any) => {
130
+ const mode = "url";
131
+ let contents = `export const url = "${await makeResourceURL(
132
+ args.path
133
+ )}";
134
+
135
+ import fs from "fs";
136
+ let buffer_promise = null;
137
+
138
+ export const getBuffer = function(){
139
+ if(buffer_promise) return buffer_promise;
140
+ buffer_promise = fs.promises.readFile("${args.path}");
141
+ return buffer_promise;
142
+ }
143
+
144
+ export const getContent = async function(){
145
+ const buffer = await getBuffer();
146
+ return buffer.toString("utf-8");
147
+ }
148
+
149
+ export const getBase64 = async function(){
150
+ const buffer = await getBuffer();
151
+ return buffer.toString("base64");
152
+ }
153
+
154
+ export default { getContent, url, getBase64, getBuffer };
155
+ `;
156
+ return {
157
+ contents,
158
+ };
159
+ }
160
+ );
161
+ },
162
+ },
163
+ ],
53
164
  });
54
165
  } catch (e) {
55
166
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access