From a73230e23b0b787544b0486444bf7d82fc78a6e2 Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Tue, 14 Nov 2023 07:34:06 +1100 Subject: [PATCH] throw error when not using config file --- packages/remix-dev/vite/plugin.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/remix-dev/vite/plugin.ts b/packages/remix-dev/vite/plugin.ts index 81de0abe566..c9c76d350c1 100644 --- a/packages/remix-dev/vite/plugin.ts +++ b/packages/remix-dev/vite/plugin.ts @@ -554,13 +554,20 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => { ? { isSsrBuild: true, getManifest: createBuildManifest } : { isSsrBuild: false }; - // load same config file again for child compiler so that - // parent and child compiler's plugins have independent state. - // note that reusing `viteUserConfig.plugins` for child compiler would lead - // to mutating single plugin instance in an unexpected way, - // for example, during `vite build`, `configResolved` plugin hook would called - // with `command = "build"` by parent and then `command = "serve"` by child. - let childConfigFile = await vite.loadConfigFromFile( + // We load the same Vite config file again for the child compiler so + // that both parent and child compiler's plugins have independent state. + // If we re-used the `viteUserConfig.plugins` array for the child + // compiler, it could lead to mutating shared state between plugin + // instances in unexpected ways, e.g. during `vite build` the + // `configResolved` plugin hook would be called with `command = "build"` + // by parent and then `command = "serve"` by child, which some plugins + // may respond to by updating state referenced by the parent. + if (!viteConfig.configFile) { + throw new Error( + "The Remix Vite plugin requires the use of a Vite config file" + ); + } + let childCompilerConfigFile = await vite.loadConfigFromFile( { command: viteConfig.command, mode: viteConfig.mode, @@ -571,6 +578,11 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => { viteConfig.configFile ); + invariant( + childCompilerConfigFile, + "Vite config file was unable to be resolved for Remix child compiler" + ); + viteChildCompiler = await vite.createServer({ ...viteUserConfig, mode: viteConfig.mode, @@ -584,7 +596,7 @@ export const remixVitePlugin: RemixVitePlugin = (options = {}) => { configFile: false, envFile: false, plugins: [ - ...(childConfigFile?.config.plugins ?? []) + ...(childCompilerConfigFile.config.plugins ?? []) .flat() // Exclude this plugin from the child compiler to prevent an // infinite loop (plugin creates a child compiler with the same