From 70744143cd09863412a59aad14de888067bd8531 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 11 Jan 2021 16:43:10 -0500 Subject: [PATCH] fix: support resolved Ids that start with null bytes fix #1471 --- packages/vite/src/node/constants.ts | 6 ++++++ packages/vite/src/node/plugins/importAnalysis.ts | 6 ++++-- packages/vite/src/node/server/middlewares/transform.ts | 6 +++++- packages/vite/src/node/server/transformRequest.ts | 7 ++++++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/constants.ts b/packages/vite/src/node/constants.ts index 17e5626573dc72..66bec81230a9a1 100644 --- a/packages/vite/src/node/constants.ts +++ b/packages/vite/src/node/constants.ts @@ -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 diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 490adf130c3868..5310e7ee9f253b 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -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' @@ -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` diff --git a/packages/vite/src/node/server/middlewares/transform.ts b/packages/vite/src/node/server/middlewares/transform.ts index 10f74df58ccab6..6b9a57684de06c 100644 --- a/packages/vite/src/node/server/middlewares/transform.ts +++ b/packages/vite/src/node/server/middlewares/transform.ts @@ -19,6 +19,7 @@ import { DEP_CACHE_DIR, DEP_VERSION_RE, FS_PREFIX, + NULL_BYTE_PLACEHOLDER, VALID_ID_PREFIX } from '../../constants' @@ -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 { diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index 665a2c020e15dd..1518545b5a2b0e 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -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) }