From 12d8c5829f134c1996f71d8ffa08ea7043e5d1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Wed, 29 Mar 2023 10:48:37 +0800 Subject: [PATCH 1/3] refactor: cleanup --- src/index.ts | 81 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3ed4eff..dd20635 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,3 @@ -import path from 'path' -import { fileURLToPath } from 'url' import { type AddressInfo } from 'net' import { defineNuxtModule } from '@nuxt/kit' import { @@ -9,12 +7,10 @@ import { startup, } from 'vite-electron-plugin' -const cjs__dirname = typeof __dirname === 'undefined' - ? path.dirname(fileURLToPath(import.meta.url)) - : __dirname +const isProduction = process.env.NODE_ENV === 'production' // Fix tsc build error -import { NuxtModule } from '@nuxt/schema' +import { NuxtModule, Nuxt } from '@nuxt/schema' export interface ElectronOptions extends Partial { /** @@ -36,36 +32,19 @@ export default defineNuxtModule({ outDir: 'dist-electron', }, async setup(options, nuxt) { - const isProduction = process.env.NODE_ENV === 'production' + adaptElectronConfig(options, nuxt) - // Force to SPA mode always since we don't need SSR for a desktop app. - nuxt.options.ssr = false - - if (isProduction) { - // Fix path to make it works with Electron protocol `file://` - nuxt.options.app.baseURL ??= './' - if (nuxt.options.app.baseURL.startsWith('/')) { - nuxt.options.app.baseURL = '.' + nuxt.options.app.baseURL - } - nuxt.options.runtimeConfig.app.baseURL ??= './' - if (nuxt.options.runtimeConfig.app.baseURL.startsWith('/')) { - nuxt.options.runtimeConfig.app.baseURL = '.' + nuxt.options.runtimeConfig.app.baseURL - } - nuxt.options.router.options.hashMode ??= true // Avoid 404 errors - } - - // Use Node.js in Renderer process if (options.renderer) { - // For Vite - nuxt.options.vite.plugins ??= [] - nuxt.options.vite.plugins.push((await import('vite-plugin-electron-renderer')).default(options.renderer)) - - // TODO: For Webpack - - nuxt.options.nitro.plugins ??= [] - nuxt.options.nitro.plugins.push(path.join(cjs__dirname, 'cjs-shim')) // #14 + nodeIntegration(options, nuxt) } + nuxt.hook('build:manifest', (manifest) => { + for (const key in manifest) { + // or other logic + manifest[key].dynamicImports = [] + } + }) + nuxt.hooks.addHooks({ // For development listen(server, listener) { @@ -94,3 +73,41 @@ export default defineNuxtModule({ }) } }) + +/** Opinionated config for Electrono */ +function adaptElectronConfig(options: ElectronOptions, nuxt: Nuxt) { + // Force to SPA mode always since we don't need SSR for a desktop app. + nuxt.options.ssr = false + nuxt.options.app.buildAssetsDir = '/' // #16 + nuxt.options.app.baseURL = './' + nuxt.options.runtimeConfig.app.baseURL = './' + + if (isProduction) { + // TODO: calculate correct `require(id)` + + // Fix path to make it works with Electron protocol `file://` + /* + nuxt.options.app.baseURL ??= './' + if (nuxt.options.app.baseURL.startsWith('/')) { + nuxt.options.app.baseURL = '.' + nuxt.options.app.baseURL + } + nuxt.options.runtimeConfig.app.baseURL ??= './' + if (nuxt.options.runtimeConfig.app.baseURL.startsWith('/')) { + nuxt.options.runtimeConfig.app.baseURL = '.' + nuxt.options.runtimeConfig.app.baseURL + } + */ + + nuxt.options.router.options.hashMode ??= true // Avoid 404 errors + } +} + +/** Use Node.js in Renderer process */ +async function nodeIntegration(options: ElectronOptions, nuxt: Nuxt) { + // For Vite + nuxt.options.vite.plugins ??= [] + nuxt.options.vite.plugins.push((await import('vite-plugin-electron-renderer')).default(options.renderer)) + + // TODO: For Webpack + + nuxt.options.nitro.plugins ??= [] +} From 2ddff85244ab32df4f04aa9418e6ee18c451f215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Wed, 29 Mar 2023 10:50:12 +0800 Subject: [PATCH 2/3] log: v0.4.3 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9debbd..6970bd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.4.3 (2023-03-29) + +- 12d8c58 refactor: cleanup + ## 0.4.2 (2023-03-12) #### Main Changed From 7c2d18a296112890dc5e1f759d17d66a1d6a3df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Wed, 29 Mar 2023 10:50:16 +0800 Subject: [PATCH 3/3] v0.4.3 --- package.json | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 19faec7..0a6d3a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuxt-electron", - "version": "0.4.2", + "version": "0.4.3", "description": "Nuxt Integration with Electron", "main": "./dist/index.cjs", "types": "./dist/index.d.ts", @@ -10,11 +10,7 @@ "import": "./dist/index.mjs", "require": "./dist/index.cjs" }, - "./*": { - "types": "./dist/*.d.ts", - "import": "./dist/*.mjs", - "require": "./dist/*.cjs" - } + "./*": "./*" }, "repository": { "type": "git",