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

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