Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add deps.includeSourcemap #7095

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ Vite will process inlined modules. This could be helpful to handle packages that

If `true`, every dependency will be inlined. All dependencies, specified in [`ssr.noExternal`](https://vitejs.dev/guide/ssr.html#ssr-externals) will be inlined by default.

#### server.deps.includeSourcemap

- **Type:** `(string | RegExp)[]`
- **Default:** `[]`

Vitest will include inline source maps for any dependency specified in `inlineSourcemap`.

#### server.deps.fallbackCJS

- **Type** `boolean`
Expand Down
3 changes: 3 additions & 0 deletions packages/vite-node/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ function parseServerOptions(
external: toArray(serverOptions.deps?.external).map((dep) => {
return dep.startsWith('/') && dep.endsWith('/') ? new RegExp(dep) : dep
}),
includeSourcemap: toArray(serverOptions.deps?.includeSourcemap).map((dep) => {
return dep.startsWith('/') && dep.endsWith('/') ? new RegExp(dep) : dep
}),
moduleDirectories: serverOptions.deps?.moduleDirectories
? toArray(serverOptions.deps?.moduleDirectories)
: undefined,
Expand Down
20 changes: 10 additions & 10 deletions packages/vite-node/src/externalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ async function _shouldExternalize(

const moduleDirectories = options?.moduleDirectories || ['/node_modules/']

if (matchExternalizePattern(id, moduleDirectories, options?.inline)) {
if (matchDependencyPattern(id, moduleDirectories, options?.inline)) {
return false
}
if (options?.inlineFiles && options?.inlineFiles.includes(id)) {
return false
}
if (matchExternalizePattern(id, moduleDirectories, options?.external)) {
if (matchDependencyPattern(id, moduleDirectories, options?.external)) {
return id
}

Expand All @@ -138,10 +138,10 @@ async function _shouldExternalize(
const guessCJS = isLibraryModule && options?.fallbackCJS
id = guessCJS ? guessCJSversion(id) || id : id

if (matchExternalizePattern(id, moduleDirectories, defaultInline)) {
if (matchDependencyPattern(id, moduleDirectories, defaultInline)) {
return false
}
if (matchExternalizePattern(id, moduleDirectories, depsExternal)) {
if (matchDependencyPattern(id, moduleDirectories, depsExternal)) {
return id
}

Expand All @@ -152,7 +152,7 @@ async function _shouldExternalize(
return false
}

function matchExternalizePattern(
export function matchDependencyPattern(
id: string,
moduleDirectories: string[],
patterns?: (string | RegExp)[] | true,
Expand All @@ -163,22 +163,22 @@ function matchExternalizePattern(
if (patterns === true) {
return true
}
for (const ex of patterns) {
if (typeof ex === 'string') {
if (moduleDirectories.some(dir => id.includes(join(dir, ex)))) {
for (const pattern of patterns) {
if (typeof pattern === 'string') {
if (moduleDirectories.some(dir => id.includes(join(dir, pattern)))) {
return true
}
}
else {
if (ex.test(id)) {
if (pattern.test(id)) {
return true
}
}
}
return false
}

function patchWindowsImportPath(path: string) {
export function patchWindowsImportPath(path: string) {
if (path.match(/^\w:\\/)) {
return `file:///${slash(path)}`
}
Expand Down
21 changes: 19 additions & 2 deletions packages/vite-node/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { pathToFileURL } from 'node:url'
import createDebug from 'debug'
import { join, normalize, relative, resolve } from 'pathe'
import { Debugger } from './debug'
import { shouldExternalize } from './externalize'
import { matchDependencyPattern, patchWindowsImportPath, shouldExternalize } from './externalize'
import { withInlineSourcemap } from './source-map'
import {
normalizeModuleId,
Expand Down Expand Up @@ -395,6 +395,23 @@ export class ViteNodeServer {
})
}

shouldIncludeSourceMap(id: string) {
if (!id.includes('/node_modules/')) {
return true
}

const { includeSourcemap, moduleDirectories } = this.options.deps || {}
if (!includeSourcemap?.length) {
return false
}

return matchDependencyPattern(
patchWindowsImportPath(id),
moduleDirectories || ['/node_modules/'],
includeSourcemap,
)
}

private async _transformRequest(
id: string,
filepath: string,
Expand Down Expand Up @@ -424,7 +441,7 @@ export class ViteNodeServer {
}

const sourcemap = this.options.sourcemap ?? 'inline'
if (sourcemap === 'inline' && result && !id.includes('node_modules')) {
if (sourcemap === 'inline' && result && this.shouldIncludeSourceMap(id)) {
result = await this.processTransformResult(filepath, result)
}

Expand Down
1 change: 1 addition & 0 deletions packages/vite-node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface DepsHandlingOptions {
external?: (string | RegExp)[]
inline?: (string | RegExp)[] | true
inlineFiles?: string[]
includeSourcemap?: (string | RegExp)[]
/**
* A list of directories that are considered to hold Node.js modules
* Have to include "/" at the start and end of the path
Expand Down
48 changes: 47 additions & 1 deletion test/vite-node/test/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join, resolve } from 'pathe'
import { createServer, type Plugin, type ViteDevServer } from 'vite'
import { ViteNodeServer } from 'vite-node/server'
import { describe, expect, test, vi } from 'vitest'
import { describe, expect, onTestFinished, test, vi } from 'vitest'
import { extractSourceMap } from '../../../packages/vite-node/src/source-map'

describe('server works correctly', async () => {
Expand Down Expand Up @@ -281,3 +281,49 @@ describe('externalize', () => {
})
})
})

describe('includeSourcemap', () => {
test('doesn\'t include source maps for deps when not specified', async () => {
const server = await createServer()
onTestFinished(() => server.close())

const vnServer = new ViteNodeServer(server, {
deps: {
inline: true,
},
})

const result = await vnServer.fetchResult('/node_modules/vitest/vitest.mjs', 'ssr')
expect(result.code).not.toContain('//# sourceMappingURL=')
})

test('doesn\'t include source maps for deps not matching pattern', async () => {
const server = await createServer()
onTestFinished(() => server.close())

const vnServer = new ViteNodeServer(server, {
deps: {
inline: true,
includeSourcemap: ['foo'],
},
})

const result = await vnServer.fetchResult('/node_modules/vitest/vitest.mjs', 'ssr')
expect(result.code).not.toContain('//# sourceMappingURL=')
})

test('includes source maps for deps matching pattern', async () => {
const server = await createServer()
onTestFinished(() => server.close())

const vnServer = new ViteNodeServer(server, {
deps: {
inline: true,
includeSourcemap: ['vitest'],
},
})

const result = await vnServer.fetchResult('/node_modules/vitest/vitest.mjs', 'ssr')
expect(result.code).toContain('//# sourceMappingURL=')
})
})
Loading