From 95ae29ba4ae62d81f1e07bbc3a649e9b29f70496 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 11 Mar 2024 13:00:00 +0100 Subject: [PATCH] refactor: move safeModulesPath set to server --- packages/vite/src/node/plugins/importAnalysis.ts | 4 +--- packages/vite/src/node/server/index.ts | 5 +++++ packages/vite/src/node/server/middlewares/static.ts | 3 +-- packages/vite/src/node/server/moduleGraph.ts | 7 ------- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 11a311d67cbe51..34761cb74c019b 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -525,9 +525,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { // record as safe modules // safeModulesPath should not include the base prefix. // See https://github.com/vitejs/vite/issues/9438#issuecomment-1465270409 - server?.browserEnvironment.moduleGraph.safeModulesPath.add( - fsPathFromUrl(stripBase(url, base)), - ) + server?._safeModulesPath.add(fsPathFromUrl(stripBase(url, base))) if (url !== specifier) { let rewriteDone = false diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index c9eda234f4f829..add8b0b98cc67c 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -385,6 +385,10 @@ export interface ViteDevServer { abort: () => void } > + /** + * @internal + */ + _safeModulesPath: Set /** * @internal */ @@ -696,6 +700,7 @@ export async function _createServer( _importGlobMap: new Map(), _forceOptimizeOnRestart: false, _pendingRequests: new Map(), + _safeModulesPath: new Set(), _fsDenyGlob: picomatch(config.server.fs.deny, { matchBase: true, nocase: true, diff --git a/packages/vite/src/node/server/middlewares/static.ts b/packages/vite/src/node/server/middlewares/static.ts index c498b57cb45450..40fc132c80ea19 100644 --- a/packages/vite/src/node/server/middlewares/static.ts +++ b/packages/vite/src/node/server/middlewares/static.ts @@ -217,8 +217,7 @@ export function isFileServingAllowed( if (server._fsDenyGlob(file)) return false - if (server.browserEnvironment.moduleGraph.safeModulesPath.has(file)) - return true // TODO: safeModulePaths shouldn't be part of a moduleGraph + if (server._safeModulesPath.has(file)) return true if ( server.config.server.fs.allow.some( diff --git a/packages/vite/src/node/server/moduleGraph.ts b/packages/vite/src/node/server/moduleGraph.ts index dc53a01eff0aa7..29e8f0a3ce486c 100644 --- a/packages/vite/src/node/server/moduleGraph.ts +++ b/packages/vite/src/node/server/moduleGraph.ts @@ -86,9 +86,6 @@ export class EnvironmentModuleGraph { // a single file may corresponds to multiple modules with different queries fileToModulesMap = new Map>() - // TODO: this property should be shared across all module graphs - safeModulesPath = new Set() - /** * @internal */ @@ -634,10 +631,6 @@ export class ModuleGraph { fileToModulesMap: Map> - get safeModulesPath(): Set { - return this._browser.safeModulesPath - } - constructor(moduleGraphs: { browser: EnvironmentModuleGraph node: EnvironmentModuleGraph