Compare commits

..

No commits in common. "master" and "v1.0.22" have entirely different histories.

37 changed files with 2478 additions and 3212 deletions

2
.eslintignore Normal file
View File

@ -0,0 +1,2 @@
node_modules
dist

38
.eslintrc.js Normal file
View File

@ -0,0 +1,38 @@
module.exports = {
root: true,
env: {
commonjs: true,
es6: true,
node: true
},
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2021
},
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:prettier/recommended'
],
rules: {
'no-empty': ['warn', { allowEmptyCatch: true }],
'@typescript-eslint/ban-ts-comment': ['error', { 'ts-ignore': 'allow-with-description' }],
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-empty-function': ['error', { allow: ['arrowFunctions'] }],
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-var-requires': 'off'
},
overrides: [
{
files: ['*.js'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off'
}
}
]
}

1
.github/FUNDING.yml vendored
View File

@ -1 +0,0 @@
github: alex8088

View File

@ -1,5 +1,6 @@
name: "\U0001F41E Bug Report" name: "\U0001F41E Bug Report"
description: Report an issue with electron-vite description: Report an issue with electron-vite
labels: ['bug', 'triage']
body: body:
- type: markdown - type: markdown
attributes: attributes:
@ -44,7 +45,7 @@ body:
required: true required: true
- label: Read the [Contributing Guidelines](https://github.com/alex8088/electron-vite/blob/master/CONTRIBUTING.md). - label: Read the [Contributing Guidelines](https://github.com/alex8088/electron-vite/blob/master/CONTRIBUTING.md).
required: true required: true
- label: Read the [docs](https://electron-vite.org). - label: Read the [docs](https://evite.netlify.app).
required: true required: true
- label: Check that there isn't [already an issue](https://github.com/alex8088/electron-vite/issues) that reports the same bug to avoid creating a duplicate. - label: Check that there isn't [already an issue](https://github.com/alex8088/electron-vite/issues) that reports the same bug to avoid creating a duplicate.
required: true required: true

View File

@ -1,5 +1 @@
blank_issues_enabled: false blank_issues_enabled: false
contact_links:
- name: Questions & Discussions
url: https://github.com/alex8088/electron-vite/discussions
about: Use GitHub discussions for message-board style questions and discussions.

View File

@ -40,7 +40,7 @@ body:
required: true required: true
- label: Read the [Contributing Guidelines](https://github.com/alex8088/electron-vite/blob/master/CONTRIBUTING.md). - label: Read the [Contributing Guidelines](https://github.com/alex8088/electron-vite/blob/master/CONTRIBUTING.md).
required: true required: true
- label: Read the [docs](https://electron-vite.org). - label: Read the [docs](https://evite.netlify.app).
required: true required: true
- label: Check that there isn't [already an issue](https://github.com/alex8088/electron-vite/issues) that requests the same feature to avoid creating a duplicate. - label: Check that there isn't [already an issue](https://github.com/alex8088/electron-vite/issues) that reports the same bug to avoid creating a duplicate.
required: true required: true

3
.gitignore vendored
View File

@ -1,5 +1,4 @@
node_modules node_modules
dist dist
.DS_Store
.eslintcache
*.log* *.log*

11
.vscode/settings.json vendored
View File

@ -1,11 +0,0 @@
{
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}

View File

@ -1,182 +1,3 @@
### v4.0.0-beta.0 (_2025-06-28_)
- refactor!: bump required node version to 20.19+, 22.12+
- fix(deps)!: update Vite to v7 and remove cjs build
- fix: use `import type` for type-only imports
- perf: build compatibility target for Electron 36 ([#766](https://github.com/alex8088/electron-vite/pull/766))
- perf: build compatibility target for Electron 37
- chore(deps): update pnpm to v10
- chore(deps): update all non-major dependencies
- chore(deps): update lint-staged to v16
### v3.1.0 (_2025-03-25_)
- fix(bytecodePlugin): optimize 'use strict' directive replacement ([#681](https://github.com/alex8088/electron-vite/issues/681))
- perf: build compatilibity target for Electron 35 ([#729](https://github.com/alex8088/electron-vite/pull/729))
- chore(deps): update all non-major dependencies
- chore(deps): update globals to v16
- chore(deps): update esbuild to v0.25
### v3.1.0-beta.0 (_2025-03-12_)
See [v3.1.0-beta.0 changelog](https://github.com/alex8088/electron-vite/blob/v3.1.0-beta.0/CHANGELOG.md)
### v3.0.0 (_2025-02-16_)
- feat: resolve conditions for preload
- perf: build compatilibity target for Electron 32
- perf: build compatilibity target for Electron 33 ([#651](https://github.com/alex8088/electron-vite/pull/651))
- perf: build compatilibity target for Electron 34
- chore: move to eslint flat config
- chore(deps): update all non-major dependencies
- chore(deps): update @rollup/plugin-node-resolve to v16
- chore(deps): update @rollup/plugin-typescript to v12
- chore(deps): update esbuild to v0.24
- chore(deps): update vite to v6
- chore(deps): update @type/node to v22
### v3.0.0-beta.0 (_2025-01-22_)
See [v3.0.0-beta.0 changelog](https://github.com/alex8088/electron-vite/blob/v3.0.0-beta.0/CHANGELOG.md)
### v2.3.0 (_2024-06-23_)
- feat: resolve import.meta.\[dirname|filename\] to support CommonJS format
- fix: don't handle module ID that begin with \0 ([#530](https://github.com/alex8088/electron-vite/pull/530))
- fix: not using the mode from the config file ([#539](https://github.com/alex8088/electron-vite/pull/539))
- fix: default mode should not overrite user config mode
- perf: build compatilibity target for Electron 31
- perf: improve cjs shim
- chore(deps): update all non-major dependencies
- chore(deps): update @typescript-eslint/\* to v7
- chore(deps): update esbuild to v0.21
### v2.2.0 (_2024-04-21_)
- feat: export mergeConfig from vite ([#471](https://github.com/alex8088/electron-vite/issues/471))
- fix(types): narrow down the return type of defineConfig
- perf: build compatilibity target for Electron 30
- refactor(config): defineConfig types
- chore: fix camelcase typo
- chore: use rollup-plugin-rm to clean dist
### v2.1.0 (_2024-03-03_)
- feat: easy way to fork processes and use workers
- fix: config via build.lib fails when default entry point not found ([#393](https://github.com/alex8088/electron-vite/issues/393))
- perf: build compatilibity target for Electron 29
- perf: allow integrating more complex render solutions ([#412](https://github.com/alex8088/electron-vite/pull/412))
- perf(bytecodePlugin): warn that strings cannot be protected when minification is enabled ([#417](https://github.com/alex8088/electron-vite/issues/417))
### v2.0.0 (_2024-01-09_)
- feat: bump minimum node version to 18
- feat: migrate to ESM
- feat: support vite 5
- feat: add package.json to export map
- feat: support ESM in Electron
- feat: env variables prefixed with VITE\_ will be shared in main process and renderer
- feat: support for passing arguments to electron in dev and preview commands ([#339](https://github.com/alex8088/electron-vite/pull/339))
- feat: config file supports "type": "module" in package.json
- fix: emit assets when ssr is enabled
- fix: externalizeDepPlugin not work
- fix: electron's export subpaths also need to be externalized ([#372](https://github.com/alex8088/electron-vite/issues/372))
- perf: improve package.json resolve
- perf: use magic-string hires boundary for sourcemaps
- perf: build compatilibity target for Electron 28
- pref: resolve import meta url in CommonJS format
- perf(worker): ESM syntax
- perf: package version
- perf: dev error message
- perf(externalizeDepsPlugin): use cached package data to improve performance
- perf: loadEnv api also needs to load shared env variables prefixed with VITE\_
- refactor: build
- refactor: file hashes use url-safe base64 encoded hashes in vite 5 (rollup 4)
- refactor: remove Electron 11, 12 build compatilibity target
- refactor: use dynamic import directly
- build: use rollup-plugin-dts
- chore(deps): update all non-major dependencies
- chore(deps): update lint-staged to v15
- chore(deps): update eslint-config-prettier to v9
- chore(deps): update @rollup/plugin-typescript to v11
- chore(deps): update rollup to v4
- chore(deps): update vite to v5
- chore(deps): update esbuild to v0.19
- chore(deps): update typescript to 5.3.3
- chore: improve prettier config
- chore: update homepage
### v2.0.0-beta.4 (_2024-01-06_)
See [v2.0.0-beta.4 changelog](https://github.com/alex8088/electron-vite/blob/v2.0.0-beta.4/CHANGELOG.md)
### v2.0.0-beta.3 (_2024-01-04_)
See [v2.0.0-beta.3 changelog](https://github.com/alex8088/electron-vite/blob/v2.0.0-beta.3/CHANGELOG.md)
### v2.0.0-beta.2 (_2023-12-19_)
See [v2.0.0-beta.2 changelog](https://github.com/alex8088/electron-vite/blob/v2.0.0-beta.2/CHANGELOG.md)
### v2.0.0-beta.1 (_2023-12-14_)
See [v2.0.0-beta.1 changelog](https://github.com/alex8088/electron-vite/blob/v2.0.0-beta.1/CHANGELOG.md)
### v2.0.0-beta.0 (_2023-12-13_)
See [v2.0.0-beta.0 changelog](https://github.com/alex8088/electron-vite/blob/v2.0.0-beta.0/CHANGELOG.md)
### v1.0.29 (_2023-11-17_)
- feat(cli): support --noSandbox option for dev and preview command
- perf: build compatilibity target for Electron 27
### v1.0.28 (_2023-09-18_)
- feat(cli): supports specifying electron entry file ([#270](https://github.com/alex8088/electron-vite/issues/270))
- fix(externalizeDepsPlugin): supports subpath
- perf: build compatilibity target for Electron 26
- chore(types): add .json?commonjs-external&asset typing
### v1.0.27 (_2023-08-01_)
- chore: remove preinstall script
### v1.0.26 (_2023-07-30_)
- feat(cli): add CLI `--inspect[-brk]` to support debugging without IDEs ([#231](https://github.com/alex8088/electron-vite/issues/231))
- feat(types): add process.env.ELECTRON_RENDERER_URL type
- feat(types): add Vite importMeta types
- perf: spawn Electron process using parent's stdios ([#236](https://github.com/alex8088/electron-vite/issues/236))
- chore: update user config interface jsdoc
- chore(deps): update pnpm to v8
- chore(deps): update prettier to v3
- chore(deps): update @typescript-eslint/\* to v6
### v1.0.25 (_2023-07-11_)
- fix: remove node resolve condition for preload [#204](https://github.com/alex8088/electron-vite/issues/204)
- fix(asset): asset handling error when hot reloading
- chore(deps): update all non-major dependencies
- chore(deps): update fs-extra to v11
- chore(deps): update @types/node to v18
- chore(deps): update typescript to 5.0.4
- chore(deps): update vite to 4.4.2
- chore(deps): update esbuild to v0.18
- chore(deps): update rollup to 3.26.2
### v1.0.24 (_2023-06-25_)
- fix(bytecodePlugin): bytecode loader relative path is incorrect
- perf: ignore `browser` field and additional `node` condition for main config
### v1.0.23 (_2023-06-04_)
- feat: supports ES build target for renderer [#174](https://github.com/alex8088/electron-vite/issues/174)
- revert: chore: remove process env define [#159](https://github.com/alex8088/electron-vite/issues/174)
- perf: build compatilibity target for Electron 25
- chore(deps): update all non-major dependencies
### v1.0.22 (_2023-04-23_) ### v1.0.22 (_2023-04-23_)
- feat(cli): add --rendererOnly flag to dev command - feat(cli): add --rendererOnly flag to dev command

115
README.md
View File

@ -9,17 +9,17 @@
<p align="center"> <p align="center">
<img src="https://img.shields.io/npm/v/electron-vite?color=6988e6&label=version"> <img src="https://img.shields.io/npm/v/electron-vite?color=6988e6&label=version">
<img src="https://img.shields.io/github/license/alex8088/electron-vite?color=blue" alt="license" /> <img src="https://img.shields.io/github/license/alex8088/wx-vue-next?color=blue" alt="license" />
</p> </p>
<p align="center"> <p align="center">
<a href="https://electron-vite.org">Documentation</a> | <a href="https://evite.netlify.app/">Documentation</a> |
<a href="https://electron-vite.org/guide">Getting Started</a> | <a href="https://evite.netlify.app/guide/">Getting Started</a> |
<a href="https://github.com/alex8088/quick-start/tree/master/packages/create-electron">create-electron</a> <a href="https://github.com/alex8088/quick-start/tree/master/packages/create-electron">create-electron</a>
</p> </p>
<p align="center"> <p align="center">
<a href="https://cn.electron-vite.org">中文文档</a> <a href="https://cn-evite.netlify.app/">中文文档</a>
</p> </p>
<br /> <br />
@ -28,13 +28,15 @@
## Features ## Features
- ⚡️ [Vite](https://vitejs.dev) powered and use the same way. - ⚡️ [Vite](https://vitejs.dev) powered and use the same way.
- 🛠 Pre-configured for Electron, don't worry about configuration. - 🛠 Centralized Configuration.
- 💡 Optimize asset handling (Node.js addons, WebAssembly, Worker Thread, etc). - 💡 Pre-configured for Electron, don't worry about configuration.
- 🚀 Fast HMR for renderer processes. - 🚀 Fast HMR for renderers.
- 🔥 Hot reloading for main process and preload scripts. - 🔥 Hot reloading for main process and preload scripts.
- 🔌 Easy to debug in IDEs like VSCode or WebStorm. - 🔌 Easy to debug.
- 🔋 Static asset handling (Node.js addons, WebAssembly, etc).
- 🔒 Compile to v8 bytecode to protect source code. - 🔒 Compile to v8 bytecode to protect source code.
- 🏷️ Support for TypeScript decorators. - 🏷️ Support for TypeScript decorators.
- 🔩 Easy to use workers and fork process.
- 📦 Out-of-the-box support for TypeScript, Vue, React, Svelte, SolidJS and more. - 📦 Out-of-the-box support for TypeScript, Vue, React, Svelte, SolidJS and more.
## Usage ## Usage
@ -59,7 +61,7 @@ In a project where `electron-vite` is installed, you can use `electron-vite` bin
} }
``` ```
### Configuration ### Configuring
When running `electron-vite` from the command line, electron-vite will automatically try to resolve a config file named `electron.vite.config.js` inside project root. The most basic config file looks like this: When running `electron-vite` from the command line, electron-vite will automatically try to resolve a config file named `electron.vite.config.js` inside project root. The most basic config file looks like this:
@ -78,24 +80,97 @@ export default {
} }
``` ```
### Use HMR in Renderer
In order to use the renderer process HMR, you need to use the `environment variables` to determine whether the window browser loads a local html file or a local URL.
```js
function createWindow() {
// Create the browser window
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, '../preload/index.js')
}
})
// Load the remote URL for development or the local html file for production
if (!app.isPackaged && process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
mainWindow.loadFile(path.join(__dirname, '../renderer/index.html'))
}
}
```
### Hot Reloading
Hot reloading refers to quickly rebuilding and restarting the Electron app when the main process or preload scripts module changes. In fact, it's not really hot reloading, but similar. It also brings a good development experience to developers.
There are two ways to enable it:
1. Use CLI option `-w` or `--watch`, e.g. `electron-vite dev --watch`. This is the preferred way, it's more flexible.
2. Use configuration option `build.watch` and set to `{}`. In addition, more watcher options can be configured, see [WatcherOptions](https://rollupjs.org/guide/en/#watch-options).
### Debugging in VSCode
Add a file `.vscode/launch.json` with the following configuration:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-vite",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron-vite.cmd"
},
"runtimeArgs": ["--sourcemap"]
}
]
}
```
Then, set some breakpoints in `main.ts` (source code), and start debugging in the `VSCode Debug View`.
### Source Code Protection
Use the plugin `bytecodePlugin` to enable it:
```js
import { defineConfig, bytecodePlugin } from 'electron-vite'
export default defineConfig({
main: {
plugins: [bytecodePlugin()]
},
preload: {
plugins: [bytecodePlugin()]
},
renderer: {
// ...
}
})
```
`bytecodePlugin` only works in production and supports the main process and preload scripts.
Also, you can learn more by playing with the [example](https://github.com/alex8088/electron-vite-bytecode-example).
### Getting Started ### Getting Started
Clone the [electron-vite-boilerplate](https://github.com/alex8088/electron-vite-boilerplate) or use the [create-electron](https://github.com/alex8088/quick-start/tree/master/packages/create-electron) tool to scaffold your project. Clone the [electron-vite-boilerplate](https://github.com/alex8088/electron-vite-boilerplate) or use the [create-electron](https://github.com/alex8088/quick-start/tree/master/packages/create-electron) tool to scaffold your project.
```bash ```bash
npm create @quick-start/electron npm init @quick-start/electron
``` ```
Currently supported template presets include:
| JavaScript | TypeScript |
| :--------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------: |
| [vanilla](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/vanilla) | [vanilla-ts](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/vanilla-ts) |
| [vue](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/vue) | [vue-ts](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/vue-ts) |
| [react](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/react) | [react-ts](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/react-ts) |
| [svelte](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/svelte) | [svelte-ts](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/svelte-ts) |
| [solid](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/solid) | [solid-ts](https://github.com/alex8088/quick-start/tree/master/packages/create-electron/playground/solid-ts) |
## Contribution ## Contribution
See [Contributing Guide](CONTRIBUTING.md). See [Contributing Guide](CONTRIBUTING.md).

52
api-extractor.json Normal file
View File

@ -0,0 +1,52 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "./dist/types/index.d.ts",
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "",
"publicTrimmedFilePath": "./dist/index.d.ts"
},
"apiReport": {
"enabled": false
},
"docModel": {
"enabled": false
},
"tsdocMetadata": {
"enabled": false
},
"messages": {
"compilerMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"extractorMessageReporting": {
"default": {
"logLevel": "warning",
"addToApiReportFile": true
},
"ae-missing-release-tag": {
"logLevel": "none"
}
},
"tsdocMessageReporting": {
"default": {
"logLevel": "warning"
},
"tsdoc-undefined-tag": {
"logLevel": "none"
}
}
}
}

View File

@ -24,7 +24,7 @@ if (debugIndex > 0) {
} }
function run() { function run() {
import('../dist/cli.js') require('../dist/cli')
} }
run() run()

View File

@ -1,64 +0,0 @@
// ts-check
import eslint from '@eslint/js'
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'
import globals from 'globals'
import tseslint from 'typescript-eslint'
export default tseslint.config(
{ ignores: ['**/node_modules', '**/dist', '**/bin'] },
eslint.configs.recommended,
tseslint.configs.recommended,
eslintPluginPrettierRecommended,
{
languageOptions: {
parser: tseslint.parser,
parserOptions: {
sourceType: 'module',
ecmaVersion: 2022
},
globals: {
...globals.es2021,
...globals.node
}
},
settings: {
node: {
version: '^20.19.0 || >=22.12.0'
}
},
rules: {
'prettier/prettier': 'warn',
'@typescript-eslint/ban-ts-comment': ['error', { 'ts-ignore': 'allow-with-description' }],
'@typescript-eslint/explicit-function-return-type': [
'error',
{
allowExpressions: true,
allowTypedFunctionExpressions: true,
allowHigherOrderFunctions: true,
allowIIFEs: true
}
],
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-empty-function': ['error', { allow: ['arrowFunctions'] }],
'@typescript-eslint/no-empty-object-type': ['error', { allowInterfaces: 'always' }],
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-require-imports': 'error',
'@typescript-eslint/no-unused-expressions': [
'error',
{
allowShortCircuit: true,
allowTaggedTemplates: true,
allowTernary: true
}
]
}
},
{
files: ['*.js', '*.mjs'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off'
}
}
)

83
node.d.ts vendored
View File

@ -4,12 +4,6 @@ declare module '*?nodeWorker' {
export default function (options: WorkerOptions): Worker export default function (options: WorkerOptions): Worker
} }
// module path
declare module '*?modulePath' {
const src: string
export default src
}
// node asset // node asset
declare module '*?asset' { declare module '*?asset' {
const src: string const src: string
@ -21,11 +15,6 @@ declare module '*?asset&asarUnpack' {
export default src export default src
} }
declare module '*.json?commonjs-external&asset' {
const src: string
export default src
}
// native node module // native node module
declare module '*.node' { declare module '*.node' {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ /* eslint-disable-next-line @typescript-eslint/no-explicit-any */
@ -38,75 +27,3 @@ declare module '*.wasm?loader' {
const loadWasm: (options?: WebAssembly.Imports) => Promise<WebAssembly.Instance> const loadWasm: (options?: WebAssembly.Imports) => Promise<WebAssembly.Instance>
export default loadWasm export default loadWasm
} }
// build-in process env
declare namespace NodeJS {
interface ProcessEnv {
/**
* Vite's dev server address for Electron renderers.
*/
readonly ELECTRON_RENDERER_URL?: string
}
}
// Refer to Vite's ImportMeta type declarations
// <https://github.com/vitejs/vite/blob/main/packages/vite/types/importMeta.d.ts>
interface ImportMetaEnv {
MODE: string
DEV: boolean
PROD: boolean
}
interface ImportGlobOptions<Eager extends boolean, AsType extends string> {
/**
* Import type for the import url.
*/
as?: AsType
/**
* Import as static or dynamic
*
* @default false
*/
eager?: Eager
/**
* Import only the specific named export. Set to `default` to import the default export.
*/
import?: string
/**
* Custom queries
*/
query?: string | Record<string, string | number | boolean>
/**
* Search files also inside `node_modules/` and hidden directories (e.g. `.git/`). This might have impact on performance.
*
* @default false
*/
exhaustive?: boolean
}
interface KnownAsTypeMap {
raw: string
url: string
worker: Worker
}
interface ImportGlobFunction {
/**
* Import a list of files with a glob pattern.
*
* https://vitejs.dev/guide/features.html#glob-import
*/
<Eager extends boolean, As extends string, T = As extends keyof KnownAsTypeMap ? KnownAsTypeMap[As] : unknown>(
glob: string | string[],
options?: ImportGlobOptions<Eager, As>
): (Eager extends true ? true : false) extends true ? Record<string, T> : Record<string, () => Promise<T>>
<M>(glob: string | string[], options?: ImportGlobOptions<false, string>): Record<string, () => Promise<M>>
<M>(glob: string | string[], options: ImportGlobOptions<true, string>): Record<string, M>
}
interface ImportMeta {
url: string
readonly env: ImportMetaEnv
glob: ImportGlobFunction
}

View File

@ -1,17 +1,9 @@
{ {
"name": "electron-vite", "name": "electron-vite",
"version": "4.0.0-beta.0", "version": "1.0.22",
"description": "Electron build tooling based on Vite", "description": "Electron build tooling based on Vite",
"type": "module", "main": "dist/index.js",
"main": "./dist/index.js", "types": "dist/index.d.ts",
"types": "./dist/index.d.ts",
"exports": {
".": "./dist/index.js",
"./node": {
"types": "./node.d.ts"
},
"./package.json": "./package.json"
},
"bin": { "bin": {
"electron-vite": "bin/electron-vite.js" "electron-vite": "bin/electron-vite.js"
}, },
@ -21,9 +13,8 @@
"node.d.ts" "node.d.ts"
], ],
"engines": { "engines": {
"node": "^20.19.0 || >=22.12.0" "node": "^14.18.0 || >=16.0.0"
}, },
"packageManager": "pnpm@10.12.4",
"author": "Alex Wei<https://github.com/alex8088>", "author": "Alex Wei<https://github.com/alex8088>",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
@ -33,7 +24,7 @@
"bugs": { "bugs": {
"url": "https://github.com/alex8088/electron-vite/issues" "url": "https://github.com/alex8088/electron-vite/issues"
}, },
"homepage": "https://electron-vite.org", "homepage": "https://github.com/alex8088/electron-vite#readme",
"keywords": [ "keywords": [
"electron", "electron",
"vite", "vite",
@ -42,9 +33,9 @@
], ],
"scripts": { "scripts": {
"format": "prettier --write .", "format": "prettier --write .",
"lint": "eslint --cache .", "lint": "eslint --ext .ts src/**",
"typecheck": "tsc --noEmit", "typecheck": "tsc --noEmit",
"build": "pnpm run lint && rollup -c rollup.config.ts --configPlugin typescript" "build": "npm run lint && node scripts/build.js"
}, },
"simple-git-hooks": { "simple-git-hooks": {
"pre-commit": "npx lint-staged", "pre-commit": "npx lint-staged",
@ -61,7 +52,7 @@
}, },
"peerDependencies": { "peerDependencies": {
"@swc/core": "^1.0.0", "@swc/core": "^1.0.0",
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0" "vite": "^3.0.0 || ^4.0.0"
}, },
"peerDependenciesMeta": { "peerDependenciesMeta": {
"@swc/core": { "@swc/core": {
@ -69,40 +60,31 @@
} }
}, },
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.29.0", "@microsoft/api-extractor": "^7.34.4",
"@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-node-resolve": "^16.0.1", "@rollup/plugin-typescript": "^9.0.2",
"@rollup/plugin-typescript": "^12.1.3", "@swc/core": "^1.3.37",
"@swc/core": "^1.12.7", "@types/node": "16.18.3",
"@types/node": "^22.15.33", "@typescript-eslint/eslint-plugin": "^5.54.0",
"eslint": "^9.29.0", "@typescript-eslint/parser": "^5.54.0",
"eslint-config-prettier": "^10.1.5", "eslint": "^8.35.0",
"eslint-plugin-prettier": "^5.5.1", "eslint-config-prettier": "^8.6.0",
"globals": "^16.2.0", "eslint-plugin-prettier": "^4.2.1",
"lint-staged": "^16.1.2", "fs-extra": "^10.1.0",
"prettier": "^3.6.2", "lint-staged": "^13.1.2",
"rollup": "^4.44.1", "prettier": "^2.8.4",
"rollup-plugin-dts": "^6.2.1", "rollup": "^3.18.0",
"rollup-plugin-rm": "^1.0.2", "simple-git-hooks": "^2.8.1",
"simple-git-hooks": "^2.13.0", "tslib": "^2.5.0",
"tslib": "^2.8.1", "typescript": "^4.8.4",
"typescript": "^5.8.3", "vite": "^4.2.1"
"typescript-eslint": "^8.35.0",
"vite": "^7.0.0"
}, },
"dependencies": { "dependencies": {
"@babel/core": "^7.27.7", "@babel/core": "^7.21.0",
"@babel/plugin-transform-arrow-functions": "^7.27.1", "@babel/plugin-transform-arrow-functions": "^7.20.7",
"cac": "^6.7.14", "cac": "^6.7.14",
"esbuild": "^0.25.5", "esbuild": "^0.17.13",
"magic-string": "^0.30.17", "magic-string": "^0.30.0",
"picocolors": "^1.1.1" "picocolors": "^1.0.0"
},
"pnpm": {
"onlyBuiltDependencies": [
"@swc/core",
"esbuild",
"simple-git-hooks"
]
} }
} }

4098
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,41 +0,0 @@
import { createRequire } from 'node:module'
import { defineConfig } from 'rollup'
import ts from '@rollup/plugin-typescript'
import resolve from '@rollup/plugin-node-resolve'
import json from '@rollup/plugin-json'
import dts from 'rollup-plugin-dts'
import rm from 'rollup-plugin-rm'
const require = createRequire(import.meta.url)
const pkg = require('./package.json')
const external = ['esbuild', ...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})]
export default defineConfig([
{
input: ['src/index.ts', 'src/cli.ts'],
output: [
{
dir: 'dist',
entryFileNames: '[name].js',
chunkFileNames: 'chunks/lib-[hash].js',
format: 'es'
}
],
external,
plugins: [
rm('dist', 'buildStart'),
json(),
ts({ compilerOptions: { rootDir: 'src', declaration: true, declarationDir: 'dist/types' } }),
resolve()
],
treeshake: {
moduleSideEffects: false
}
},
{
input: 'dist/types/index.d.ts',
output: [{ file: pkg.types, format: 'es' }],
plugins: [dts(), rm('dist/types', 'buildEnd')]
}
])

67
scripts/build.js Normal file
View File

@ -0,0 +1,67 @@
const path = require('path')
const colors = require('picocolors')
const fs = require('fs-extra')
const rollup = require('rollup')
const typescript = require('@rollup/plugin-typescript')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor')
;(async () => {
const dist = path.resolve(__dirname, '../dist')
await fs.remove(dist)
console.log()
console.log(colors.bold(colors.yellow(`Rolling up ts code...`)))
const pkg = require('../package.json')
const external = ['esbuild', ...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})]
const bundle = await rollup.rollup({
input: {
index: path.resolve(__dirname, '../src/index.ts'),
cli: path.resolve(__dirname, '../src/cli.ts')
},
external,
plugins: [
typescript({
tsconfig: path.resolve(__dirname, '../tsconfig.json')
}),
nodeResolve()
],
treeshake: {
moduleSideEffects: false
}
})
await bundle.write({
dir: dist,
entryFileNames: '[name].js',
chunkFileNames: 'chunks/lib-[hash].js',
format: 'cjs'
})
console.log(colors.bold(colors.yellow(`Rolling up type definitions...`)))
if (pkg.types) {
const extractorConfig = ExtractorConfig.loadFileAndPrepare(path.resolve(__dirname, '../api-extractor.json'))
const extractorResult = Extractor.invoke(extractorConfig, {
localBuild: true,
showVerboseMessages: true
})
if (extractorResult.succeeded) {
console.log(colors.green('API Extractor completed successfully'))
} else {
console.error(
`API Extractor completed with ${extractorResult.errorCount} errors` +
` and ${extractorResult.warningCount} warnings`
)
process.exitCode = 1
}
}
await fs.remove(path.resolve(dist, 'types'))
console.log(colors.green(`Build ${pkg.name}@${pkg.version} successfully`))
})()

View File

@ -1,13 +1,13 @@
// Invoked on the commit-msg git hook by simple-git-hooks. // Invoked on the commit-msg git hook by simple-git-hooks.
import colors from 'picocolors' const colors = require('picocolors')
import fs from 'node:fs' const fs = require('fs')
const msgPath = process.argv[2] const msgPath = process.argv[2]
const msg = fs.readFileSync(msgPath, 'utf-8').trim() const msg = fs.readFileSync(msgPath, 'utf-8').trim()
const commitRE = const commitRE =
/^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\(.+\))?!?: .{1,50}/ /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\(.+\))?: .{1,50}/
if (!commitRE.test(msg)) { if (!commitRE.test(msg)) {
console.log() console.log()

View File

@ -1,5 +1,5 @@
import { build as viteBuild } from 'vite' import { build as viteBuild } from 'vite'
import { type InlineConfig, resolveConfig } from './config' import { InlineConfig, resolveConfig } from './config'
/** /**
* Bundles the electron app for production. * Bundles the electron app for production.

View File

@ -1,8 +1,7 @@
import { cac } from 'cac' import { cac } from 'cac'
import colors from 'picocolors' import colors from 'picocolors'
import { type LogLevel, createLogger } from 'vite' import { LogLevel, createLogger } from 'vite'
import type { InlineConfig } from './config' import { InlineConfig } from './config'
import { version } from '../package.json'
const cli = cac('electron-vite') const cli = cac('electron-vite')
@ -25,20 +24,6 @@ interface GlobalCLIOptions {
w?: boolean w?: boolean
watch?: boolean watch?: boolean
outDir?: string outDir?: string
entry?: string
}
interface DevCLIOptions {
inspect?: boolean | string
inspectBrk?: boolean | string
remoteDebuggingPort?: string
noSandbox?: boolean
rendererOnly?: boolean
}
interface PreviewCLIOptions {
noSandbox?: boolean
skipBuild?: boolean
} }
function createInlineConfig(root: string, options: GlobalCLIOptions): InlineConfig { function createInlineConfig(root: string, options: GlobalCLIOptions): InlineConfig {
@ -67,7 +52,6 @@ cli
.option('--ignoreConfigWarning', `[boolean] ignore config warning`) .option('--ignoreConfigWarning', `[boolean] ignore config warning`)
.option('--sourcemap', `[boolean] output source maps for debug (default: false)`) .option('--sourcemap', `[boolean] output source maps for debug (default: false)`)
.option('--outDir <dir>', `[string] output directory (default: out)`) .option('--outDir <dir>', `[string] output directory (default: out)`)
.option('--entry <file>', `[string] specify electron entry file`)
// dev // dev
cli cli
@ -75,36 +59,13 @@ cli
.alias('serve') .alias('serve')
.alias('dev') .alias('dev')
.option('-w, --watch', `[boolean] rebuilds when main process or preload script modules have changed on disk`) .option('-w, --watch', `[boolean] rebuilds when main process or preload script modules have changed on disk`)
.option('--inspect [port]', `[boolean | number] enable V8 inspector on the specified port`)
.option('--inspectBrk [port]', `[boolean | number] enable V8 inspector on the specified port`)
.option('--remoteDebuggingPort <port>', `[string] port for remote debugging`) .option('--remoteDebuggingPort <port>', `[string] port for remote debugging`)
.option('--noSandbox', `[boolean] forces renderer process to run un-sandboxed`)
.option('--rendererOnly', `[boolean] only dev server for the renderer`) .option('--rendererOnly', `[boolean] only dev server for the renderer`)
.action(async (root: string, options: DevCLIOptions & GlobalCLIOptions) => { .action(async (root: string, options: { remoteDebuggingPort?: string; rendererOnly: boolean } & GlobalCLIOptions) => {
if (options.remoteDebuggingPort) { if (options.remoteDebuggingPort) {
process.env.REMOTE_DEBUGGING_PORT = options.remoteDebuggingPort process.env.REMOTE_DEBUGGING_PORT = options.remoteDebuggingPort
} }
if (options.inspect) {
process.env.V8_INSPECTOR_PORT = typeof options.inspect === 'number' ? `${options.inspect}` : '5858'
}
if (options.inspectBrk) {
process.env.V8_INSPECTOR_BRK_PORT = typeof options.inspectBrk === 'number' ? `${options.inspectBrk}` : '5858'
}
if (options.noSandbox) {
process.env.NO_SANDBOX = '1'
}
if (options['--']) {
process.env.ELECTRON_CLI_ARGS = JSON.stringify(options['--'])
}
if (options.entry) {
process.env.ELECTRON_ENTRY = options.entry
}
const { createServer } = await import('./server') const { createServer } = await import('./server')
const inlineConfig = createInlineConfig(root, options) const inlineConfig = createInlineConfig(root, options)
@ -125,10 +86,6 @@ cli.command('build [root]', 'build for production').action(async (root: string,
const { build } = await import('./build') const { build } = await import('./build')
const inlineConfig = createInlineConfig(root, options) const inlineConfig = createInlineConfig(root, options)
if (options.entry) {
process.env.ELECTRON_ENTRY = options.entry
}
try { try {
await build(inlineConfig) await build(inlineConfig)
} catch (e) { } catch (e) {
@ -141,24 +98,11 @@ cli.command('build [root]', 'build for production').action(async (root: string,
// preview // preview
cli cli
.command('preview [root]', 'start electron app to preview production build') .command('preview [root]', 'start electron app to preview production build')
.option('--noSandbox', `[boolean] forces renderer process to run un-sandboxed`)
.option('--skipBuild', `[boolean] skip build`) .option('--skipBuild', `[boolean] skip build`)
.action(async (root: string, options: PreviewCLIOptions & GlobalCLIOptions) => { .action(async (root: string, options: { skipBuild?: boolean } & GlobalCLIOptions) => {
const { preview } = await import('./preview') const { preview } = await import('./preview')
const inlineConfig = createInlineConfig(root, options) const inlineConfig = createInlineConfig(root, options)
if (options.noSandbox) {
process.env.NO_SANDBOX = '1'
}
if (options.entry) {
process.env.ELECTRON_ENTRY = options.entry
}
if (options['--']) {
process.env.ELECTRON_CLI_ARGS = JSON.stringify(options['--'])
}
try { try {
await preview(inlineConfig, { skipBuild: options.skipBuild }) await preview(inlineConfig, { skipBuild: options.skipBuild })
} catch (e) { } catch (e) {
@ -169,6 +113,6 @@ cli
}) })
cli.help() cli.help()
cli.version(version) cli.version(require('../package.json').version)
cli.parse() cli.parse()

View File

@ -5,7 +5,7 @@ import { createRequire } from 'node:module'
import colors from 'picocolors' import colors from 'picocolors'
import { import {
type UserConfig as ViteConfig, type UserConfig as ViteConfig,
type UserConfigExport as ViteConfigExport, type UserConfigExport as UserViteConfigExport,
type ConfigEnv, type ConfigEnv,
type Plugin, type Plugin,
type LogLevel, type LogLevel,
@ -18,10 +18,7 @@ import { build } from 'esbuild'
import { electronMainVitePlugin, electronPreloadVitePlugin, electronRendererVitePlugin } from './plugins/electron' import { electronMainVitePlugin, electronPreloadVitePlugin, electronRendererVitePlugin } from './plugins/electron'
import assetPlugin from './plugins/asset' import assetPlugin from './plugins/asset'
import workerPlugin from './plugins/worker' import workerPlugin from './plugins/worker'
import importMetaPlugin from './plugins/importMeta' import { isObject, dynamicImport } from './utils'
import esmShimPlugin from './plugins/esm'
import modulePathPlugin from './plugins/modulePath'
import { isObject, isFilePathESM } from './utils'
export { defineConfig as defineViteConfig } from 'vite' export { defineConfig as defineViteConfig } from 'vite'
@ -29,42 +26,42 @@ export interface UserConfig {
/** /**
* Vite config options for electron main process * Vite config options for electron main process
* *
* https://vitejs.dev/config/ * https://cn.vitejs.dev/config/
*/ */
main?: ViteConfig & { configFile?: string | false } main?: ViteConfig & { configFile?: string | false }
/** /**
* Vite config options for electron renderer process * Vite config options for electron renderer process
* *
* https://vitejs.dev/config/ * https://cn.vitejs.dev/config/
*/ */
renderer?: ViteConfig & { configFile?: string | false } renderer?: ViteConfig & { configFile?: string | false }
/** /**
* Vite config options for electron preload files * Vite config options for electron preload files
* *
* https://vitejs.dev/config/ * https://cn.vitejs.dev/config/
*/ */
preload?: ViteConfig & { configFile?: string | false } preload?: ViteConfig & { configFile?: string | false }
} }
export interface ElectronViteConfig { export interface UserConfigSchema {
/** /**
* Vite config options for electron main process * Vite config options for electron main process
* *
* https://vitejs.dev/config/ * https://cn.vitejs.dev/config/
*/ */
main?: ViteConfigExport main?: UserViteConfigExport
/** /**
* Vite config options for electron renderer process * Vite config options for electron renderer process
* *
* https://vitejs.dev/config/ * https://cn.vitejs.dev/config/
*/ */
renderer?: ViteConfigExport renderer?: UserViteConfigExport
/** /**
* Vite config options for electron preload files * Vite config options for electron preload files
* *
* https://vitejs.dev/config/ * https://cn.vitejs.dev/config/
*/ */
preload?: ViteConfigExport preload?: UserViteConfigExport
} }
export type InlineConfig = Omit<ViteConfig, 'base'> & { export type InlineConfig = Omit<ViteConfig, 'base'> & {
@ -73,28 +70,16 @@ export type InlineConfig = Omit<ViteConfig, 'base'> & {
ignoreConfigWarning?: boolean ignoreConfigWarning?: boolean
} }
export type ElectronViteConfigFnObject = (env: ConfigEnv) => ElectronViteConfig export type UserConfigFn = (env: ConfigEnv) => UserConfigSchema | Promise<UserConfigSchema>
export type ElectronViteConfigFnPromise = (env: ConfigEnv) => Promise<ElectronViteConfig> export type UserConfigExport = UserConfigSchema | Promise<UserConfigSchema> | UserConfigFn
export type ElectronViteConfigFn = (env: ConfigEnv) => ElectronViteConfig | Promise<ElectronViteConfig>
export type ElectronViteConfigExport =
| ElectronViteConfig
| Promise<ElectronViteConfig>
| ElectronViteConfigFnObject
| ElectronViteConfigFnPromise
| ElectronViteConfigFn
/** /**
* Type helper to make it easier to use `electron.vite.config.*` * Type helper to make it easier to use `electron.vite.config.*`
* accepts a direct {@link ElectronViteConfig} object, or a function that returns it. * accepts a direct {@link UserConfig} object, or a function that returns it.
* The function receives a object that exposes two properties: * The function receives a object that exposes two properties:
* `command` (either `'build'` or `'serve'`), and `mode`. * `command` (either `'build'` or `'serve'`), and `mode`.
*/ */
export function defineConfig(config: ElectronViteConfig): ElectronViteConfig export function defineConfig(config: UserConfigExport): UserConfigExport {
export function defineConfig(config: Promise<ElectronViteConfig>): Promise<ElectronViteConfig>
export function defineConfig(config: ElectronViteConfigFnObject): ElectronViteConfigFnObject
export function defineConfig(config: ElectronViteConfigExport): ElectronViteConfigExport
export function defineConfig(config: ElectronViteConfigExport): ElectronViteConfigExport {
return config return config
} }
@ -112,6 +97,8 @@ export async function resolveConfig(
const config = inlineConfig const config = inlineConfig
const mode = inlineConfig.mode || defaultMode const mode = inlineConfig.mode || defaultMode
config.mode = mode
process.env.NODE_ENV = defaultMode process.env.NODE_ENV = defaultMode
let userConfig: UserConfig | undefined let userConfig: UserConfig | undefined
@ -140,20 +127,11 @@ export async function resolveConfig(
if (loadResult.config.main) { if (loadResult.config.main) {
const mainViteConfig: ViteConfig = mergeConfig(loadResult.config.main, deepClone(config)) const mainViteConfig: ViteConfig = mergeConfig(loadResult.config.main, deepClone(config))
mainViteConfig.mode = inlineConfig.mode || mainViteConfig.mode || defaultMode
if (outDir) { if (outDir) {
resetOutDir(mainViteConfig, outDir, 'main') resetOutDir(mainViteConfig, outDir, 'main')
} }
mergePlugins(mainViteConfig, [ mergePlugins(mainViteConfig, [...electronMainVitePlugin({ root }), assetPlugin(), workerPlugin()])
...electronMainVitePlugin({ root }),
assetPlugin(),
workerPlugin(),
modulePathPlugin(),
importMetaPlugin(),
esmShimPlugin()
])
loadResult.config.main = mainViteConfig loadResult.config.main = mainViteConfig
loadResult.config.main.configFile = false loadResult.config.main.configFile = false
@ -162,17 +140,10 @@ export async function resolveConfig(
if (loadResult.config.preload) { if (loadResult.config.preload) {
const preloadViteConfig: ViteConfig = mergeConfig(loadResult.config.preload, deepClone(config)) const preloadViteConfig: ViteConfig = mergeConfig(loadResult.config.preload, deepClone(config))
preloadViteConfig.mode = inlineConfig.mode || preloadViteConfig.mode || defaultMode
if (outDir) { if (outDir) {
resetOutDir(preloadViteConfig, outDir, 'preload') resetOutDir(preloadViteConfig, outDir, 'preload')
} }
mergePlugins(preloadViteConfig, [ mergePlugins(preloadViteConfig, [...electronPreloadVitePlugin({ root }), assetPlugin()])
...electronPreloadVitePlugin({ root }),
assetPlugin(),
importMetaPlugin(),
esmShimPlugin()
])
loadResult.config.preload = preloadViteConfig loadResult.config.preload = preloadViteConfig
loadResult.config.preload.configFile = false loadResult.config.preload.configFile = false
@ -181,8 +152,6 @@ export async function resolveConfig(
if (loadResult.config.renderer) { if (loadResult.config.renderer) {
const rendererViteConfig: ViteConfig = mergeConfig(loadResult.config.renderer, deepClone(config)) const rendererViteConfig: ViteConfig = mergeConfig(loadResult.config.renderer, deepClone(config))
rendererViteConfig.mode = inlineConfig.mode || rendererViteConfig.mode || defaultMode
if (outDir) { if (outDir) {
resetOutDir(rendererViteConfig, outDir, 'renderer') resetOutDir(rendererViteConfig, outDir, 'renderer')
} }
@ -258,7 +227,11 @@ export async function loadConfigFromFile(
} }
} }
const isESM = isFilePathESM(resolvedPath) // electron does not support adding type: "module" to package.json
let isESM = false
if (/\.m[jt]s$/.test(resolvedPath) || resolvedPath.endsWith('.ts')) {
isESM = true
}
try { try {
const bundled = await bundleConfigFile(resolvedPath, isESM) const bundled = await bundleConfigFile(resolvedPath, isESM)
@ -343,7 +316,7 @@ async function bundleConfigFile(fileName: string, isESM: boolean): Promise<{ cod
absWorkingDir: process.cwd(), absWorkingDir: process.cwd(),
entryPoints: [fileName], entryPoints: [fileName],
write: false, write: false,
target: ['node20'], target: ['node14.18', 'node16'],
platform: 'node', platform: 'node',
bundle: true, bundle: true,
format: isESM ? 'esm' : 'cjs', format: isESM ? 'esm' : 'cjs',
@ -406,18 +379,17 @@ async function loadConfigFormBundledFile(
configFile: string, configFile: string,
bundledCode: string, bundledCode: string,
isESM: boolean isESM: boolean
): Promise<ElectronViteConfigExport> { ): Promise<UserConfigExport> {
if (isESM) { if (isESM) {
const fileNameTmp = path.resolve(configRoot, `${CONFIG_FILE_NAME}.${Date.now()}.mjs`) const fileNameTmp = path.resolve(configRoot, `${CONFIG_FILE_NAME}.${Date.now()}.mjs`)
fs.writeFileSync(fileNameTmp, bundledCode) fs.writeFileSync(fileNameTmp, bundledCode)
const fileUrl = pathToFileURL(fileNameTmp) const fileUrl = pathToFileURL(fileNameTmp)
try { try {
return (await import(fileUrl.href)).default return (await dynamicImport(fileUrl)).default
} finally { } finally {
try { try {
fs.unlinkSync(fileNameTmp) fs.unlinkSync(fileNameTmp)
// eslint-disable-next-line no-empty
} catch {} } catch {}
} }
} else { } else {

View File

@ -1,19 +1,19 @@
import path from 'node:path' import path from 'node:path'
import fs from 'node:fs' import fs from 'node:fs'
import { createRequire } from 'node:module' import { createRequire } from 'node:module'
import { type ChildProcess, spawn } from 'node:child_process' import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
import { loadPackageData } from './utils' import { type Logger } from 'vite'
const _require = createRequire(import.meta.url) const _require = createRequire(import.meta.url)
const ensureElectronEntryFile = (root = process.cwd()): void => { const ensureElectronEntryFile = (root = process.cwd()): void => {
if (process.env.ELECTRON_ENTRY) return const pkg = path.join(root, 'package.json')
const pkg = loadPackageData() if (fs.existsSync(pkg)) {
if (pkg) { const main = require(pkg).main
if (!pkg.main) { if (!main) {
throw new Error('No entry point found for electron app, please add a "main" field to package.json') throw new Error('No entry point found for electron app, please add a "main" field to package.json')
} else { } else {
const entryPath = path.resolve(root, pkg.main) const entryPath = path.resolve(root, main)
if (!fs.existsSync(entryPath)) { if (!fs.existsSync(entryPath)) {
throw new Error(`No electron app entry file found: ${entryPath}`) throw new Error(`No electron app entry file found: ${entryPath}`)
} }
@ -36,16 +36,6 @@ const getElectronMajorVer = (): string => {
return majorVer return majorVer
} }
export function supportESM(): boolean {
const majorVer = getElectronMajorVer()
return parseInt(majorVer) >= 28
}
export function getElectronMajorVersion(): number {
const majorVer = getElectronMajorVer()
return parseInt(majorVer)
}
export function getElectronPath(): string { export function getElectronPath(): string {
let electronExecPath = process.env.ELECTRON_EXEC_PATH || '' let electronExecPath = process.env.ELECTRON_EXEC_PATH || ''
if (!electronExecPath) { if (!electronExecPath) {
@ -69,26 +59,20 @@ export function getElectronNodeTarget(): string {
const electronVer = getElectronMajorVer() const electronVer = getElectronMajorVer()
const nodeVer = { const nodeVer = {
'37': '22.16',
'36': '22.14',
'35': '22.14',
'34': '20.18',
'33': '20.18',
'32': '20.16',
'31': '20.14',
'30': '20.11',
'29': '20.9',
'28': '18.18',
'27': '18.17',
'26': '18.16',
'25': '18.15',
'24': '18.14', '24': '18.14',
'23': '18.12', '23': '18.12',
'22': '16.17', '22': '16.17',
'21': '16.16', '21': '16.16',
'20': '16.15', '20': '16.15',
'19': '16.14', '19': '16.14',
'18': '16.13' '18': '16.13',
'17': '16.13',
'16': '16.9',
'15': '16.5',
'14': '14.17',
'13': '14.17',
'12': '14.16',
'11': '12.18'
} }
if (electronVer && parseInt(electronVer) > 10) { if (electronVer && parseInt(electronVer) > 10) {
let target = nodeVer[electronVer] let target = nodeVer[electronVer]
@ -102,26 +86,20 @@ export function getElectronChromeTarget(): string {
const electronVer = getElectronMajorVer() const electronVer = getElectronMajorVer()
const chromeVer = { const chromeVer = {
'37': '138',
'36': '136',
'35': '134',
'34': '132',
'33': '130',
'32': '128',
'31': '126',
'30': '124',
'29': '122',
'28': '120',
'27': '118',
'26': '116',
'25': '114',
'24': '112', '24': '112',
'23': '110', '23': '110',
'22': '108', '22': '108',
'21': '106', '21': '106',
'20': '104', '20': '104',
'19': '102', '19': '102',
'18': '100' '18': '100',
'17': '98',
'16': '96',
'15': '94',
'14': '93',
'13': '91',
'12': '89',
'11': '87'
} }
if (electronVer && parseInt(electronVer) > 10) { if (electronVer && parseInt(electronVer) > 10) {
let target = chromeVer[electronVer] let target = chromeVer[electronVer]
@ -131,34 +109,25 @@ export function getElectronChromeTarget(): string {
return '' return ''
} }
export function startElectron(root: string | undefined): ChildProcess { export function startElectron(root: string | undefined, logger: Logger): ChildProcessWithoutNullStreams {
ensureElectronEntryFile(root) ensureElectronEntryFile(root)
const electronPath = getElectronPath() const electronPath = getElectronPath()
const isDev = process.env.NODE_ENV_ELECTRON_VITE === 'development' const inspect = !!process.env.VSCODE_INSPECTOR_OPTIONS
const args: string[] = process.env.ELECTRON_CLI_ARGS ? JSON.parse(process.env.ELECTRON_CLI_ARGS) : [] const args: string[] = []
if (!!process.env.REMOTE_DEBUGGING_PORT && process.env.NODE_ENV_ELECTRON_VITE === 'development') {
if (!!process.env.REMOTE_DEBUGGING_PORT && isDev) {
args.push(`--remote-debugging-port=${process.env.REMOTE_DEBUGGING_PORT}`) args.push(`--remote-debugging-port=${process.env.REMOTE_DEBUGGING_PORT}`)
} }
if (!!process.env.V8_INSPECTOR_PORT && isDev) { const ps = spawn(electronPath, ['.'].concat(args))
args.push(`--inspect=${process.env.V8_INSPECTOR_PORT}`) ps.stdout.on('data', chunk => {
} !inspect && chunk.toString().trim() && logger.info(chunk.toString())
})
if (!!process.env.V8_INSPECTOR_BRK_PORT && isDev) { ps.stderr.on('data', chunk => {
args.push(`--inspect-brk=${process.env.V8_INSPECTOR_BRK_PORT}`) !inspect && chunk.toString().trim() && logger.error(chunk.toString())
} })
if (process.env.NO_SANDBOX === '1') {
args.push('--no-sandbox')
}
const entry = process.env.ELECTRON_ENTRY || '.'
const ps = spawn(electronPath, [entry].concat(args), { stdio: 'inherit' })
ps.on('close', process.exit) ps.on('close', process.exit)
return ps return ps

View File

@ -1,4 +1,4 @@
export { type LogLevel, createLogger, mergeConfig } from 'vite' export { type LogLevel, createLogger, splitVendorChunkPlugin, splitVendorChunk } from 'vite'
export * from './config' export * from './config'
export { createServer } from './server' export { createServer } from './server'
export { build } from './build' export { build } from './build'

View File

@ -42,7 +42,7 @@ function resolveAsset(id: string): AssetResolved | null {
return null return null
} }
const nodeAssetRE = /__VITE_NODE_ASSET__([\w$]+)__/g const nodeAssetRE = /__VITE_NODE_ASSET__([a-z\d]{8})__/g
const nodePublicAssetRE = /__VITE_NODE_PUBLIC_ASSET__([a-z\d]{8})__/g const nodePublicAssetRE = /__VITE_NODE_PUBLIC_ASSET__([a-z\d]{8})__/g
const wasmHelperId = '\0__electron-vite-wasm-helper' const wasmHelperId = '\0__electron-vite-wasm-helper'
@ -68,10 +68,6 @@ export default function assetPlugin(): Plugin {
name: 'vite:node-asset', name: 'vite:node-asset',
apply: 'build', apply: 'build',
enforce: 'pre', enforce: 'pre',
buildStart(): void {
publicAssetPathCache.clear()
assetCache.clear()
},
configResolved(config): void { configResolved(config): void {
sourcemap = config.build.sourcemap sourcemap = config.build.sourcemap
publicDir = normalizePath(config.publicDir) publicDir = normalizePath(config.publicDir)
@ -87,12 +83,6 @@ export default function assetPlugin(): Plugin {
return wasmHelperCode return wasmHelperCode
} }
if (id.startsWith('\0')) {
// Rollup convention, this id should be handled by the
// plugin that marked it with \0
return
}
const assetResolved = resolveAsset(id) const assetResolved = resolveAsset(id)
if (!assetResolved) { if (!assetResolved) {
return return
@ -115,7 +105,7 @@ export default function assetPlugin(): Plugin {
const hash = this.emitFile({ const hash = this.emitFile({
type: 'asset', type: 'asset',
name: path.basename(file), name: path.basename(file),
source: source as unknown as Uint8Array source
}) })
referenceId = `__VITE_NODE_ASSET__${hash}__` referenceId = `__VITE_NODE_ASSET__${hash}__`
assetCache.set(file, referenceId) assetCache.set(file, referenceId)
@ -179,7 +169,7 @@ export default function assetPlugin(): Plugin {
if (s) { if (s) {
return { return {
code: s.toString(), code: s.toString(),
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null map: sourcemap ? s.generateMap({ hires: true }) : null
} }
} else { } else {
return null return null

View File

@ -15,7 +15,7 @@ import { toRelativePath } from '../utils'
const _require = createRequire(import.meta.url) const _require = createRequire(import.meta.url)
function getBytecodeCompilerPath(): string { function getBytecodeCompilerPath(): string {
return path.join(path.dirname(_require.resolve('electron-vite/package.json')), 'bin', 'electron-bytecode.cjs') return path.join(path.dirname(_require.resolve('electron-vite/package.json')), 'bin', 'electron-bytecode.js')
} }
function compileToBytecode(code: string): Promise<Buffer> { function compileToBytecode(code: string): Promise<Buffer> {
@ -98,7 +98,7 @@ const bytecodeModuleLoaderCode = [
` ret |= buffer[0];`, ` ret |= buffer[0];`,
` return ret;`, ` return ret;`,
`};`, `};`,
`Module._extensions[".jsc"] = Module._extensions[".cjsc"] = function (module, filename) {`, `Module._extensions[".jsc"] = function (module, filename) {`,
` const bytecodeBuffer = fs.readFileSync(filename);`, ` const bytecodeBuffer = fs.readFileSync(filename);`,
` if (!Buffer.isBuffer(bytecodeBuffer)) {`, ` if (!Buffer.isBuffer(bytecodeBuffer)) {`,
` throw new Error("BytecodeBuffer must be a buffer object.");`, ` throw new Error("BytecodeBuffer must be a buffer object.");`,
@ -181,7 +181,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
} }
const useStrict = '"use strict";' const useStrict = '"use strict";'
const bytecodeModuleLoader = 'bytecode-loader.cjs' const bytecodeModuleLoader = 'bytecode-loader.js'
let config: ResolvedConfig let config: ResolvedConfig
let useInRenderer = false let useInRenderer = false
@ -196,14 +196,11 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
config = resolvedConfig config = resolvedConfig
useInRenderer = config.plugins.some(p => p.name === 'vite:electron-renderer-preset-config') useInRenderer = config.plugins.some(p => p.name === 'vite:electron-renderer-preset-config')
if (useInRenderer) { if (useInRenderer) {
config.logger.warn(colors.yellow('bytecodePlugin does not support renderer.')) config.logger.warn(colors.yellow('bytecodePlugin is not support renderers'))
}
if (resolvedConfig.build.minify && protectedStrings.length > 0) {
config.logger.warn(colors.yellow('Strings cannot be protected when minification is enabled.'))
} }
}, },
transform(code, id): void | { code: string; map: SourceMapInput } { transform(code, id): void | { code: string; map: SourceMapInput } {
if (config.build.minify || protectedStrings.length === 0 || !filter(id)) return if (protectedStrings.length === 0 || !filter(id)) return
let match: RegExpExecArray | null let match: RegExpExecArray | null
let s: MagicString | undefined let s: MagicString | undefined
@ -225,20 +222,11 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
if (s) { if (s) {
return { return {
code: s.toString(), code: s.toString(),
map: config.build.sourcemap ? s.generateMap({ hires: 'boundary' }) : null map: config.build.sourcemap ? s.generateMap({ hires: true }) : null
} }
} }
}, },
renderChunk(code, chunk, options): { code: string } | null { renderChunk(code, chunk): { code: string } | null {
if (options.format === 'es') {
config.logger.warn(
colors.yellow(
'bytecodePlugin does not support ES module, please remove "type": "module" ' +
'in package.json or set the "build.rollupOptions.output.format" option to "cjs".'
)
)
return null
}
if (useInRenderer) { if (useInRenderer) {
return null return null
} }
@ -252,8 +240,8 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
} }
return null return null
}, },
generateBundle(options): void { generateBundle(): void {
if (options.format !== 'es' && !useInRenderer && bytecodeRequired) { if (!useInRenderer && bytecodeRequired) {
this.emitFile({ this.emitFile({
type: 'asset', type: 'asset',
source: bytecodeModuleLoaderCode.join('\n') + '\n', source: bytecodeModuleLoaderCode.join('\n') + '\n',
@ -263,7 +251,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
} }
}, },
async writeBundle(options, output): Promise<void> { async writeBundle(options, output): Promise<void> {
if (options.format === 'es' || useInRenderer || !bytecodeRequired) { if (useInRenderer || !bytecodeRequired) {
return return
} }
@ -287,7 +275,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
} }
const getBytecodeLoaderBlock = (chunkFileName: string): string => { const getBytecodeLoaderBlock = (chunkFileName: string): string => {
return `require("${toRelativePath(bytecodeModuleLoader, normalizePath(chunkFileName))}");` return `require("${toRelativePath(bytecodeModuleLoader, chunkFileName)}");`
} }
await Promise.all( await Promise.all(
@ -310,7 +298,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
const chunkFileName = path.resolve(outDir, name) const chunkFileName = path.resolve(outDir, name)
if (bytecodeChunks.includes(name)) { if (bytecodeChunks.includes(name)) {
const bytecodeBuffer = await compileToBytecode(_code) const bytecodeBuffer = await compileToBytecode(_code)
fs.writeFileSync(path.resolve(outDir, name + 'c'), bytecodeBuffer as unknown as Uint8Array) fs.writeFileSync(path.resolve(outDir, name + 'c'), bytecodeBuffer)
if (chunk.isEntry) { if (chunk.isEntry) {
if (!removeBundleJS) { if (!removeBundleJS) {
keepBundle(chunkFileName) keepBundle(chunkFileName)
@ -344,9 +332,7 @@ export function bytecodePlugin(options: BytecodeOptions = {}): Plugin | null {
} }
} }
const bytecodeLoaderBlock = getBytecodeLoaderBlock(chunk.fileName) const bytecodeLoaderBlock = getBytecodeLoaderBlock(chunk.fileName)
_code = hasBytecodeMoudle _code = hasBytecodeMoudle ? _code.replace(useStrict, `${useStrict}\n${bytecodeLoaderBlock}`) : _code
? _code.replace(/("use strict";)|('use strict';)/, `${useStrict}\n${bytecodeLoaderBlock}`)
: _code
} }
fs.writeFileSync(chunkFileName, _code) fs.writeFileSync(chunkFileName, _code)
} }

View File

@ -2,16 +2,14 @@ import path from 'node:path'
import fs from 'node:fs' import fs from 'node:fs'
import { builtinModules } from 'node:module' import { builtinModules } from 'node:module'
import colors from 'picocolors' import colors from 'picocolors'
import { type Plugin, type LibraryOptions, mergeConfig, normalizePath } from 'vite' import { type Plugin, mergeConfig, normalizePath } from 'vite'
import type { OutputOptions } from 'rollup' import { getElectronNodeTarget, getElectronChromeTarget } from '../electron'
import { getElectronNodeTarget, getElectronChromeTarget, supportESM } from '../electron'
import { loadPackageData } from '../utils'
export interface ElectronPluginOptions { export interface ElectronPluginOptions {
root?: string root?: string
} }
function findLibEntry(root: string, scope: string): string | undefined { function findLibEntry(root: string, scope: string): string {
for (const name of ['index', scope]) { for (const name of ['index', scope]) {
for (const ext of ['js', 'ts', 'mjs', 'cjs']) { for (const ext of ['js', 'ts', 'mjs', 'cjs']) {
const entryFile = path.resolve(root, 'src', scope, `${name}.${ext}`) const entryFile = path.resolve(root, 'src', scope, `${name}.${ext}`)
@ -20,7 +18,7 @@ function findLibEntry(root: string, scope: string): string | undefined {
} }
} }
} }
return undefined return ''
} }
function findInput(root: string, scope = 'renderer'): string { function findInput(root: string, scope = 'renderer'): string {
@ -31,25 +29,6 @@ function findInput(root: string, scope = 'renderer'): string {
return '' return ''
} }
function processEnvDefine(): Record<string, string> {
return {
'process.env': `process.env`,
'global.process.env': `global.process.env`,
'globalThis.process.env': `globalThis.process.env`
}
}
function resolveBuildOutputs(
outputs: OutputOptions | OutputOptions[] | undefined,
libOptions: LibraryOptions | false
): OutputOptions | OutputOptions[] | undefined {
if (libOptions && !Array.isArray(outputs)) {
const libFormats = libOptions.formats || []
return libFormats.map(format => ({ ...outputs, format }))
}
return outputs
}
export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[] { export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[] {
return [ return [
{ {
@ -61,23 +40,16 @@ export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[
const nodeTarget = getElectronNodeTarget() const nodeTarget = getElectronNodeTarget()
const pkg = loadPackageData() || { type: 'commonjs' }
const format = pkg.type && pkg.type === 'module' && supportESM() ? 'es' : 'cjs'
const defaultConfig = { const defaultConfig = {
resolve: {
browserField: false,
mainFields: ['module', 'jsnext:main', 'jsnext'],
conditions: ['node']
},
build: { build: {
outDir: path.resolve(root, 'out', 'main'), outDir: path.resolve(root, 'out', 'main'),
target: nodeTarget, target: nodeTarget,
assetsDir: 'chunks', assetsDir: 'chunks',
rollupOptions: { rollupOptions: {
external: ['electron', /^electron\/.+/, ...builtinModules.flatMap(m => [m, `node:${m}`])], external: ['electron', ...builtinModules.flatMap(m => [m, `node:${m}`])],
output: {} output: {
entryFileNames: '[name].js'
}
}, },
reportCompressedSize: false, reportCompressedSize: false,
minify: false minify: false
@ -87,21 +59,12 @@ export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[
const build = config.build || {} const build = config.build || {}
const rollupOptions = build.rollupOptions || {} const rollupOptions = build.rollupOptions || {}
if (!rollupOptions.input) { if (!rollupOptions.input) {
const libOptions = build.lib
const outputOptions = rollupOptions.output
defaultConfig.build['lib'] = { defaultConfig.build['lib'] = {
entry: findLibEntry(root, 'main'), entry: findLibEntry(root, 'main'),
formats: formats: ['cjs']
libOptions && libOptions.formats && libOptions.formats.length > 0
? []
: [
outputOptions && !Array.isArray(outputOptions) && outputOptions.format
? outputOptions.format
: format
]
} }
} else { } else {
defaultConfig.build.rollupOptions.output['format'] = format defaultConfig.build.rollupOptions.output['format'] = 'cjs'
} }
defaultConfig.build.rollupOptions.output['assetFileNames'] = path.posix.join( defaultConfig.build.rollupOptions.output['assetFileNames'] = path.posix.join(
@ -112,22 +75,13 @@ export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[
const buildConfig = mergeConfig(defaultConfig.build, build) const buildConfig = mergeConfig(defaultConfig.build, build)
config.build = buildConfig config.build = buildConfig
config.resolve = mergeConfig(defaultConfig.resolve, config.resolve || {}) config.envPrefix = config.envPrefix || 'MAIN_VITE_'
config.define = config.define || {}
config.define = { ...processEnvDefine(), ...config.define }
config.envPrefix = config.envPrefix || ['MAIN_VITE_', 'VITE_']
config.publicDir = config.publicDir || 'resources' config.publicDir = config.publicDir || 'resources'
// do not copy public dir // do not copy public dir
config.build.copyPublicDir = false config.build.copyPublicDir = false
// module preload polyfill does not apply to nodejs (main process) // module preload polyfill does not apply to nodejs (main process)
config.build.modulePreload = false config.build.modulePreload = false
// enable ssr build
config.build.ssr = true
config.build.ssrEmitAssets = true
config.ssr = { ...config.ssr, ...{ noExternal: true } }
} }
}, },
{ {
@ -137,45 +91,37 @@ export function electronMainVitePlugin(options?: ElectronPluginOptions): Plugin[
configResolved(config): void { configResolved(config): void {
const build = config.build const build = config.build
if (!build.target) { if (!build.target) {
throw new Error('build.target option is required in the electron vite main config.') throw new Error('build target required for the electron vite main config')
} else { } else {
const targets = Array.isArray(build.target) ? build.target : [build.target] const targets = Array.isArray(build.target) ? build.target : [build.target]
if (targets.some(t => !t.startsWith('node'))) { if (targets.some(t => !t.startsWith('node'))) {
throw new Error('The electron vite main config build.target option must be "node?".') throw new Error('the electron vite main config build target must be node')
} }
} }
const libOptions = build.lib const lib = build.lib
const rollupOptions = build.rollupOptions if (!lib) {
const rollupOptions = build.rollupOptions
if (!(libOptions && libOptions.entry) && !rollupOptions?.input) { if (!rollupOptions?.input) {
throw new Error( throw new Error('build lib field required for the electron vite main config')
'An entry point is required in the electron vite main config, ' +
'which can be specified using "build.lib.entry" or "build.rollupOptions.input".'
)
}
const resolvedOutputs = resolveBuildOutputs(rollupOptions.output, libOptions)
if (resolvedOutputs) {
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
if (outputs.length > 1) {
throw new Error('The electron vite main config does not support multiple outputs.')
} else { } else {
const outpout = outputs[0] const output = rollupOptions?.output
if (['es', 'cjs'].includes(outpout.format || '')) { if (output) {
if (outpout.format === 'es' && !supportESM()) { const formats = Array.isArray(output) ? output : [output]
throw new Error( if (formats.some(f => f.format !== 'cjs')) {
'The electron vite main config output format does not support "es", ' + throw new Error('the electron vite main config output format must be cjs')
'you can upgrade electron to the latest version or switch to "cjs" format.'
)
} }
} else {
throw new Error(
`The electron vite main config output format must be "cjs"${supportESM() ? ' or "es"' : ''}.`
)
} }
} }
} else {
if (!lib.entry) {
throw new Error('build entry field required for the electron vite main config')
}
if (!lib.formats) {
throw new Error('build format field required for the electron vite main config')
} else if (!lib.formats.includes('cjs')) {
throw new Error('the electron vite main config build lib format must be cjs')
}
} }
} }
} }
@ -193,24 +139,16 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug
const nodeTarget = getElectronNodeTarget() const nodeTarget = getElectronNodeTarget()
const pkg = loadPackageData() || { type: 'commonjs' }
const format = pkg.type && pkg.type === 'module' && supportESM() ? 'es' : 'cjs'
const defaultConfig = { const defaultConfig = {
ssr: {
resolve: {
conditions: ['module', 'browser', 'development|production'],
mainFields: ['browser', 'module', 'jsnext:main', 'jsnext']
}
},
build: { build: {
outDir: path.resolve(root, 'out', 'preload'), outDir: path.resolve(root, 'out', 'preload'),
target: nodeTarget, target: nodeTarget,
assetsDir: 'chunks', assetsDir: 'chunks',
rollupOptions: { rollupOptions: {
external: ['electron', /^electron\/.+/, ...builtinModules.flatMap(m => [m, `node:${m}`])], external: ['electron', ...builtinModules.flatMap(m => [m, `node:${m}`])],
output: {} output: {
entryFileNames: '[name].js'
}
}, },
reportCompressedSize: false, reportCompressedSize: false,
minify: false minify: false
@ -220,21 +158,12 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug
const build = config.build || {} const build = config.build || {}
const rollupOptions = build.rollupOptions || {} const rollupOptions = build.rollupOptions || {}
if (!rollupOptions.input) { if (!rollupOptions.input) {
const libOptions = build.lib
const outputOptions = rollupOptions.output
defaultConfig.build['lib'] = { defaultConfig.build['lib'] = {
entry: findLibEntry(root, 'preload'), entry: findLibEntry(root, 'preload'),
formats: formats: ['cjs']
libOptions && libOptions.formats && libOptions.formats.length > 0
? []
: [
outputOptions && !Array.isArray(outputOptions) && outputOptions.format
? outputOptions.format
: format
]
} }
} else { } else {
defaultConfig.build.rollupOptions.output['format'] = format defaultConfig.build.rollupOptions.output['format'] = 'cjs'
} }
defaultConfig.build.rollupOptions.output['assetFileNames'] = path.posix.join( defaultConfig.build.rollupOptions.output['assetFileNames'] = path.posix.join(
@ -245,41 +174,13 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug
const buildConfig = mergeConfig(defaultConfig.build, build) const buildConfig = mergeConfig(defaultConfig.build, build)
config.build = buildConfig config.build = buildConfig
const resolvedOutputs = resolveBuildOutputs(config.build.rollupOptions!.output, config.build.lib || false) config.envPrefix = config.envPrefix || 'PRELOAD_VITE_'
if (resolvedOutputs) {
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
if (outputs.find(({ format }) => format === 'es')) {
if (Array.isArray(config.build.rollupOptions!.output)) {
config.build.rollupOptions!.output.forEach(output => {
if (output.format === 'es') {
output['entryFileNames'] = '[name].mjs'
output['chunkFileNames'] = '[name]-[hash].mjs'
}
})
} else {
config.build.rollupOptions!.output!['entryFileNames'] = '[name].mjs'
config.build.rollupOptions!.output!['chunkFileNames'] = '[name]-[hash].mjs'
}
}
}
config.define = config.define || {}
config.define = { ...processEnvDefine(), ...config.define }
config.envPrefix = config.envPrefix || ['PRELOAD_VITE_', 'VITE_']
config.publicDir = config.publicDir || 'resources' config.publicDir = config.publicDir || 'resources'
// do not copy public dir // do not copy public dir
config.build.copyPublicDir = false config.build.copyPublicDir = false
// module preload polyfill does not apply to nodejs (preload scripts) // module preload polyfill does not apply to nodejs (preload scripts)
config.build.modulePreload = false config.build.modulePreload = false
// enable ssr build
config.build.ssr = true
config.build.ssrEmitAssets = true
config.ssr = mergeConfig(defaultConfig.ssr, config.ssr || {})
config.ssr.noExternal = true
} }
}, },
{ {
@ -289,45 +190,37 @@ export function electronPreloadVitePlugin(options?: ElectronPluginOptions): Plug
configResolved(config): void { configResolved(config): void {
const build = config.build const build = config.build
if (!build.target) { if (!build.target) {
throw new Error('build.target option is required in the electron vite preload config.') throw new Error('build target required for the electron vite preload config')
} else { } else {
const targets = Array.isArray(build.target) ? build.target : [build.target] const targets = Array.isArray(build.target) ? build.target : [build.target]
if (targets.some(t => !t.startsWith('node'))) { if (targets.some(t => !t.startsWith('node'))) {
throw new Error('The electron vite preload config build.target must be "node?".') throw new Error('the electron vite preload config build target must be node')
} }
} }
const libOptions = build.lib const lib = build.lib
const rollupOptions = build.rollupOptions if (!lib) {
const rollupOptions = build.rollupOptions
if (!(libOptions && libOptions.entry) && !rollupOptions?.input) { if (!rollupOptions?.input) {
throw new Error( throw new Error('build lib field required for the electron vite preload config')
'An entry point is required in the electron vite preload config, ' +
'which can be specified using "build.lib.entry" or "build.rollupOptions.input".'
)
}
const resolvedOutputs = resolveBuildOutputs(rollupOptions.output, libOptions)
if (resolvedOutputs) {
const outputs = Array.isArray(resolvedOutputs) ? resolvedOutputs : [resolvedOutputs]
if (outputs.length > 1) {
throw new Error('The electron vite preload config does not support multiple outputs.')
} else { } else {
const outpout = outputs[0] const output = rollupOptions?.output
if (['es', 'cjs'].includes(outpout.format || '')) { if (output) {
if (outpout.format === 'es' && !supportESM()) { const formats = Array.isArray(output) ? output : [output]
throw new Error( if (formats.some(f => f.format !== 'cjs')) {
'The electron vite preload config output format does not support "es", ' + throw new Error('the electron vite preload config output format must be cjs')
'you can upgrade electron to the latest version or switch to "cjs" format.'
)
} }
} else {
throw new Error(
`The electron vite preload config output format must be "cjs"${supportESM() ? ' or "es"' : ''}.`
)
} }
} }
} else {
if (!lib.entry) {
throw new Error('build entry field required for the electron vite preload config')
}
if (!lib.formats) {
throw new Error('build format field required for the electron vite preload config')
} else if (!lib.formats.includes('cjs')) {
throw new Error('the electron vite preload config lib format must be cjs')
}
} }
} }
} }
@ -383,7 +276,7 @@ export function electronRendererVitePlugin(options?: ElectronPluginOptions): Plu
config.envDir = config.envDir || path.resolve(root) config.envDir = config.envDir || path.resolve(root)
config.envPrefix = config.envPrefix || ['RENDERER_VITE_', 'VITE_'] config.envPrefix = config.envPrefix || 'RENDERER_VITE_'
} }
}, },
{ {
@ -391,25 +284,23 @@ export function electronRendererVitePlugin(options?: ElectronPluginOptions): Plu
enforce: 'post', enforce: 'post',
configResolved(config): void { configResolved(config): void {
if (config.base !== './' && config.base !== '/') { if (config.base !== './' && config.base !== '/') {
config.logger.warn(colors.yellow('(!) Should not set "base" option for the electron vite renderer config.')) config.logger.warn(colors.yellow('should not set base field for the electron vite renderer config'))
} }
const build = config.build const build = config.build
if (!build.target) { if (!build.target) {
throw new Error('build.target option is required in the electron vite renderer config.') throw new Error('build target required for the electron vite renderer config')
} else { } else {
const targets = Array.isArray(build.target) ? build.target : [build.target] const targets = Array.isArray(build.target) ? build.target : [build.target]
if (targets.some(t => !t.startsWith('chrome') && !/^es((202\d{1})|next)$/.test(t))) { if (targets.some(t => !t.startsWith('chrome'))) {
config.logger.warn( throw new Error('the electron vite renderer config build target must be chrome')
'The electron vite renderer config build.target is not "chrome?" or "es?". This could be a mistake.'
)
} }
} }
const rollupOptions = build.rollupOptions const rollupOptions = build.rollupOptions
if (!rollupOptions.input) { if (!rollupOptions.input) {
config.logger.warn(colors.yellow(`index.html file is not found in ${colors.dim('/src/renderer')} directory.`)) config.logger.warn(colors.yellow(`index.html file is not found in ${colors.dim('/src/renderer')} directory`))
throw new Error('build.rollupOptions.input option is required in the electron vite renderer config.') throw new Error('build rollupOptions input field required for the electron vite renderer config')
} }
} }
} }

View File

@ -1,79 +0,0 @@
/*
* The core of this plugin was conceived by pi0 and is taken from the following repository:
* https://github.com/unjs/unbuild/blob/main/src/builder/plugins/cjs.ts
* license: https://github.com/unjs/unbuild/blob/main/LICENSE
*/
import MagicString from 'magic-string'
import type { SourceMapInput } from 'rollup'
import type { Plugin } from 'vite'
import { getElectronMajorVersion } from '../electron'
const CJSyntaxRe = /__filename|__dirname|require\(|require\.resolve\(/
const CJSShim_normal = `
// -- CommonJS Shims --
import __cjs_url__ from 'node:url';
import __cjs_path__ from 'node:path';
import __cjs_mod__ from 'node:module';
const __filename = __cjs_url__.fileURLToPath(import.meta.url);
const __dirname = __cjs_path__.dirname(__filename);
const require = __cjs_mod__.createRequire(import.meta.url);
`
const CJSShim_node_20_11 = `
// -- CommonJS Shims --
import __cjs_mod__ from 'node:module';
const __filename = import.meta.filename;
const __dirname = import.meta.dirname;
const require = __cjs_mod__.createRequire(import.meta.url);
`
const ESMStaticImportRe =
/(?<=\s|^|;)import\s*([\s"']*(?<imports>[\p{L}\p{M}\w\t\n\r $*,/{}@.]+)from\s*)?["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gmu
interface StaticImport {
end: number
}
function findStaticImports(code: string): StaticImport[] {
const matches: StaticImport[] = []
for (const match of code.matchAll(ESMStaticImportRe)) {
matches.push({ end: (match.index || 0) + match[0].length })
}
return matches
}
export default function esmShimPlugin(): Plugin {
let sourcemap: boolean | 'inline' | 'hidden' = false
const CJSShim = getElectronMajorVersion() >= 30 ? CJSShim_node_20_11 : CJSShim_normal
return {
name: 'vite:esm-shim',
apply: 'build',
enforce: 'post',
configResolved(config): void {
sourcemap = config.build.sourcemap
},
renderChunk(code, _chunk, options): { code: string; map?: SourceMapInput } | null {
if (options.format === 'es') {
if (code.includes(CJSShim) || !CJSyntaxRe.test(code)) {
return null
}
const lastESMImport = findStaticImports(code).pop()
const indexToAppend = lastESMImport ? lastESMImport.end : 0
const s = new MagicString(code)
s.appendRight(indexToAppend, CJSShim)
return {
code: s.toString(),
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
}
}
return null
}
}
}

View File

@ -1,5 +1,6 @@
import path from 'node:path'
import { createRequire } from 'node:module'
import { type Plugin, mergeConfig } from 'vite' import { type Plugin, mergeConfig } from 'vite'
import { loadPackageData } from '../utils'
export interface ExternalOptions { export interface ExternalOptions {
exclude?: string[] exclude?: string[]
@ -12,19 +13,19 @@ export interface ExternalOptions {
export function externalizeDepsPlugin(options: ExternalOptions = {}): Plugin | null { export function externalizeDepsPlugin(options: ExternalOptions = {}): Plugin | null {
const { exclude = [], include = [] } = options const { exclude = [], include = [] } = options
const pkg = loadPackageData() || {} const packagePath = path.resolve(process.cwd(), 'package.json')
const require = createRequire(import.meta.url)
const pkg = require(packagePath)
let deps = Object.keys(pkg.dependencies || {}) let deps = Object.keys(pkg.dependencies || {})
if (include.length) { if (include.length) {
deps = deps.concat(include.filter(dep => dep.trim() !== '')) deps = deps.concat(include)
} }
if (exclude.length) { if (exclude.length) {
deps = deps.filter(dep => !exclude.includes(dep)) deps = deps.filter(dep => !exclude.includes(dep))
} }
deps = [...new Set(deps)]
return { return {
name: 'vite:externalize-deps', name: 'vite:externalize-deps',
enforce: 'pre', enforce: 'pre',
@ -32,7 +33,7 @@ export function externalizeDepsPlugin(options: ExternalOptions = {}): Plugin | n
const defaultConfig = { const defaultConfig = {
build: { build: {
rollupOptions: { rollupOptions: {
external: deps.length > 0 ? [...deps, new RegExp(`^(${deps.join('|')})/.+`)] : [] external: [...new Set(deps)]
} }
} }
} }

View File

@ -1,21 +0,0 @@
import type { Plugin } from 'vite'
export default function importMetaPlugin(): Plugin {
return {
name: 'vite:import-meta',
apply: 'build',
enforce: 'pre',
resolveImportMeta(property, { format }): string | null {
if (property === 'url' && format === 'cjs') {
return `require("url").pathToFileURL(__filename).href`
}
if (property === 'filename' && format === 'cjs') {
return `__filename`
}
if (property === 'dirname' && format === 'cjs') {
return `__dirname`
}
return null
}
}
}

View File

@ -1,65 +0,0 @@
import type { Plugin } from 'vite'
import type { SourceMapInput } from 'rollup'
import MagicString from 'magic-string'
import { cleanUrl, parseRequest, toRelativePath } from '../utils'
const modulePathRE = /__VITE_MODULE_PATH__([\w$]+)__/g
/**
* Resolve `?modulePath` import and return the module bundle path.
*/
export default function modulePathPlugin(): Plugin {
let sourcemap: boolean | 'inline' | 'hidden' = false
return {
name: 'vite:module-path',
apply: 'build',
enforce: 'pre',
configResolved(config): void {
sourcemap = config.build.sourcemap
},
resolveId(id, importer): string | void {
const query = parseRequest(id)
if (query && typeof query.modulePath === 'string') {
return id + `&importer=${importer}`
}
},
load(id): string | void {
const query = parseRequest(id)
if (query && typeof query.modulePath === 'string' && typeof query.importer === 'string') {
const cleanPath = cleanUrl(id)
const hash = this.emitFile({
type: 'chunk',
id: cleanPath,
importer: query.importer
})
const refId = `__VITE_MODULE_PATH__${hash}__`
return `
import { join } from 'path'
export default join(__dirname, ${refId})`
}
},
renderChunk(code, chunk): { code: string; map: SourceMapInput } | null {
if (code.match(modulePathRE)) {
let match: RegExpExecArray | null
const s = new MagicString(code)
while ((match = modulePathRE.exec(code))) {
const [full, hash] = match
const filename = this.getFileName(hash)
const outputFilepath = toRelativePath(filename, chunk.fileName)
const replacement = JSON.stringify(outputFilepath)
s.overwrite(match.index, match.index + full.length, replacement, {
contentOnly: true
})
}
return {
code: s.toString(),
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null
}
}
return null
}
}
}

View File

@ -3,7 +3,7 @@ import type { SourceMapInput } from 'rollup'
import MagicString from 'magic-string' import MagicString from 'magic-string'
import { cleanUrl, parseRequest, toRelativePath } from '../utils' import { cleanUrl, parseRequest, toRelativePath } from '../utils'
const nodeWorkerAssetUrlRE = /__VITE_NODE_WORKER_ASSET__([\w$]+)__/g const nodeWorkerAssetUrlRE = /__VITE_NODE_WORKER_ASSET__([a-z\d]{8})__/g
/** /**
* Resolve `?nodeWorker` import and automatically generate `Worker` wrapper. * Resolve `?nodeWorker` import and automatically generate `Worker` wrapper.
@ -35,7 +35,7 @@ export default function workerPlugin(): Plugin {
const assetRefId = `__VITE_NODE_WORKER_ASSET__${hash}__` const assetRefId = `__VITE_NODE_WORKER_ASSET__${hash}__`
return ` return `
import { Worker } from 'node:worker_threads'; import { Worker } from 'node:worker_threads';
export default function (options) { return new Worker(new URL(${assetRefId}, import.meta.url), options); }` export default function (options) { return new Worker(require.resolve(${assetRefId}), options); }`
} }
}, },
renderChunk(code, chunk): { code: string; map: SourceMapInput } | null { renderChunk(code, chunk): { code: string; map: SourceMapInput } | null {
@ -55,7 +55,7 @@ export default function workerPlugin(): Plugin {
return { return {
code: s.toString(), code: s.toString(),
map: sourcemap ? s.generateMap({ hires: 'boundary' }) : null map: sourcemap ? s.generateMap({ hires: true }) : null
} }
} }

View File

@ -11,7 +11,7 @@ export async function preview(inlineConfig: InlineConfig = {}, options: { skipBu
const logger = createLogger(inlineConfig.logLevel) const logger = createLogger(inlineConfig.logLevel)
startElectron(inlineConfig.root) startElectron(inlineConfig.root, logger)
logger.info(colors.green(`\nstart electron app...\n`)) logger.info(colors.green(`\nstart electron app...\n`))
} }

View File

@ -1,8 +1,8 @@
import type { ChildProcess } from 'node:child_process' import type { ChildProcessWithoutNullStreams } from 'node:child_process'
import { import {
type UserConfig as ViteConfig, type UserConfig as ViteConfig,
type ViteDevServer, type ViteDevServer,
createServer as viteCreateServer, createServer as ViteCreateServer,
build as viteBuild, build as viteBuild,
createLogger, createLogger,
mergeConfig mergeConfig
@ -22,11 +22,7 @@ export async function createServer(
const logger = createLogger(inlineConfig.logLevel) const logger = createLogger(inlineConfig.logLevel)
let server: ViteDevServer | undefined let server: ViteDevServer | undefined
let ps: ChildProcess | undefined let ps: ChildProcessWithoutNullStreams | undefined
const errorHook = (e): void => {
logger.error(`${colors.bgRed(colors.white(' ERROR '))} ${colors.red(e.message)}`)
}
const mainViteConfig = config.config?.main const mainViteConfig = config.config?.main
if (mainViteConfig && !options.rendererOnly) { if (mainViteConfig && !options.rendererOnly) {
@ -38,13 +34,13 @@ export async function createServer(
ps.removeAllListeners() ps.removeAllListeners()
ps.kill() ps.kill()
ps = startElectron(inlineConfig.root) ps = startElectron(inlineConfig.root, logger)
logger.info(colors.green(`\nrestart electron app...`)) logger.info(colors.green(`\nrestart electron app...`))
} }
} }
await doBuild(mainViteConfig, watchHook, errorHook) await doBuild(mainViteConfig, watchHook)
logger.info(colors.green(`\nbuild the electron main process successfully`)) logger.info(colors.green(`\nbuild the electron main process successfully`))
} }
@ -63,7 +59,7 @@ export async function createServer(
} }
} }
await doBuild(preloadViteConfig, watchHook, errorHook) await doBuild(preloadViteConfig, watchHook)
logger.info(colors.green(`\nbuild the electron preload files successfully`)) logger.info(colors.green(`\nbuild the electron preload files successfully`))
} }
@ -80,7 +76,7 @@ export async function createServer(
if (rendererViteConfig) { if (rendererViteConfig) {
logger.info(colors.gray(`\n-----\n`)) logger.info(colors.gray(`\n-----\n`))
server = await viteCreateServer(rendererViteConfig) server = await ViteCreateServer(rendererViteConfig)
if (!server.httpServer) { if (!server.httpServer) {
throw new Error('HTTP server not available') throw new Error('HTTP server not available')
@ -104,7 +100,7 @@ export async function createServer(
server.printUrls() server.printUrls()
} }
ps = startElectron(inlineConfig.root) ps = startElectron(inlineConfig.root, logger)
logger.info(colors.green(`\nstart electron app...\n`)) logger.info(colors.green(`\nstart electron app...\n`))
} }
@ -112,7 +108,7 @@ export async function createServer(
type UserConfig = ViteConfig & { configFile?: string | false } type UserConfig = ViteConfig & { configFile?: string | false }
async function doBuild(config: UserConfig, watchHook: () => void, errorHook: (e: Error) => void): Promise<void> { async function doBuild(config: UserConfig, watchHook: () => void): Promise<void> {
return new Promise(resolve => { return new Promise(resolve => {
if (config.build?.watch) { if (config.build?.watch) {
let firstBundle = true let firstBundle = true
@ -135,12 +131,10 @@ async function doBuild(config: UserConfig, watchHook: () => void, errorHook: (e:
}) })
} }
viteBuild(config) viteBuild(config).then(() => {
.then(() => { if (!config.build?.watch) {
if (!config.build?.watch) { resolve()
resolve() }
} })
})
.catch(e => errorHook(e))
}) })
} }

View File

@ -1,14 +1,14 @@
import { URL, URLSearchParams } from 'node:url' import { URL, URLSearchParams } from 'node:url'
import path from 'node:path' import path from 'node:path'
import fs from 'node:fs'
import { createHash } from 'node:crypto' import { createHash } from 'node:crypto'
import { createRequire } from 'node:module'
import { loadEnv as viteLoadEnv } from 'vite' import { loadEnv as viteLoadEnv } from 'vite'
export function isObject(value: unknown): value is Record<string, unknown> { export function isObject(value: unknown): value is Record<string, unknown> {
return Object.prototype.toString.call(value) === '[object Object]' return Object.prototype.toString.call(value) === '[object Object]'
} }
export const dynamicImport = new Function('file', 'return import(file)')
export const wildcardHosts = new Set(['0.0.0.0', '::', '0000:0000:0000:0000:0000:0000:0000:0000']) export const wildcardHosts = new Set(['0.0.0.0', '::', '0000:0000:0000:0000:0000:0000:0000:0000'])
export function resolveHostname(optionsHost: string | boolean | undefined): string { export function resolveHostname(optionsHost: string | boolean | undefined): string {
@ -29,10 +29,7 @@ export function parseRequest(id: string): Record<string, string> | null {
} }
export function getHash(text: Buffer | string): string { export function getHash(text: Buffer | string): string {
return createHash('sha256') return createHash('sha256').update(text).digest('hex').substring(0, 8)
.update(text as unknown as Uint8Array)
.digest('hex')
.substring(0, 8)
} }
export function toRelativePath(filename: string, importer: string): string { export function toRelativePath(filename: string, importer: string): string {
@ -41,49 +38,14 @@ export function toRelativePath(filename: string, importer: string): string {
} }
/** /**
* Load `.env` files within the `envDir` (default: `process.cwd()`) . * Load `.env` files within the `envDir`(default: `process.cwd()`).
* By default, only env variables prefixed with `VITE_`, `MAIN_VITE_`, `PRELOAD_VITE_` and * By default, only env variables prefixed with `MAIN_VITE_`, `PRELOAD_VITE_` and
* `RENDERER_VITE_` are loaded, unless `prefixes` is changed. * `RENDERER_VITE_` are loaded, unless `prefixes` is changed.
*/ */
export function loadEnv( export function loadEnv(
mode: string, mode: string,
envDir: string = process.cwd(), envDir: string = process.cwd(),
prefixes: string | string[] = ['VITE_', 'MAIN_VITE_', 'PRELOAD_VITE_', 'RENDERER_VITE_'] prefixes: string | string[] = ['MAIN_VITE_', 'PRELOAD_VITE_', 'RENDERER_VITE_']
): Record<string, string> { ): Record<string, string> {
return viteLoadEnv(mode, envDir, prefixes) return viteLoadEnv(mode, envDir, prefixes)
} }
interface PackageData {
main?: string
type?: 'module' | 'commonjs'
dependencies?: Record<string, string>
}
let packageCached: PackageData | null = null
export function loadPackageData(root = process.cwd()): PackageData | null {
if (packageCached) return packageCached
const pkg = path.join(root, 'package.json')
if (fs.existsSync(pkg)) {
const _require = createRequire(import.meta.url)
const data = _require(pkg)
packageCached = {
main: data.main,
type: data.type,
dependencies: data.dependencies
}
return packageCached
}
return null
}
export function isFilePathESM(filePath: string): boolean {
if (/\.m[jt]s$/.test(filePath) || filePath.endsWith('.ts')) {
return true
} else if (/\.c[jt]s$/.test(filePath)) {
return false
} else {
const pkg = loadPackageData()
return pkg?.type === 'module'
}
}

View File

@ -1,20 +1,23 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES2023", "target": "es2019",
"module": "ESNext", "module": "esnext",
"lib": ["ESNext"], "lib": ["esnext"],
"sourceMap": false, "sourceMap": false,
"strict": true, "strict": true,
"allowJs": true, "allowJs": true,
"esModuleInterop": true, "esModuleInterop": true,
"moduleResolution": "bundler", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"skipLibCheck": true, "skipLibCheck": true,
"noUnusedLocals": true, "noUnusedLocals": true,
"noUnusedParameters": true, "noUnusedParameters": true,
"noImplicitAny": false, "noImplicitAny": false,
"noImplicitReturns": true "noImplicitReturns": true,
"declaration": true,
"declarationDir": "dist/types",
"outDir": "dist"
}, },
"include": ["src", "rollup.config.ts"] "include": ["src"]
} }