@next2d/vite-plugin-next2d-auto-loader 2.0.3 → 2.0.4

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.
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @return {object}
3
+ * @method
4
+ * @public
5
+ */
6
+ export default function autoLoader(): any;
package/dist/index.js CHANGED
@@ -1,134 +1,242 @@
1
- import * as c from "fs";
2
- let S = "";
3
- const w = (t) => {
4
- const e = `${process.cwd()}/src/config`, r = process.env.NEXT2D_EBUILD_ENVIRONMENT || "local", s = {
5
- platform: process.env.NEXT2D_TARGET_PLATFORM || "web",
6
- stage: {},
7
- routing: {}
8
- }, l = `${e}/config.json`;
9
- if (c.existsSync(l)) {
10
- const n = JSON.parse(
11
- c.readFileSync(l, { encoding: "utf8" })
12
- );
13
- r in n && Object.assign(s, n[r]), n.all && Object.assign(s, n.all);
14
- }
15
- const u = `${e}/stage.json`;
16
- c.existsSync(u) && Object.assign(
17
- s.stage,
18
- JSON.parse(c.readFileSync(u, { encoding: "utf8" }))
19
- );
20
- const f = `${e}/routing.json`;
21
- c.existsSync(f) && Object.assign(
22
- s.routing,
23
- JSON.parse(c.readFileSync(f, { encoding: "utf8" }))
24
- );
25
- const y = new RegExp(/{{(.*?)}}/, "g");
26
- let o = JSON.stringify(s, null, 4);
27
- const g = o.match(y);
28
- if (g)
29
- for (let n = 0; n < g.length; ++n) {
30
- const h = g[n], p = h.replace(/\{|\{|\}|\}/g, "").replace(/\s+/g, "").split(".");
31
- if (!p.length)
32
- continue;
33
- let $ = s;
34
- for (let d = 0; d < p.length; ++d) {
35
- const m = p[d];
36
- m in $ && ($ = $[m]);
37
- }
38
- s !== $ && (o = o.replace(h, $));
39
- }
40
- if (S !== o) {
41
- S = o;
42
- const n = `const config = ${o};
1
+ import * as fs from "fs";
2
+ /**
3
+ * @type {boolean}
4
+ * @private
5
+ */
6
+ const useTypeScript = fs.existsSync(`${process.cwd()}/src/index.ts`);
7
+ /**
8
+ * @type {string}
9
+ * @private
10
+ */
11
+ const ext = useTypeScript ? "ts" : "js";
12
+ /**
13
+ * @type {string}
14
+ * @private
15
+ */
16
+ let $cacheConfig = "";
17
+ /**
18
+ * @typem {string}
19
+ * @private
20
+ */
21
+ let $cachePackages = "";
22
+ /**
23
+ * @type {string}
24
+ * @private
25
+ */
26
+ const EOL = "\n";
27
+ /**
28
+ * @return {object}
29
+ * @method
30
+ * @public
31
+ */
32
+ export default function autoLoader() {
33
+ /**
34
+ * @description 指定されたパスのファイルタイプを返却します。
35
+ * Returns the file type of the specified path.
36
+ *
37
+ * @param {string} path
38
+ * @return {string}
39
+ * @method
40
+ */
41
+ const getFileType = (path) => {
42
+ try {
43
+ const stat = fs.statSync(path);
44
+ switch (true) {
45
+ case stat.isFile():
46
+ return "file";
47
+ case stat.isDirectory():
48
+ return "directory";
49
+ default:
50
+ return "unknown";
51
+ }
52
+ }
53
+ catch (_) {
54
+ return "unknown";
55
+ }
56
+ };
57
+ /**
58
+ * @description 指定されたディレクトリパス内のファイルパスリストを返却します。
59
+ * Returns a list of file paths in the specified directory path.
60
+ *
61
+ * @param {string} dir_path
62
+ * @return {string[]}
63
+ * @method
64
+ */
65
+ const getFilePathList = (dir_path) => {
66
+ const files = [];
67
+ const paths = fs.readdirSync(dir_path);
68
+ for (let idx = 0; idx < paths.length; ++idx) {
69
+ const path = `${dir_path}/${paths[idx]}`;
70
+ switch (getFileType(path)) {
71
+ case "file":
72
+ files.push(path);
73
+ break;
74
+ case "directory":
75
+ files.push(...getFilePathList(path));
76
+ break;
77
+ default:
78
+ break;
79
+ }
80
+ }
81
+ return files;
82
+ };
83
+ /**
84
+ * @description config ディレクトリのjsonファイルを読み込み、Config.[ts|js]を生成します。
85
+ * Reads JSON files from the config directory and generates Config.[ts|js].
86
+ *
87
+ * @return {void}
88
+ * @method
89
+ */
90
+ const buildConfig = () => {
91
+ const configDir = `${process.cwd()}/src/config`;
92
+ const environment = process.env.NEXT2D_EBUILD_ENVIRONMENT || "local";
93
+ const platform = process.env.NEXT2D_TARGET_PLATFORM || "web";
94
+ const config = {
95
+ "platform": platform,
96
+ "stage": {},
97
+ "routing": {}
98
+ };
99
+ // load config.json
100
+ const configPath = `${configDir}/config.json`;
101
+ if (fs.existsSync(configPath)) {
102
+ const configObject = JSON.parse(fs.readFileSync(configPath, { "encoding": "utf8" }));
103
+ if (environment in configObject) {
104
+ Object.assign(config, configObject[environment]);
105
+ }
106
+ if (configObject.all) {
107
+ Object.assign(config, configObject.all);
108
+ }
109
+ }
110
+ // load stage.json
111
+ const stagePath = `${configDir}/stage.json`;
112
+ if (fs.existsSync(stagePath)) {
113
+ Object.assign(config.stage, JSON.parse(fs.readFileSync(stagePath, { "encoding": "utf8" })));
114
+ }
115
+ // load routing.json
116
+ const routingPath = `${configDir}/routing.json`;
117
+ if (fs.existsSync(routingPath)) {
118
+ Object.assign(config.routing, JSON.parse(fs.readFileSync(routingPath, { "encoding": "utf8" })));
119
+ }
120
+ const regexp = new RegExp(/{{(.*?)}}/, "g");
121
+ let configString = JSON.stringify(config, null, 4);
122
+ const values = configString.match(regexp);
123
+ if (values) {
124
+ for (let idx = 0; idx < values.length; ++idx) {
125
+ const value = values[idx];
126
+ const names = value
127
+ .replace(/\{|\{|\}|\}/g, "")
128
+ .replace(/\s+/g, "")
129
+ .split(".");
130
+ if (!names.length) {
131
+ continue;
132
+ }
133
+ let configValue = config;
134
+ for (let idx = 0; idx < names.length; ++idx) {
135
+ const name = names[idx];
136
+ if (name in configValue) {
137
+ configValue = configValue[name];
138
+ }
139
+ }
140
+ if (config === configValue) {
141
+ continue;
142
+ }
143
+ configString = configString.replace(value, configValue);
144
+ }
145
+ }
146
+ if ($cacheConfig !== configString) {
147
+ // cache update
148
+ $cacheConfig = configString;
149
+ const source = `const config = ${configString};
43
150
  export { config };`;
44
- c.writeFileSync(`${e}/Config.${t}`, n);
45
- }
46
- }, k = (t) => {
47
- try {
48
- const e = c.statSync(t);
49
- switch (!0) {
50
- case e.isFile():
51
- return "file";
52
- case e.isDirectory():
53
- return "directory";
54
- default:
55
- return "unknown";
56
- }
57
- } catch {
58
- return "unknown";
59
- }
60
- }, O = (t) => {
61
- const e = [], r = c.readdirSync(t);
62
- for (let a = 0; a < r.length; ++a) {
63
- const s = `${t}/${r[a]}`;
64
- switch (k(s)) {
65
- case "file":
66
- e.push(s);
67
- break;
68
- case "directory":
69
- e.push(...O(s));
70
- break;
71
- }
72
- }
73
- return e;
74
- };
75
- let j = "";
76
- const i = `
77
- `, b = (t) => {
78
- const e = process.cwd(), r = O(`${e}/src`);
79
- let a = "", s = `[${i}`;
80
- for (let u = 0; u < r.length; ++u) {
81
- const f = r[u];
82
- if (f.indexOf(`.${t}`) === -1)
83
- continue;
84
- const o = c.readFileSync(f, { encoding: "utf-8" }).split(`
85
- `), g = f.replace(`${e}/`, "");
86
- for (let n = 0; n < o.length; ++n) {
87
- const h = o[n];
88
- if (h.indexOf("export class ") === -1)
89
- continue;
90
- const p = h.split(" ")[2];
91
- switch (!0) {
92
- case g.indexOf("src/view/") > -1:
93
- a += `import { ${p} } from "@/${g.split("src/")[1].split(`.${t}`)[0]}";${i}`, s += ` ["${p}", ${p}],${i}`;
94
- break;
95
- case g.indexOf("src/model/") > -1:
96
- {
97
- const $ = f.split("src/model/")[1].split("/").join(".").slice(0, -3), d = f.split("src/model/")[1].split("/").join("_").slice(0, -3);
98
- a += `import { ${p} as ${d} } from "@/${g.split("src/")[1].split(`.${t}`)[0]}";${i}`, s += ` ["${$}", ${d}],${i}`;
99
- }
100
- break;
101
- }
102
- break;
103
- }
104
- }
105
- s = s.slice(0, -2), s += `${i}]`;
106
- let l = `${a}${i}`;
107
- l += t === "ts" ? `const packages: Array<Array<string | Function>> = ${s};${i}` : `const packages = ${s};${i}`, l += "export { packages };", j !== l && (j = l, c.writeFileSync(`${e}/src/Packages.${t}`, l));
108
- };
109
- console.log(process.cwd());
110
- const N = c.existsSync(`${process.cwd()}/src/index.ts`), x = N ? "ts" : "js";
111
- function F() {
112
- return {
113
- name: "vite-plugin-next2d-auto-loader",
114
- buildStart: {
115
- order: "pre",
116
- handler() {
117
- w(x), b(x);
118
- }
119
- },
120
- configureServer(t) {
121
- const e = `${process.cwd()}/src/config`;
122
- t.watcher.add([
123
- `${e}/config.json`,
124
- `${e}/routing.json`,
125
- `${e}/stage.json`
126
- ]), t.watcher.on("change", () => {
127
- w(x);
128
- });
129
- }
130
- };
151
+ fs.writeFileSync(`${configDir}/Config.${ext}`, source);
152
+ }
153
+ };
154
+ /**
155
+ * @description view、 model ディレクトリ配下のファイルを読み込み、Package.[ts|js]を生成します。
156
+ * Reads files under the view and model directories and generates Package.[ts|js].
157
+ *
158
+ * @return {void}
159
+ * @method
160
+ */
161
+ const buildPackage = () => {
162
+ const dir = process.cwd();
163
+ const filePaths = getFilePathList(`${dir}/src`);
164
+ let imports = "";
165
+ let packages = `[${EOL}`;
166
+ for (let idx = 0; idx < filePaths.length; ++idx) {
167
+ const filePath = filePaths[idx];
168
+ // ts, js 以外はスキップ
169
+ if (filePath.indexOf(`.${ext}`) === -1) {
170
+ continue;
171
+ }
172
+ const js = fs.readFileSync(filePath, { "encoding": "utf-8" });
173
+ const lines = js.split("\n");
174
+ const path = filePath.replace(`${dir}/`, "");
175
+ for (let idx = 0; idx < lines.length; ++idx) {
176
+ const line = lines[idx];
177
+ // クラス定義以外はスキップ
178
+ if (line.indexOf("export class ") === -1) {
179
+ continue;
180
+ }
181
+ const name = line.split(" ")[2];
182
+ switch (true) {
183
+ case path.indexOf("src/view/") > -1:
184
+ imports += `import { ${name} } from "@/${path.split("src/")[1].split(`.${ext}`)[0]}";${EOL}`;
185
+ packages += ` ["${name}", ${name}],${EOL}`;
186
+ break;
187
+ case path.indexOf("src/model/") > -1:
188
+ {
189
+ const key = filePath
190
+ .split("src/model/")[1]
191
+ .split("/")
192
+ .join(".")
193
+ .slice(0, -3);
194
+ const asName = filePath
195
+ .split("src/model/")[1]
196
+ .split("/")
197
+ .join("_")
198
+ .slice(0, -3);
199
+ imports += `import { ${name} as ${asName} } from "@/${path.split("src/")[1].split(`.${ext}`)[0]}";${EOL}`;
200
+ packages += ` ["${key}", ${asName}],${EOL}`;
201
+ }
202
+ break;
203
+ default:
204
+ break;
205
+ }
206
+ break;
207
+ }
208
+ }
209
+ packages = packages.slice(0, -2);
210
+ packages += `${EOL}]`;
211
+ let source = `${imports}${EOL}`;
212
+ source += ext === "ts"
213
+ ? `const packages: Array<Array<string | Function>> = ${packages};${EOL}`
214
+ : `const packages = ${packages};${EOL}`;
215
+ source += "export { packages };";
216
+ if ($cachePackages !== source) {
217
+ $cachePackages = source;
218
+ fs.writeFileSync(`${dir}/src/Packages.${ext}`, source);
219
+ }
220
+ };
221
+ return {
222
+ "name": "vite-plugin-next2d-auto-loader",
223
+ "buildStart": {
224
+ "order": "pre",
225
+ handler() {
226
+ buildConfig();
227
+ buildPackage();
228
+ }
229
+ },
230
+ configureServer(server) {
231
+ const dir = `${process.cwd()}/src/config`;
232
+ server.watcher.add([
233
+ `${dir}/config.json`,
234
+ `${dir}/routing.json`,
235
+ `${dir}/stage.json`
236
+ ]);
237
+ server.watcher.on("change", () => {
238
+ buildConfig();
239
+ });
240
+ }
241
+ };
131
242
  }
132
- export {
133
- F as default
134
- };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@next2d/vite-plugin-next2d-auto-loader",
3
3
  "description": "Next2D Framework vite TypeScript Auto Loader plugin.",
4
- "version": "2.0.3",
4
+ "version": "2.0.4",
5
5
  "homepage": "https://next2d.app",
6
6
  "bugs": "https://github.com/Next2D/vite-plugin-next2d-auto-loader/issues",
7
7
  "author": "Toshiyuki Ienaga<ienaga@next2d.app>",
@@ -16,11 +16,11 @@
16
16
  },
17
17
  "keywords": [
18
18
  "Next2D",
19
- "vite plugin"
19
+ "vite-plugin"
20
20
  ],
21
21
  "scripts": {
22
22
  "lint": "eslint src/**/*.ts",
23
- "publish": "vite build"
23
+ "publish": "tsc"
24
24
  },
25
25
  "repository": {
26
26
  "type": "git",
@@ -34,8 +34,6 @@
34
34
  "@typescript-eslint/parser": "^8.23.0",
35
35
  "eslint": "^9.19.0",
36
36
  "eslint-plugin-unused-imports": "^4.1.4",
37
- "rollup-plugin-polyfill-node": "^0.13.0",
38
- "typescript": "^5.7.3",
39
- "vite": "^6.1.0"
37
+ "typescript": "^5.7.3"
40
38
  }
41
39
  }