Skip to content

Commit

Permalink
feat: add copyModule to copy modules and support `string | string[]…
Browse files Browse the repository at this point in the history
…` in external option
  • Loading branch information
subframe7536 committed Sep 18, 2024
1 parent 3b54f53 commit 1df8437
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 22 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,16 @@ export interface BuildEntryOption {
* { db: './electron/native/db.ts' }
*/
nativeModuleEntryMap?: Record<string, string>
/**
* Skip process dynamic require
*
* Useful for `better-sqlite3` and other old packages
*/
ignoreDynamicRequires?: boolean
/**
* `external` option in `build.rollupOptions`, external `.node` by default
*/
external?: string | string[] | ((source: string, importer: string | undefined, isResolved: boolean) => boolean | null | undefined | void)
/**
* Custom options for `vite` build
* ```ts
Expand Down Expand Up @@ -881,6 +891,20 @@ export interface BuildEntryOption {
*/
skipIfExist?: boolean
}) => void
/**
* Copy specified modules to entry output dir, just like `external` option in rollup
*/
copyModules: (options: {
/**
* External Modules
*/
modules: string[]
/**
* Skip copy if `to` exist
* @default true
*/
skipIfExist?: boolean
}) => void
}) => Promisable<void>
}
Expand Down
27 changes: 18 additions & 9 deletions src/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import path from 'node:path'
import fs from 'node:fs'
import { getPackageInfoSync, loadPackageJSON } from 'local-pkg'
import type { BuildOptions, InlineConfig, Plugin, PluginOption } from 'vite'
import { mergeConfig, normalizePath } from 'vite'
import ElectronSimple from 'vite-plugin-electron/simple'
import { startup } from 'vite-plugin-electron'
import type { ElectronSimpleOptions } from 'vite-plugin-electron/simple'
import { notBundle } from 'vite-plugin-electron/plugin'
import { loadPackageJSON } from 'local-pkg'
import { isCI } from 'ci-info'
import { buildAsar, buildEntry, buildVersion } from './build'
import type { ElectronUpdaterOptions, PKG } from './option'
import { parseOptions } from './option'
import { id, log } from './constant'
import type { BytecodeOptions } from './bytecode'
import { copyAndSkipIfExist } from './utils'

export { isCI } from 'ci-info'
export { getPackageInfo, getPackageInfoSync, loadPackageJSON, resolveModule } from 'local-pkg'

type MakeRequired<T, K extends keyof T> = Exclude<T, undefined> & { [P in K]-?: T[P] }
type ReplaceKey<
Expand Down Expand Up @@ -293,15 +295,22 @@ export async function electronWithUpdater(
return path.join(entryOutputDirPath, ...paths)
},
copyToEntryOutputDir({ from, to, skipIfExist = true }) {
if (fs.existsSync(from)) {
const target = path.join(entryOutputDirPath, to ?? path.basename(from))
if (!skipIfExist || !fs.existsSync(target)) {
try {
fs.cpSync(from, target)
} catch (error) {
log.warn(`Copy failed: ${error}`, { timestamp: true })
}
if (!fs.existsSync(from)) {
log.warn(`${from} not found`, { timestamp: true })
return
}
const target = path.join(entryOutputDirPath, to ?? path.basename(from))
copyAndSkipIfExist(from, target, skipIfExist)
},
copyModules({ modules, skipIfExist = true }) {
const nodeModulesPath = path.join(entryOutputDirPath, 'node_modules')
for (const m of modules) {
const { rootPath } = getPackageInfoSync(m) || {}
if (!rootPath) {
log.warn(`Package '${m}' not found`, { timestamp: true })
continue
}
copyAndSkipIfExist(rootPath, path.join(nodeModulesPath, m), skipIfExist)
}
},
})
Expand Down
46 changes: 33 additions & 13 deletions src/vite/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ export interface BuildEntryOption {
*/
ignoreDynamicRequires?: boolean
/**
* `external` option in `build.rollupOptions`
* @default source => source.endsWith('.node')
* `external` option in `build.rollupOptions`, external `.node` by default
*/
external?: (source: string, importer: string | undefined, isResolved: boolean) => boolean | null | undefined | void
external?: string | string[] | ((source: string, importer: string | undefined, isResolved: boolean) => boolean | null | undefined | void)
/**
* Custom options for `vite` build
* ```ts
Expand Down Expand Up @@ -112,6 +111,20 @@ export interface BuildEntryOption {
*/
skipIfExist?: boolean
}) => void
/**
* Copy specified modules to entry output dir, just like `external` option in rollup
*/
copyModules: (options: {
/**
* External Modules
*/
modules: string[]
/**
* Skip copy if `to` exist
* @default true
*/
skipIfExist?: boolean
}) => void
}) => Promisable<void>
}

Expand Down Expand Up @@ -243,14 +256,7 @@ type ParseOptionReturn = {
buildAsarOption: BuildAsarOption
buildEntryOption: Required<Omit<BuildEntryOption, 'postBuild'>>
buildVersionOption: BuildVersionOption
postBuild: ((args: {
getPathFromEntryOutputDir: (...paths: string[]) => string
copyToEntryOutputDir: (options: {
from: string
to?: string
skipIfExist?: boolean
}) => void
}) => Promisable<void>) | undefined
postBuild: BuildEntryOption['postBuild']
cert: string
}

Expand All @@ -270,7 +276,7 @@ export function parseOptions(
nativeModuleEntryMap = {},
postBuild,
ignoreDynamicRequires = false,
external = (source: string) => source.endsWith('.node'),
external,
overrideViteOptions = {},
} = {},
paths: {
Expand Down Expand Up @@ -314,7 +320,21 @@ export function parseOptions(
nativeModuleEntryMap,
overrideViteOptions,
ignoreDynamicRequires,
external,
external: (source, importer, isResolved) => {
if (source.endsWith('.node')) {
return false
}
if (!external) {
return undefined
}
if (typeof external === 'string') {
return source === external
}
if (Array.isArray(external)) {
return external.includes(source)
}
return external(source, importer, isResolved)
},
}
// generate keys or get from file
const { privateKey, cert } = parseKeys({
Expand Down
13 changes: 13 additions & 0 deletions src/vite/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import fs from 'node:fs'
import { log } from './constant'

export function readableSize(size: number): string {
const units = ['B', 'KB', 'MB', 'GB']
let i = 0
Expand All @@ -9,3 +12,13 @@ export function readableSize(size: number): string {

return `${size.toFixed(2)} ${units[i]}`
}

export function copyAndSkipIfExist(from: string, to: string, skipIfExist: boolean): void {
if (!skipIfExist || !fs.existsSync(to)) {
try {
fs.cpSync(from, to, { recursive: true })
} catch (error) {
log.warn(`Copy failed: ${error}`, { timestamp: true })
}
}
}

0 comments on commit 1df8437

Please sign in to comment.