@sealcode/sealgen 0.10.1 → 0.10.2
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/lib/build.js +22 -7
- package/package.json +1 -1
- package/src/build.ts +49 -18
package/lib/build.js
CHANGED
|
@@ -37,7 +37,7 @@ const sleep_1 = require("./utils/sleep");
|
|
|
37
37
|
const generate_collections_1 = require("./generate-collections");
|
|
38
38
|
const generate_routes_1 = require("./generate-routes");
|
|
39
39
|
const generate_names = (function* generate_names() {
|
|
40
|
-
let base_name = "
|
|
40
|
+
let base_name = "____unique_import_";
|
|
41
41
|
let count = 0;
|
|
42
42
|
while (true) {
|
|
43
43
|
yield base_name + count;
|
|
@@ -63,13 +63,28 @@ async function build_ts() {
|
|
|
63
63
|
build.onLoad({ filter: /.*/ }, async ({ path }) => {
|
|
64
64
|
const named_and_default_regex = /^import\s+((\S+),\s+)?({\s*[^}]+})\s+from\s+("[^"]+");\s*$/gm;
|
|
65
65
|
const old_content = await fs_1.promises.readFile(path, "utf-8");
|
|
66
|
-
const contents = old_content.replace(named_and_default_regex, (
|
|
66
|
+
const contents = old_content.replace(named_and_default_regex, (_, default_import_name, __, all_named, module_path) => {
|
|
67
67
|
const new_name = generate_names.next().value;
|
|
68
|
-
|
|
69
|
-
?
|
|
70
|
-
: ""}
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
const result = `import * as ${new_name} from ${module_path}${default_import_name
|
|
69
|
+
? `; const ${default_import_name.replace(",", "")} = ${new_name}.default || ${new_name};`
|
|
70
|
+
: ""};` +
|
|
71
|
+
all_named
|
|
72
|
+
.match(/[a-zA-Z_$][^,}]+/g)
|
|
73
|
+
.map((importname) => {
|
|
74
|
+
console.log(importname, `importname.startsWith("default as")`, importname.startsWith("default as"));
|
|
75
|
+
if (importname.startsWith("default as")) {
|
|
76
|
+
return `const ${importname.replace("default as ", "")} = ${new_name}?.default || ${new_name};`;
|
|
77
|
+
}
|
|
78
|
+
if (importname.match(/([^{]*)\sas\s([^}]*)/)) {
|
|
79
|
+
// is "import {asdfas as somethong_else}
|
|
80
|
+
const [_, original, named] = importname.match(/([^{]*)\sas\s([^}]*)/);
|
|
81
|
+
return `const named = ${new_name}?.default.${original} || ${new_name}?.${original}`;
|
|
82
|
+
}
|
|
83
|
+
return `const ${importname} = ${new_name}?.default?.${importname} || ${new_name}?.${importname};`;
|
|
84
|
+
})
|
|
85
|
+
.join(" ");
|
|
86
|
+
console.log({ path, result });
|
|
87
|
+
return result;
|
|
73
88
|
});
|
|
74
89
|
return { contents, loader: "ts" };
|
|
75
90
|
});
|
package/package.json
CHANGED
package/src/build.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { generateCollections } from "./generate-collections";
|
|
|
9
9
|
import { generateRoutes } from "./generate-routes";
|
|
10
10
|
|
|
11
11
|
const generate_names = (function* generate_names() {
|
|
12
|
-
let base_name = "
|
|
12
|
+
let base_name = "____unique_import_";
|
|
13
13
|
let count = 0;
|
|
14
14
|
while (true) {
|
|
15
15
|
yield base_name + count;
|
|
@@ -46,29 +46,60 @@ async function build_ts() {
|
|
|
46
46
|
const contents = old_content.replace(
|
|
47
47
|
named_and_default_regex,
|
|
48
48
|
(
|
|
49
|
-
entire_string,
|
|
50
|
-
default_import_name,
|
|
51
49
|
_,
|
|
50
|
+
default_import_name,
|
|
51
|
+
__,
|
|
52
52
|
all_named,
|
|
53
53
|
module_path
|
|
54
54
|
) => {
|
|
55
55
|
const new_name =
|
|
56
56
|
generate_names.next().value;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
57
|
+
const result =
|
|
58
|
+
`import * as ${new_name} from ${module_path}${
|
|
59
|
+
default_import_name
|
|
60
|
+
? `; const ${default_import_name.replace(
|
|
61
|
+
",",
|
|
62
|
+
""
|
|
63
|
+
)} = ${new_name}.default || ${new_name};`
|
|
64
|
+
: ""
|
|
65
|
+
};` +
|
|
66
|
+
all_named
|
|
67
|
+
.match(/[a-zA-Z_$][^,}]+/g)
|
|
68
|
+
.map((importname: string) => {
|
|
69
|
+
console.log(
|
|
70
|
+
importname,
|
|
71
|
+
`importname.startsWith("default as")`,
|
|
72
|
+
importname.startsWith(
|
|
73
|
+
"default as"
|
|
74
|
+
)
|
|
75
|
+
);
|
|
76
|
+
if (
|
|
77
|
+
importname.startsWith(
|
|
78
|
+
"default as"
|
|
79
|
+
)
|
|
80
|
+
) {
|
|
81
|
+
return `const ${importname.replace(
|
|
82
|
+
"default as ",
|
|
83
|
+
""
|
|
84
|
+
)} = ${new_name}?.default || ${new_name};`;
|
|
85
|
+
}
|
|
86
|
+
if (
|
|
87
|
+
importname.match(
|
|
88
|
+
/([^{]*)\sas\s([^}]*)/
|
|
89
|
+
)
|
|
90
|
+
) {
|
|
91
|
+
// is "import {asdfas as somethong_else}
|
|
92
|
+
const [_, original, named] =
|
|
93
|
+
importname.match(
|
|
94
|
+
/([^{]*)\sas\s([^}]*)/
|
|
95
|
+
) as string[];
|
|
96
|
+
return `const named = ${new_name}?.default.${original} || ${new_name}?.${original}`;
|
|
97
|
+
}
|
|
98
|
+
return `const ${importname} = ${new_name}?.default?.${importname} || ${new_name}?.${importname};`;
|
|
99
|
+
})
|
|
100
|
+
.join(" ");
|
|
101
|
+
console.log({ path, result });
|
|
102
|
+
return result;
|
|
72
103
|
}
|
|
73
104
|
);
|
|
74
105
|
return { contents, loader: "ts" };
|