From d2db0b7e502ee1e8eb7b8567ae3e52ab3ac3fdae Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 16 Jan 2025 15:41:40 +0000 Subject: [PATCH] refactor(vite): drop `externality` and use vite internal config --- packages/vite/package.json | 1 - packages/vite/src/utils/external.ts | 36 ----------------------------- packages/vite/src/vite-node.ts | 35 +++++++++++++++++----------- pnpm-lock.yaml | 13 ----------- 4 files changed, 21 insertions(+), 64 deletions(-) delete mode 100644 packages/vite/src/utils/external.ts diff --git a/packages/vite/package.json b/packages/vite/package.json index 0d56cf0cc3..9ad25027f1 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -41,7 +41,6 @@ "defu": "^6.1.4", "esbuild": "^0.24.2", "escape-string-regexp": "^5.0.0", - "externality": "^1.0.2", "get-port-please": "^3.1.2", "h3": "npm:h3-nightly@2.0.0-1718872656.6765a6e", "jiti": "^2.4.2", diff --git a/packages/vite/src/utils/external.ts b/packages/vite/src/utils/external.ts deleted file mode 100644 index c6889cb3a1..0000000000 --- a/packages/vite/src/utils/external.ts +++ /dev/null @@ -1,36 +0,0 @@ -import type { ExternalsOptions } from 'externality' -import { ExternalsDefaults, isExternal } from 'externality' -import type { ViteDevServer } from 'vite' -import escapeStringRegexp from 'escape-string-regexp' -import { withTrailingSlash } from 'ufo' -import type { Nuxt } from 'nuxt/schema' -import { resolve } from 'pathe' -import { toArray } from '.' - -export function createIsExternal (viteServer: ViteDevServer, nuxt: Nuxt) { - const externalOpts: ExternalsOptions = { - inline: [ - /virtual:/, - /\.ts$/, - ...ExternalsDefaults.inline || [], - ...( - viteServer.config.ssr.noExternal && viteServer.config.ssr.noExternal !== true - ? toArray(viteServer.config.ssr.noExternal) - : [] - ), - ], - external: [ - '#shared', - new RegExp('^' + escapeStringRegexp(withTrailingSlash(resolve(nuxt.options.rootDir, nuxt.options.dir.shared)))), - ...(viteServer.config.ssr.external as string[]) || [], - /node_modules/, - ], - resolve: { - modules: nuxt.options.modulesDir, - type: 'module', - extensions: ['.ts', '.js', '.json', '.vue', '.mjs', '.jsx', '.tsx', '.wasm'], - }, - } - - return (id: string) => isExternal(id, nuxt.options.rootDir, externalOpts) -} diff --git a/packages/vite/src/vite-node.ts b/packages/vite/src/vite-node.ts index 914bb713b6..c358c1343c 100644 --- a/packages/vite/src/vite-node.ts +++ b/packages/vite/src/vite-node.ts @@ -6,13 +6,12 @@ import { isAbsolute, join, normalize, resolve } from 'pathe' // import { addDevServerHandler } from '@nuxt/kit' import { isFileServingAllowed } from 'vite' import type { ModuleNode, Plugin as VitePlugin } from 'vite' -import { getQuery } from 'ufo' +import { getQuery, withTrailingSlash } from 'ufo' import { normalizeViteManifest } from 'vue-bundle-renderer' -import { resolve as resolveModule } from 'mlly' +import escapeStringRegexp from 'escape-string-regexp' import { distDir } from './dirs' import type { ViteBuildContext } from './vite' -import { isCSS } from './utils' -import { createIsExternal } from './utils/external' +import { isCSS, toArray } from './utils' import { transpile } from './utils/transpile' // TODO: Remove this in favor of registerViteNodeMiddleware @@ -118,10 +117,27 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set = ne const node = new ViteNodeServer(viteServer, { deps: { inline: [ - /\/node_modules\/(.*\/)?(nuxt|nuxt3|nuxt-nightly)\//, + /^virtual:/, + /\.ts$/, + // Rollup + /\0/, + // Common /^#/, + /\?/, + /\/node_modules\/(.*\/)?(nuxt|nuxt3|nuxt-nightly)\//, + ...( + viteServer.config.ssr.noExternal && viteServer.config.ssr.noExternal !== true + ? toArray(viteServer.config.ssr.noExternal) + : [] + ), ...transpile({ isServer: true, isDev: ctx.nuxt.options.dev }), ], + external: [ + '#shared', + new RegExp('^' + escapeStringRegexp(withTrailingSlash(resolve(ctx.nuxt.options.rootDir, ctx.nuxt.options.dir.shared)))), + ...(viteServer.config.ssr.external as string[]) || [], + /node_modules/, + ], }, transformMode: { ssr: [/.*/], @@ -129,15 +145,6 @@ function createViteNodeApp (ctx: ViteBuildContext, invalidates: Set = ne }, }) - const isExternal = createIsExternal(viteServer, ctx.nuxt) - node.shouldExternalize = async (id: string) => { - const result = await isExternal(id) - if (result?.external) { - return resolveModule(result.id, { url: ctx.nuxt.options.modulesDir }).catch(() => false) - } - return false - } - return eventHandler(async (event) => { const moduleId = decodeURI(event.path).substring(1) if (moduleId === '/') { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cf7bc24fc6..d520c8402b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -837,9 +837,6 @@ importers: escape-string-regexp: specifier: ^5.0.0 version: 5.0.0 - externality: - specifier: ^1.0.2 - version: 1.0.2 get-port-please: specifier: ^3.1.2 version: 3.1.2 @@ -4462,9 +4459,6 @@ packages: extend@3.0.2: resolution: {integrity: sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==} - externality@1.0.2: - resolution: {integrity: sha512-LyExtJWKxtgVzmgtEHyQtLFpw1KFhQphF9nTG8TpAIVkiI/xQ3FJh75tRFLYl4hkn7BNIIdLJInuDAavX35pMw==} - fake-indexeddb@6.0.0: resolution: {integrity: sha512-YEboHE5VfopUclOck7LncgIqskAqnv4q0EWbYCaxKKjAvO93c+TJIaBuGy8CBFdbg9nKdpN3AuPRwVBJ4k7NrQ==} engines: {node: '>=18'} @@ -12047,13 +12041,6 @@ snapshots: extend@3.0.2: {} - externality@1.0.2: - dependencies: - enhanced-resolve: 5.18.0 - mlly: 1.7.4 - pathe: 1.1.2 - ufo: 1.5.4 - fake-indexeddb@6.0.0: {} fast-deep-equal@3.1.3: {}