Skip to content

Commit a7d1eda

Browse files
committed
🐛 index生成方法を高度化
1 parent 0682d15 commit a7d1eda

File tree

9 files changed

+45
-26
lines changed

9 files changed

+45
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Javaの機能をJavaScriptにそれっぽく作成したもの
1515
CDNのURL
1616

1717
```url
18-
https://cdn.jsdelivr.net/gh/hi2ma-bu4/JavaLibraryScript/index.js
18+
https://cdn.jsdelivr.net/gh/hi2ma-bu4/JavaLibraryScript/dist/JavaLibraryScript.min.js
1919
```
2020

2121
## 連携

dev/generateIndex.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ const path = require("node:path");
33

44
const CL = require("./libs/ColorLogger.js");
55

6+
const skipList = new Set([
7+
"index.js", // 自動生成対象自身
8+
"main.js",
9+
]);
10+
11+
function isPlainObjectExport(modulePath) {
12+
try {
13+
const mod = require(modulePath);
14+
return typeof mod === "object" && mod !== null && !Array.isArray(mod);
15+
} catch {
16+
return false;
17+
}
18+
}
19+
620
/**
721
* index.jsを生成する
822
* @param {string} dir
@@ -11,34 +25,40 @@ function generateIndex(dir, baseDir = dir) {
1125
const entries = fs.readdirSync(dir, { withFileTypes: true });
1226

1327
// jsファイルだけ、かつindex.jsは除外
14-
const jsFiles = entries.filter((e) => e.isFile() && e.name.endsWith(".js") && e.name !== "index.js");
28+
const jsFiles = entries.filter((e) => e.isFile() && e.name.endsWith(".js") && !skipList.has(e.name));
1529

1630
// サブディレクトリ
17-
const subDirs = entries.filter((e) => e.isDirectory());
31+
const subDirs = entries.filter((e) => e.isDirectory() && !skipList.has(e.name));
1832

1933
// 先にサブディレクトリも再帰処理(深い階層から順に)
2034
for (const subDir of subDirs) {
2135
generateIndex(path.join(dir, subDir.name), baseDir);
2236
}
2337

2438
// export文を作成
25-
let exportsObj = {};
39+
const exportLines = [];
2640

2741
// ファイルのエクスポートを設定
2842
jsFiles.forEach((file) => {
29-
const name = path.basename(file.name, ".js");
30-
exportsObj[name] = `require("./${file.name}")`;
43+
const filePath = path.join(dir, file.name);
44+
const requirePath = `./${file.name}`;
45+
const key = path.basename(file.name, ".js");
46+
47+
const fullRequirePath = path.resolve(filePath);
48+
49+
if (isPlainObjectExport(fullRequirePath)) {
50+
exportLines.push(` ...require("${requirePath}")`);
51+
} else {
52+
exportLines.push(` ${key}: require("${requirePath}")`);
53+
}
3154
});
3255

3356
// サブフォルダのindexもexport
3457
subDirs.forEach((subDir) => {
35-
const name = subDir.name;
36-
exportsObj[name] = `require("./${name}/index.js")`;
58+
exportLines.push(` ${subDir.name}: require("./${subDir.name}")`);
3759
});
3860

3961
// module.exportsの内容を文字列で作成
40-
const exportLines = Object.entries(exportsObj).map(([key, val]) => ` ${key}: ${val}`);
41-
4262
const content = `module.exports = {\n${exportLines.join(",\n")}\n};\n`;
4363

4464
// index.jsを書き込み

dist/JavaLibraryScript.js

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)