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 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", 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 ??= [] +}