diff --git a/packages/plugin/vite/src/VitePlugin.ts b/packages/plugin/vite/src/VitePlugin.ts index 1df51a905f..3d4a9a17fb 100644 --- a/packages/plugin/vite/src/VitePlugin.ts +++ b/packages/plugin/vite/src/VitePlugin.ts @@ -8,7 +8,6 @@ import { PRESET_TIMER } from 'listr2'; // eslint-disable-next-line node/no-unpublished-import import { default as vite } from 'vite'; -import { getFlatDependencies } from './util/package'; import { onBuildDone } from './util/plugins'; import ViteConfigGenerator from './ViteConfig'; @@ -104,7 +103,6 @@ Your packaged app may be larger than expected if you dont ignore everything othe packageAfterCopy = async (_forgeConfig: ResolvedForgeConfig, buildPath: string): Promise => { const pj = await fs.readJson(path.resolve(this.projectDir, 'package.json')); - const flatDependencies = await getFlatDependencies(this.projectDir); if (!pj.main?.includes('.vite/')) { throw new Error(`Electron Forge is configured to use the Vite plugin. The plugin expects the @@ -119,11 +117,6 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`); await fs.writeJson(path.resolve(buildPath, 'package.json'), pj, { spaces: 2, }); - - // Copy the dependencies in package.json - for (const dep of flatDependencies) { - await fs.copy(dep.src, path.resolve(buildPath, dep.dest)); - } }; startLogic = async (): Promise => { diff --git a/packages/plugin/vite/src/util/package.ts b/packages/plugin/vite/src/util/package.ts deleted file mode 100644 index 0fbb9a4a78..0000000000 --- a/packages/plugin/vite/src/util/package.ts +++ /dev/null @@ -1,108 +0,0 @@ -import path from 'node:path'; - -import fs from 'fs-extra'; - -export interface Dependency { - name: string; - path: SourceAndDestination; - dependencies: Dependency[]; -} - -export interface SourceAndDestination { - src: string; - dest: string; -} - -function isRootPath(dir: string) { - // *unix or Windows root path - return dir === '/' || /^[a-zA-Z]:\\$/i.test(dir); -} - -export async function isDirectory(p: string): Promise { - try { - const stat = await fs.promises.stat(p); - return stat.isDirectory(); - } catch { - return false; - } -} - -export async function lookupNodeModulesPaths(root: string, paths: string[] = []): Promise { - if (!root) return paths; - if (!path.isAbsolute(root)) return paths; - - const p = path.join(root, 'node_modules'); - - if (await isDirectory(p)) { - paths = paths.concat(p); - } - root = path.join(root, '..'); - - return isRootPath(root) ? paths : await lookupNodeModulesPaths(root, paths); -} - -export async function resolveDependencies(root: string) { - const rootDependencies = Object.keys((await fs.readJson(path.join(root, 'package.json'))).dependencies || {}); - const resolve = async (prePath: string, dependencies: string[], collected: Map = new Map()) => - await Promise.all( - dependencies.map(async (name) => { - let curPath = prePath, - depPath = null, - packageJson = null; - while (!packageJson && !isRootPath(curPath)) { - const allNodeModules = await lookupNodeModulesPaths(curPath); - - for (const nodeModules of allNodeModules) { - depPath = path.join(nodeModules, name); - if (await fs.pathExists(depPath)) break; - } - - if (depPath) { - try { - packageJson = await fs.readJson(path.join(depPath, 'package.json')); - } catch (err) { - // lookup node_modules - curPath = path.join(curPath, '..'); - if (curPath.length < root.length) { - console.error(`not found 'node_modules' in root path: ${root}`); - throw err; - } - } - } - } - - if (!depPath || !packageJson) { - throw new Error(`find dependencies error in: ${curPath}`); - } - - const result: Dependency = { - name, - path: { - src: depPath, - dest: path.relative(root, depPath), - }, - dependencies: [], - }; - const shouldResolveDeps = !collected.has(depPath); - collected.set(depPath, result); - if (shouldResolveDeps) { - result.dependencies = await resolve(depPath, Object.keys(packageJson.dependencies || {}), collected); - } - return result; - }) - ); - return resolve(root, rootDependencies); -} - -export async function getFlatDependencies(root = process.cwd()) { - const depsTree = await resolveDependencies(root); - const depsFlat = new Map(); - - const flatten = (dep: Dependency) => { - depsFlat.set(dep.path.src, dep.path); // dedup - dep.dependencies.forEach(flatten); - }; - depsTree.forEach(flatten); - - return [...depsFlat.values()]; -} diff --git a/packages/plugin/vite/test/util/package_spec.ts b/packages/plugin/vite/test/util/package_spec.ts deleted file mode 100644 index 8833d43afd..0000000000 --- a/packages/plugin/vite/test/util/package_spec.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { exec } from 'node:child_process'; -import path from 'node:path'; -import { promisify } from 'node:util'; - -import { expect } from 'chai'; - -import { getFlatDependencies, isDirectory, lookupNodeModulesPaths, resolveDependencies } from '../../src/util/package'; - -const execPromise = promisify(exec); -const packageRoot = path.join(__dirname, '../fixture/package'); -const depsTree = [ - { - name: 'electron-squirrel-startup', - path: { - src: path.join(packageRoot, 'node_modules', 'electron-squirrel-startup'), - dest: path.join('node_modules', 'electron-squirrel-startup'), - }, - dependencies: [ - { - name: 'debug', - path: { - src: path.join(packageRoot, 'node_modules', 'debug'), - dest: path.join('node_modules', 'debug'), - }, - dependencies: [ - { - name: 'ms', - path: { - src: path.join(packageRoot, 'node_modules', 'ms'), - dest: path.join('node_modules', 'ms'), - }, - dependencies: [], - }, - ], - }, - ], - }, -]; -const depsFlat = [ - { - src: path.join(packageRoot, 'node_modules', 'electron-squirrel-startup'), - dest: path.join('node_modules', 'electron-squirrel-startup'), - }, - { - src: path.join(packageRoot, 'node_modules', 'debug'), - dest: path.join('node_modules', 'debug'), - }, - { - src: path.join(packageRoot, 'node_modules', 'ms'), - dest: path.join('node_modules', 'ms'), - }, -]; - -describe('util/package', () => { - before(async () => { - await execPromise('npm install', { cwd: packageRoot }); - }); - - it('dependencies of package.json is resolved correct', async () => { - const depsT = await resolveDependencies(packageRoot); - const depsF = await getFlatDependencies(packageRoot); - expect(depsTree).deep.equal(depsT); - expect(depsFlat).deep.equal(depsF); - }); - - it('isDirectory check is correct', async () => { - expect(await isDirectory(packageRoot)).true; - expect(await isDirectory(path.join(packageRoot, 'package.json'))).false; - }); - - it('node_modules paths is lookup correct', async () => { - const paths = await lookupNodeModulesPaths(packageRoot); - expect(paths.length).gt(0); - }); -}); diff --git a/packages/template/vite-typescript/tmpl/vite.base.config.ts b/packages/template/vite-typescript/tmpl/vite.base.config.ts index 25120d990d..f150dc81e9 100644 --- a/packages/template/vite-typescript/tmpl/vite.base.config.ts +++ b/packages/template/vite-typescript/tmpl/vite.base.config.ts @@ -1,11 +1,10 @@ import { builtinModules } from 'node:module'; import type { AddressInfo } from 'node:net'; import type { ConfigEnv, Plugin, UserConfig } from 'vite'; -import pkg from './package.json'; export const builtins = ['electron', ...builtinModules.map((m) => [m, `node:${m}`]).flat()]; -export const external = [...builtins, ...Object.keys('dependencies' in pkg ? (pkg.dependencies as Record) : {})]; +export const external = [...builtins]; export function getBuildConfig(env: ConfigEnv<'build'>): UserConfig { const { root, mode, command } = env; diff --git a/packages/template/vite/tmpl/vite.base.config.mjs b/packages/template/vite/tmpl/vite.base.config.mjs index 6f9b0c52f4..062c9491ab 100644 --- a/packages/template/vite/tmpl/vite.base.config.mjs +++ b/packages/template/vite/tmpl/vite.base.config.mjs @@ -1,12 +1,11 @@ import { builtinModules } from 'node:module'; -import pkg from './package.json'; export const builtins = [ 'electron', ...builtinModules.map((m) => [m, `node:${m}`]).flat(), ]; -export const external = [...builtins, ...Object.keys(pkg.dependencies || {})]; +export const external = [...builtins]; /** @type {(env: import('vite').ConfigEnv<'build'>) => import('vite').UserConfig} */ export const getBuildConfig = (env) => {