Skip to content

Commit b1056b1

Browse files
chore: ESModuleにする (#2580)
1 parent bc06f9f commit b1056b1

File tree

115 files changed

+55
-68
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+55
-68
lines changed

build/afterAllArtifactBuild.js renamed to build/afterAllArtifactBuild.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable @typescript-eslint/no-require-imports */
22
const afterWindowsNsisWebArtifactBuild =
3-
require("./afterNsisWebArtifactBuild").default;
3+
require("./afterNsisWebArtifactBuild.cjs").default;
44

55
// buildResult: electron-builder.BuildResult
6-
exports.default = async function (buildResult) {
6+
module.exports.default = async function (buildResult) {
77
for (const [platform, targets] of buildResult.platformToTargets.entries()) {
88
const platformName = platform.name;
99

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-require-imports */
2-
const splitNsisArchive = require("./splitNsisArchive").default;
2+
const splitNsisArchive = require("./splitNsisArchive.cjs").default;
33

44
// target: electron-builder.Target
5-
exports.default = async function (target) {
5+
module.exports.default = async function (target) {
66
await splitNsisArchive(target);
77
};

build/splitNsisArchive.js renamed to build/splitNsisArchive.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const createIni = (sizes, hashes) => {
1111
};
1212

1313
// target: electron-builder.Target
14-
exports.default = async function (target) {
14+
module.exports.default = async function (target) {
1515
const projectName = process.env.npm_package_name;
1616
if (projectName == undefined) {
1717
const ErrorMessage = "Project name is undefined.";

electron-builder.config.js renamed to electron-builder.config.cjs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* eslint-disable @typescript-eslint/no-require-imports */
2-
// @ts-check
3-
const path = require("path");
4-
const fs = require("fs");
5-
const dotenv = require("dotenv");
2+
const { join, resolve } = require("path");
3+
const { readdirSync, existsSync, rmSync } = require("fs");
4+
const { config } = require("dotenv");
5+
const {
6+
default: afterAllArtifactBuild,
7+
} = require("./build/afterAllArtifactBuild.cjs");
68

7-
const dotenvPath = path.join(process.cwd(), ".env.production");
8-
dotenv.config({ path: dotenvPath });
9+
const dotenvPath = join(process.cwd(), ".env.production");
10+
config({ path: dotenvPath });
911

1012
const VOICEVOX_ENGINE_DIR =
1113
process.env.VOICEVOX_ENGINE_DIR ?? "../voicevox_engine/dist/run/";
@@ -38,24 +40,22 @@ const isArm64 = process.arch === "arm64";
3840
// cf: https://k-hyoda.hatenablog.com/entry/2021/10/23/000349#%E8%BF%BD%E5%8A%A0%E5%B1%95%E9%96%8B%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%85%88%E3%81%AE%E8%A8%AD%E5%AE%9A
3941
const extraFilePrefix = isMac ? "MacOS/" : "";
4042

41-
const sevenZipFile = fs
42-
.readdirSync(path.resolve(__dirname, "vendored", "7z"))
43-
.find(
44-
// Windows: 7za.exe, Linux: 7zzs, macOS: 7zz
45-
(fileName) => ["7za.exe", "7zzs", "7zz"].includes(fileName),
46-
);
43+
const sevenZipFile = readdirSync(resolve(__dirname, "vendored", "7z")).find(
44+
// Windows: 7za.exe, Linux: 7zzs, macOS: 7zz
45+
(fileName) => ["7za.exe", "7zzs", "7zz"].includes(fileName),
46+
);
4747

4848
if (!sevenZipFile) {
4949
throw new Error(
50-
"7z binary file not found. Run `node ./tools/download7z.js` first.",
50+
"7z binary file not found. Run `node ./tools/download7z.ts` first.",
5151
);
5252
}
5353

5454
/** @type {import("electron-builder").Configuration} */
5555
const builderOptions = {
5656
beforeBuild: async () => {
57-
if (fs.existsSync(path.resolve(__dirname, "dist_electron"))) {
58-
fs.rmSync(path.resolve(__dirname, "dist_electron"), { recursive: true });
57+
if (existsSync(resolve(__dirname, "dist_electron"))) {
58+
rmSync(resolve(__dirname, "dist_electron"), { recursive: true });
5959
}
6060
},
6161
directories: {
@@ -93,22 +93,18 @@ const builderOptions = {
9393
},
9494
{
9595
from: VOICEVOX_ENGINE_DIR,
96-
to: path.join(extraFilePrefix, "vv-engine"),
96+
to: join(extraFilePrefix, "vv-engine"),
9797
},
9898
{
99-
from: path.resolve(__dirname, "vendored", "7z", sevenZipFile),
99+
from: resolve(__dirname, "vendored", "7z", sevenZipFile),
100100
to: extraFilePrefix + sevenZipFile,
101101
},
102102
],
103103
// electron-builder installer
104104
productName: "VOICEVOX",
105105
appId: "jp.hiroshiba.voicevox",
106106
copyright: "Hiroshiba Kazuyuki",
107-
afterAllArtifactBuild: path.resolve(
108-
__dirname,
109-
"build",
110-
"afterAllArtifactBuild.js",
111-
),
107+
afterAllArtifactBuild,
112108
win: {
113109
icon: "public/icon.png",
114110
target: [
@@ -165,5 +161,4 @@ const builderOptions = {
165161
icon: "public/icon-dmg.icns",
166162
},
167163
};
168-
169164
module.exports = builderOptions;

eslint.config.mjs renamed to eslint.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import progress from "eslint-plugin-file-progress";
1515
import gitignore from "eslint-config-flat-gitignore";
1616
import voicevoxPlugin from "./eslint-plugin/index.mjs";
1717

18-
const __dirname = import.meta.dirname;
19-
2018
/**
2119
* @typedef {import("@typescript-eslint/utils/ts-eslint").FlatConfig.Config} Config
2220
* @typedef {import("@typescript-eslint/utils/ts-eslint").FlatConfig.ConfigArray} ConfigArray
@@ -64,7 +62,7 @@ const vueParserOptions = {
6462
/** @type {ParserOptions} */
6563
const typeCheckedParserOptions = {
6664
project: ["./tsconfig.json"],
67-
tsconfigRootDir: __dirname,
65+
tsconfigRootDir: import.meta.dirname,
6866
};
6967

7068
/** @type {Rules} */

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"author": "Hiroshiba Kazuyuki",
55
"private": true,
66
"main": "./dist/main.js",
7+
"type": "module",
78
"engines": {
89
"node": ">=22.14.0 <23"
910
},
@@ -20,9 +21,9 @@
2021
"typecheck": "vue-tsc --noEmit",
2122
"typos": "cross-env ./vendored/typos/typos",
2223
"// --- tools ---": "",
23-
"license:generate": "tsx tools/generateLicenses.mts",
24-
"license:merge": "tsx tools/mergeLicenses.mts",
25-
"check-suspicious-imports": "tsx tools/checkSuspiciousImports.mts",
24+
"license:generate": "tsx tools/generateLicenses.ts",
25+
"license:merge": "tsx tools/mergeLicenses.ts",
26+
"check-suspicious-imports": "tsx tools/checkSuspiciousImports.ts",
2627
"// --- test ---": "",
2728
"test:unit": "vitest --run",
2829
"test-watch:unit": "vitest --watch",
@@ -37,7 +38,7 @@
3738
"test-ui:storybook-vrt": "cross-env TARGET=storybook playwright test --ui",
3839
"// --- build ---": "",
3940
"electron:serve": "cross-env VITE_TARGET=electron vite",
40-
"electron:build": "cross-env VITE_TARGET=electron NODE_ENV=production vite build && electron-builder --config electron-builder.config.js --publish never",
41+
"electron:build": "cross-env VITE_TARGET=electron NODE_ENV=production vite build && electron-builder --config electron-builder.config.cjs --publish never",
4142
"browser:serve": "cross-env VITE_TARGET=browser vite",
4243
"browser:build": "cross-env VITE_TARGET=browser NODE_ENV=production vite build",
4344
"storybook": "storybook dev --port 6006",
@@ -46,7 +47,7 @@
4647
"preinstall": "npx -y only-allow pnpm",
4748
"postinstall": "pnpm run postinstall:packages && pnpm run postinstall:download-scripts",
4849
"postinstall:packages": "electron-builder install-app-deps && playwright install chromium",
49-
"postinstall:download-scripts": "tsx tools/download7z.mts && tsx tools/downloadTypos.mts",
50+
"postinstall:download-scripts": "tsx tools/download7z.ts && tsx tools/downloadTypos.ts",
5051
"postuninstall": "electron-builder install-app-deps"
5152
},
5253
"dependencies": {

src/backend/electron/main.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ function initializeAppPaths() {
118118
let __static: string;
119119

120120
if (isDevelopment) {
121-
// __dirnameはdist_electronを指しているので、一つ上のディレクトリに移動する
122-
appDirPath = path.resolve(__dirname, "..");
121+
// import.meta.dirnameはdist_electronを指しているので、一つ上のディレクトリに移動する
122+
appDirPath = path.resolve(import.meta.dirname, "..");
123123
__static = path.join(appDirPath, "public");
124124
} else {
125125
appDirPath = path.dirname(app.getPath("exe"));
126126
process.chdir(appDirPath);
127-
__static = __dirname;
127+
__static = import.meta.dirname;
128128
}
129129

130130
return { appDirPath, __static };
@@ -141,8 +141,8 @@ void app.whenReady().then(() => {
141141
// 読み取り先のファイルがインストールディレクトリ内であることを確認する
142142
// ref: https://www.electronjs.org/ja/docs/latest/api/protocol#protocolhandlescheme-handler
143143
const { pathname } = new URL(request.url);
144-
const pathToServe = path.resolve(path.join(__dirname, pathname));
145-
const relativePath = path.relative(__dirname, pathToServe);
144+
const pathToServe = path.resolve(path.join(import.meta.dirname, pathname));
145+
const relativePath = path.relative(import.meta.dirname, pathToServe);
146146
const isUnsafe =
147147
path.isAbsolute(relativePath) ||
148148
relativePath.startsWith("..") ||

src/backend/electron/manager/windowManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class WindowManager {
8181
show: false,
8282
backgroundColor,
8383
webPreferences: {
84-
preload: path.join(__dirname, "preload.js"),
84+
preload: path.join(import.meta.dirname, "preload.mjs"),
8585
},
8686
icon: path.join(this.staticDir, "icon.png"),
8787
});

0 commit comments

Comments
 (0)