From 306b2337467be6c63c593b7fca4e14b44372c128 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 30 May 2023 12:38:26 +0200 Subject: [PATCH] fix(vite-node): don't externalize "dist" by default (#3446) --- docs/config/index.md | 2 +- package.json | 10 +++++----- packages/ui/client/auto-imports.d.ts | 1 - packages/vite-node/src/externalize.ts | 3 +-- packages/vite-node/src/server.ts | 5 +++++ packages/vite-node/src/types.ts | 8 ++++++++ test/core/test/timeout.spec.ts | 12 ++++++------ test/core/vitest.config.ts | 2 +- 8 files changed, 27 insertions(+), 16 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index 215b3a28a08d..6f8e92213767 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -136,7 +136,7 @@ You will not be able to edit your `node_modules` code for debugging, since the c #### deps.external - **Type:** `(string | RegExp)[]` -- **Default:** `['**/node_modules/**', '**/dist/**']` +- **Default:** `['**/node_modules/**']` Externalize means that Vite will bypass the package to native Node. Externalized dependencies will not be applied Vite's transformers and resolvers, so they do not support HMR on reload. Typically, packages under `node_modules` are externalized. diff --git a/package.json b/package.json index 4bc3f64e0f24..fb79badeea8a 100644 --- a/package.json +++ b/package.json @@ -19,11 +19,11 @@ "lint": "eslint --cache .", "lint:fix": "nr lint --fix", "release": "bumpp package.json packages/*/package.json --commit --push --tag && pnpm -r publish --access public", - "test": "vitest --api -r test/core", - "test:run": "vitest run -r test/core", - "test:all": "CI=true pnpm -r --stream run test --allowOnly", - "test:ci": "CI=true pnpm -r --stream --filter !test-fails --filter !test-browser --filter !test-esm --filter !test-browser run test --allowOnly", - "test:ci:single-thread": "CI=true pnpm -r --stream --filter !test-fails --filter !test-coverage --filter !test-watch --filter !test-bail --filter !test-esm --filter !test-browser run test --allowOnly --no-threads", + "test": "VITE_NODE_DEPS_MODULE_DIRECTORIES=/node_modules/,/packages/ vitest --api -r test/core", + "test:run": "VITE_NODE_DEPS_MODULE_DIRECTORIES=/node_modules/,/packages/ vitest run -r test/core", + "test:all": "VITE_NODE_DEPS_MODULE_DIRECTORIES=/node_modules/,/packages/ CI=true pnpm -r --stream run test --allowOnly", + "test:ci": "VITE_NODE_DEPS_MODULE_DIRECTORIES=/node_modules/,/packages/ CI=true pnpm -r --stream --filter !test-fails --filter !test-browser --filter !test-esm --filter !test-browser run test --allowOnly", + "test:ci:single-thread": "VITE_NODE_DEPS_MODULE_DIRECTORIES=/node_modules/,/packages/ CI=true pnpm -r --stream --filter !test-fails --filter !test-coverage --filter !test-watch --filter !test-bail --filter !test-esm --filter !test-browser run test --allowOnly --no-threads", "typecheck": "tsc --noEmit", "typecheck:why": "tsc --noEmit --explainFiles > explainTypes.txt", "ui:build": "vite build packages/ui", diff --git a/packages/ui/client/auto-imports.d.ts b/packages/ui/client/auto-imports.d.ts index d6b718892a7d..0053cb6cc6d5 100644 --- a/packages/ui/client/auto-imports.d.ts +++ b/packages/ui/client/auto-imports.d.ts @@ -93,7 +93,6 @@ declare global { const toReactive: typeof import('@vueuse/core')['toReactive'] const toRef: typeof import('vue')['toRef'] const toRefs: typeof import('vue')['toRefs'] - const toValue: typeof import('@vueuse/core')['toValue'] const triggerRef: typeof import('vue')['triggerRef'] const tryOnBeforeMount: typeof import('@vueuse/core')['tryOnBeforeMount'] const tryOnBeforeUnmount: typeof import('@vueuse/core')['tryOnBeforeUnmount'] diff --git a/packages/vite-node/src/externalize.ts b/packages/vite-node/src/externalize.ts index d0433d8c3d5f..2d4f81f3a637 100644 --- a/packages/vite-node/src/externalize.ts +++ b/packages/vite-node/src/externalize.ts @@ -126,8 +126,7 @@ async function _shouldExternalize( if (matchExternalizePattern(id, moduleDirectories, depsExternal)) return id - const isDist = id.includes('/dist/') - if ((isLibraryModule || isDist) && await isValidNodeImport(id)) + if (isLibraryModule && await isValidNodeImport(id)) return id return false diff --git a/packages/vite-node/src/server.ts b/packages/vite-node/src/server.ts index bf4a2885056a..f468f51da063 100644 --- a/packages/vite-node/src/server.ts +++ b/packages/vite-node/src/server.ts @@ -65,6 +65,11 @@ export class ViteNodeServer { } if (options.debug) this.debugger = new Debugger(server.config.root, options.debug!) + + options.deps.moduleDirectories ??= [] + const customModuleDirectories = process.env.VITE_NODE_DEPS_MODULE_DIRECTORIES?.split(',') + if (customModuleDirectories) + options.deps.moduleDirectories.push(...customModuleDirectories) } shouldExternalize(id: string) { diff --git a/packages/vite-node/src/types.ts b/packages/vite-node/src/types.ts index 35a94ad498ae..077f974ec0a0 100644 --- a/packages/vite-node/src/types.ts +++ b/packages/vite-node/src/types.ts @@ -9,6 +9,14 @@ export type Awaitable = T | PromiseLike export interface DepsHandlingOptions { external?: (string | RegExp)[] inline?: (string | RegExp)[] | true + /** + * A list of directories that are considered to hold Node.js modules + * Have to include "/" at the start and end of the path + * + * Vite-Node checks the whole absolute path of the import, so make sure you don't include + * unwanted files accidentally + * @default ['/node_modules/'] + */ moduleDirectories?: string[] cacheDir?: string /** diff --git a/test/core/test/timeout.spec.ts b/test/core/test/timeout.spec.ts index 321f01ea721b..362d4c0cb0fd 100644 --- a/test/core/test/timeout.spec.ts +++ b/test/core/test/timeout.spec.ts @@ -1,17 +1,17 @@ import { describe, expect, test } from 'vitest' describe('suite timeout', () => { - test('true is true after 5100ms', async () => { - await new Promise(resolve => setTimeout(resolve, 5100)) + test('true is true after 100ms', async () => { + await new Promise(resolve => setTimeout(resolve, 10)) expect(true).toBe(true) }) }, { - timeout: 6000, + timeout: 100, }) describe('suite timeout simple input', () => { - test('true is true after 5100ms', async () => { - await new Promise(resolve => setTimeout(resolve, 5100)) + test('true is true after 100ms', async () => { + await new Promise(resolve => setTimeout(resolve, 10)) expect(true).toBe(true) }) -}, 6000) +}, 100) diff --git a/test/core/vitest.config.ts b/test/core/vitest.config.ts index 4f27df9a19b7..efae4016d4a8 100644 --- a/test/core/vitest.config.ts +++ b/test/core/vitest.config.ts @@ -63,7 +63,7 @@ export default defineConfig({ }, deps: { external: ['tinyspy', /src\/external/], - moduleDirectories: ['node_modules', 'projects'], + moduleDirectories: ['node_modules', 'projects', 'packages'], }, alias: [ {