Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(vite-node): don't externalize "dist" by default (#3446)
  • Loading branch information
sheremet-va committed May 30, 2023
1 parent e39adea commit 306b233
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/config/index.md
Expand Up @@ -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.

Expand Down
10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion packages/ui/client/auto-imports.d.ts
Expand Up @@ -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']
Expand Down
3 changes: 1 addition & 2 deletions packages/vite-node/src/externalize.ts
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions packages/vite-node/src/server.ts
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions packages/vite-node/src/types.ts
Expand Up @@ -9,6 +9,14 @@ export type Awaitable<T> = T | PromiseLike<T>
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
/**
Expand Down
12 changes: 6 additions & 6 deletions 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)
2 changes: 1 addition & 1 deletion test/core/vitest.config.ts
Expand Up @@ -63,7 +63,7 @@ export default defineConfig({
},
deps: {
external: ['tinyspy', /src\/external/],
moduleDirectories: ['node_modules', 'projects'],
moduleDirectories: ['node_modules', 'projects', 'packages'],
},
alias: [
{
Expand Down

0 comments on commit 306b233

Please sign in to comment.