From 6fdac6facf775e092d6deace5834fbbe475cc07b Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Thu, 10 Jul 2025 13:43:13 +0900 Subject: [PATCH] feat: enable the ESM for main (#3588) --- electron.vite.config.mts | 12 +++++------- package.json | 1 + src/index.ts | 5 +---- src/plugins/sponsorblock/tests/segments.test.js | 8 ++------ tests/index.test.js | 12 +++++------- 5 files changed, 14 insertions(+), 24 deletions(-) diff --git a/electron.vite.config.mts b/electron.vite.config.mts index 73caa775..21157f39 100644 --- a/electron.vite.config.mts +++ b/electron.vite.config.mts @@ -35,15 +35,16 @@ export default defineConfig({ }), ], publicDir: 'assets', + define: { + '__dirname': 'import.meta.dirname', + '__filename': 'import.meta.filename', + }, build: { lib: { entry: 'src/index.ts', - formats: ['cjs'], + formats: ['es'], }, outDir: 'dist/main', - commonjsOptions: { - ignoreDynamicRequires: true, - }, rollupOptions: { external: ['electron', 'custom-electron-prompt', ...builtinModules], input: './src/index.ts', @@ -148,9 +149,6 @@ export default defineConfig({ name: 'renderer', }, outDir: 'dist/renderer', - commonjsOptions: { - ignoreDynamicRequires: true, - }, rollupOptions: { external: ['electron', ...builtinModules], input: './src/index.html', diff --git a/package.json b/package.json index 04e616e6..5d71e94e 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "version": "3.9.0", "description": "YouTube Music Desktop App - including custom plugins", "main": "./dist/main/index.js", + "type": "module", "license": "MIT", "repository": "th-ch/youtube-music", "author": { diff --git a/src/index.ts b/src/index.ts index c297dbec..848d534e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -74,9 +74,6 @@ unhandled({ showDialog: false, }); -// Disable Node options if the env var is set -process.env.NODE_OPTIONS = ''; - // Prevent window being garbage collected let mainWindow: Electron.BrowserWindow | null; autoUpdater.autoDownload = false; @@ -361,7 +358,7 @@ async function createMainWindow() { show: false, webPreferences: { contextIsolation: true, - preload: path.join(__dirname, '..', 'preload', 'preload.js'), + preload: path.join(__dirname, '..', 'preload', 'preload.cjs'), ...(isTesting() ? undefined : { diff --git a/src/plugins/sponsorblock/tests/segments.test.js b/src/plugins/sponsorblock/tests/segments.test.js index 39bf0265..3b8f0d3e 100644 --- a/src/plugins/sponsorblock/tests/segments.test.js +++ b/src/plugins/sponsorblock/tests/segments.test.js @@ -1,10 +1,6 @@ -/* eslint-disable @typescript-eslint/no-require-imports */ +import { test, expect } from '@playwright/test'; -// eslint-disable-next-line no-undef -const { test, expect } = require('@playwright/test'); - -// eslint-disable-next-line no-undef -const { sortSegments } = require('../segments'); +import { sortSegments } from '../segments'; test('Segment sorting', () => { expect( diff --git a/tests/index.test.js b/tests/index.test.js index d5f4f92f..f0658476 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -1,13 +1,11 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ - -const path = require('node:path'); - -const { _electron: electron } = require('playwright'); -const { test, expect } = require('@playwright/test'); +import path from 'node:path'; +import process from 'node:process'; +import { _electron as electron } from 'playwright'; +import { test, expect } from '@playwright/test'; process.env.NODE_ENV = 'test'; -const appPath = path.resolve(__dirname, '..'); +const appPath = path.resolve(import.meta.dirname, '..'); test('YouTube Music App - With default settings, app is launched and visible', async () => { const app = await electron.launch({