Skip to content

Commit

Permalink
fix: support resolved Ids that start with null bytes
Browse files Browse the repository at this point in the history
fix #1471
  • Loading branch information
yyx990803 committed Jan 11, 2021
1 parent 62cbd53 commit 7074414
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/vite/src/node/constants.ts
Expand Up @@ -13,6 +13,12 @@ export const FS_PREFIX = `/@fs/`
* Prefix for resolved Ids that are not valid browser import specifiers
*/
export const VALID_ID_PREFIX = `/@id/`
/**
* Some Rollup plugins use ids that starts with the null byte \0 to avoid
* collisions, but it is not permitted in import URLs so we have to replace
* them.
*/
export const NULL_BYTE_PLACEHOLDER = `__x00__`

export const CLIENT_PUBLIC_PATH = `/@vite/client`
// eslint-disable-next-line
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -28,7 +28,8 @@ import {
CLIENT_DIR,
CLIENT_PUBLIC_PATH,
DEP_VERSION_RE,
VALID_ID_PREFIX
VALID_ID_PREFIX,
NULL_BYTE_PLACEHOLDER
} from '../constants'
import { ViteDevServer } from '..'
import { checkPublicFile } from './asset'
Expand Down Expand Up @@ -185,7 +186,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// prefix it to make it valid. We will strip this before feeding it
// back into the transform pipeline
if (!url.startsWith('.') && !url.startsWith('/')) {
url = VALID_ID_PREFIX + resolved.id
url =
VALID_ID_PREFIX + resolved.id.replace('\0', NULL_BYTE_PLACEHOLDER)
}

// mark non-js/css imports with `?import`
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/server/middlewares/transform.ts
Expand Up @@ -19,6 +19,7 @@ import {
DEP_CACHE_DIR,
DEP_VERSION_RE,
FS_PREFIX,
NULL_BYTE_PLACEHOLDER,
VALID_ID_PREFIX
} from '../../constants'

Expand All @@ -44,7 +45,10 @@ export function transformMiddleware(
return next()
}

let url = decodeURI(removeTimestampQuery(req.url!))
let url = decodeURI(removeTimestampQuery(req.url!)).replace(
NULL_BYTE_PLACEHOLDER,
'\0'
)
const withoutQuery = cleanUrl(url)

try {
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/server/transformRequest.ts
Expand Up @@ -88,7 +88,12 @@ export async function transformRequest(
// ensure module in graph after successful load
const mod = await moduleGraph.ensureEntryFromUrl(url)
// file is out of root, add it to the watch list
if (mod.file && !mod.file.startsWith(root + '/')) {
if (
mod.file &&
!mod.file.startsWith(root + '/') &&
// some rollup plugins use null bytes for private resolved Ids
!mod.file.includes('\0')
) {
watcher.add(mod.file)
}

Expand Down

0 comments on commit 7074414

Please sign in to comment.