From f98d8a6a465e114cc4e0448a5f77b2f009f4e0fe Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 16 Jan 2025 16:18:48 +0000 Subject: [PATCH] fix: add back externality for dev-bundler --- packages/vite/package.json | 1 + packages/vite/src/utils/external.ts | 36 +++++++++++++++++++++++++++++ pnpm-lock.yaml | 13 +++++++++++ 3 files changed, 50 insertions(+) create mode 100644 packages/vite/src/utils/external.ts diff --git a/packages/vite/package.json b/packages/vite/package.json index c8b52a67d9..8fdf3bde4c 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -41,6 +41,7 @@ "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": "^1.13.1", "jiti": "^2.4.2", diff --git a/packages/vite/src/utils/external.ts b/packages/vite/src/utils/external.ts new file mode 100644 index 0000000000..c6889cb3a1 --- /dev/null +++ b/packages/vite/src/utils/external.ts @@ -0,0 +1,36 @@ +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/pnpm-lock.yaml b/pnpm-lock.yaml index a398398920..752721ee39 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -843,6 +843,9 @@ 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 @@ -4452,6 +4455,9 @@ 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'} @@ -12076,6 +12082,13 @@ 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: {}