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 build/afterAllArtifactBuild.cjs

+2-2
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

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 build/splitNsisArchive.cjs

+1-1
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 electron-builder.config.cjs

+18-23
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 eslint.config.js

+1-3
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

+6-5
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

+5-5
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

+1-1
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
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tsconfig.json

+7-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"skipLibCheck": true,
1111
"esModuleInterop": true,
1212
"allowSyntheticDefaultImports": true,
13+
"allowImportingTsExtensions": true,
14+
"noEmit": true,
1315
"isolatedModules": true,
1416
"resolveJsonModule": true,
1517
"sourceMap": true,
@@ -21,18 +23,11 @@
2123
"lib": ["esnext", "dom", "dom.iterable"]
2224
},
2325
"include": [
24-
"playwright.config.ts",
25-
"*.mts",
26-
"eslint-plugin/**/*.mts",
27-
"src/**/*.ts",
28-
"src/**/*.tsx",
29-
"src/**/*.vue",
30-
"tests/**/*.ts",
31-
"tests/**/*.mts",
32-
"tests/**/*.tsx",
33-
"tools/**/*.ts",
34-
"tools/**/*.mts",
35-
".storybook/**/*.ts"
26+
".storybook/**/*.ts",
27+
"**/*.js",
28+
"**/*.ts",
29+
"**/*.cjs",
30+
"**/*.vue"
3631
],
3732
"exclude": ["node_modules"]
3833
}

vite.config.mts vite.config.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import path from "path";
33
import { rm } from "fs/promises";
44

5-
import electron from "vite-plugin-electron";
5+
import electron from "vite-plugin-electron/simple";
66
import tsconfigPaths from "vite-tsconfig-paths";
77
import vue from "@vitejs/plugin-vue";
88
import checker from "vite-plugin-checker";
@@ -13,7 +13,7 @@ import { z } from "zod";
1313
import {
1414
checkSuspiciousImports,
1515
CheckSuspiciousImportsOptions,
16-
} from "./tools/checkSuspiciousImports.mjs";
16+
} from "./tools/checkSuspiciousImports.js";
1717

1818
const isElectron = process.env.VITE_TARGET === "electron";
1919
const isBrowser = process.env.VITE_TARGET === "browser";
@@ -90,9 +90,10 @@ export default defineConfig((options) => {
9090
isElectron && [
9191
cleanDistPlugin(),
9292
// TODO: 関数で切り出して共通化できる部分はまとめる
93-
electron([
94-
{
93+
electron({
94+
main: {
9595
entry: "./backend/electron/main.ts",
96+
9697
// ref: https://github.com/electron-vite/vite-plugin-electron/pull/122
9798
onstart: ({ startup }) => {
9899
console.log("main process build is complete.");
@@ -124,9 +125,9 @@ export default defineConfig((options) => {
124125
},
125126
},
126127
},
127-
{
128+
preload: {
128129
// ref: https://electron-vite.github.io/guide/preload-not-split.html
129-
entry: "./backend/electron/preload.ts",
130+
input: "./src/backend/electron/preload.ts",
130131
onstart({ reload }) {
131132
if (!skipLaunchElectron) {
132133
reload();
@@ -140,13 +141,10 @@ export default defineConfig((options) => {
140141
build: {
141142
outDir: path.resolve(import.meta.dirname, "dist"),
142143
sourcemap,
143-
rollupOptions: {
144-
output: { inlineDynamicImports: true },
145-
},
146144
},
147145
},
148146
},
149-
]),
147+
}),
150148
],
151149
isBrowser && injectBrowserPreloadPlugin(),
152150
],

vitest.workspace.mts vitest.workspace.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const ignorePaths = (paths: string[]) => paths.map((path) => `!${path}`);
1111
export default defineWorkspace([
1212
// Node.js環境
1313
{
14-
extends: "./vite.config.mts",
14+
extends: "./vite.config.ts",
1515
test: {
1616
include: nodeTestPaths,
1717
name: "node",
@@ -22,7 +22,7 @@ export default defineWorkspace([
2222

2323
// happy-domのエミュレート版ブラウザ環境
2424
{
25-
extends: "./vite.config.mts",
25+
extends: "./vite.config.ts",
2626
plugins: [],
2727
test: {
2828
include: [
@@ -38,7 +38,7 @@ export default defineWorkspace([
3838

3939
// Chromiumブラウザ環境
4040
{
41-
extends: "./vite.config.mts",
41+
extends: "./vite.config.ts",
4242
test: {
4343
include: browserTestPaths,
4444
globals: true,
@@ -56,7 +56,7 @@ export default defineWorkspace([
5656

5757
// Storybook
5858
{
59-
extends: "./vite.config.mts",
59+
extends: "./vite.config.ts",
6060
plugins: [
6161
storybookTest({
6262
storybookScript: "storybook --ci --port 7160",

0 commit comments

Comments
 (0)