bridge
bridge copied to clipboard
Server failed when reloading `nuxt.config.js` with Bridge Vite
Environment
- Operating System:
Darwin - Node Version:
v16.11.1 - Nuxt Version:
2.16.0-27282256.ab1c6cb4 - Package Manager:
[email protected] - Bundler:
Webpack - User Config:
buildModules,css,target,components,bridge,unocss - Runtime Modules:
- - Build Modules:
@unocss/[email protected],@vueuse/core/[email protected],@nuxt/[email protected]
Reproduction
(Essential every Bridge app with Vite)
https://github.com/antfu/vitesse-nuxt-bridge https://github.com/nuxt/modules
Describe the bug
Everything work until we modify the nuxt.config.js and triggers this error:
FATAL Template not found: /Users/antfu/i/vitesse-nuxt-bridge/.nuxt/middleware.js 10:01:04
at normalizeTemplate (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/kit-edge/dist/index.mjs:924:13)
at addTemplate (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/kit-edge/dist/index.mjs:908:20)
at addPluginTemplate (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/kit-edge/dist/index.mjs:973:16)
at setup (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/bridge-edge/dist/chunks/module2.mjs:152:5)
at Object.wrappedModule (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/kit-edge/dist/index.mjs:1212:22)
at installModule (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/kit-edge/dist/index.mjs:1261:17)
at setup (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/bridge-edge/dist/chunks/module.mjs:993:13)
at async ModuleContainer.addModule (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/core-edge/dist/core.js:174:20)
at async ModuleContainer.ready (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/core-edge/dist/core.js:45:7)
at async Nuxt._init (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/core-edge/dist/core.js:346:5)
ERROR Cannot restart nuxt: Template not found: /Users/antfu/i/vitesse-nuxt-bridge/.nuxt/middleware.js
at normalizeTemplate (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/kit-edge/dist/index.mjs:924:13)
at addTemplate (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/kit-edge/dist/index.mjs:908:20)
at addPluginTemplate (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/kit-edge/dist/index.mjs:973:16)
at setup (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/bridge-edge/dist/chunks/module2.mjs:152:5)
at Object.wrappedModule (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/kit-edge/dist/index.mjs:1212:22)
at installModule (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/kit-edge/dist/index.mjs:1261:17)
at setup (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/bridge-edge/dist/chunks/module.mjs:993:13)
at async ModuleContainer.addModule (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/core-edge/dist/core.js:174:20)
at async ModuleContainer.ready (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/core-edge/dist/core.js:45:7)
at async Nuxt._init (node_modules/.pnpm/@[email protected]/node_modules/@nuxt/core-edge/dist/core.js:346:5)
ERROR [unhandledRejection] ENOENT: no such file or directory, open '/Users/antfu/i/vitesse-nuxt-bridge/.nuxt/dist/server/client.manifest.json'
Additional context
No response
Logs
No response
Kept running into this so I tried to look into it.
https://github.com/nuxt/framework/blob/33ebb01d7f0c25b5b89b767d9a0749084b0946c4/packages/kit/src/template.ts#L43
Changing this line to this if (!existsSync(template.src) && !template.getContent) { works as a workaround for the crash/breaking on saving nuxt.config.js. I can imagine this isn't a good way to solve the issue though.
https://github.com/nuxt/framework/blob/33ebb01d7f0c25b5b89b767d9a0749084b0946c4/packages/kit/src/template.ts#L41-L55
Both middleware.js and store.js will cause trigger this error on save/reload, but not on startup since src is not set yet (as seen here https://github.com/nuxt/framework/blob/main/packages/bridge/src/vite/templates.ts).
As both of these are added with addPluginTemplate the src will be set to their destination, and it seems like the compiled plugin template is removed on reload, which is why the error is thrown.
https://github.com/nuxt/framework/blob/33ebb01d7f0c25b5b89b767d9a0749084b0946c4/packages/kit/src/plugin.ts#L68-L80
Not sure what the intended way of fixing it is otherwise I would have opened a PR 😅.
For my dev project I added the following to the top of my nuxt.config so I can still play around without it breaking:
const kitPath = './node_modules/@nuxt/kit/dist/index.mjs'
const kit = fs.readFileSync(kitPath, 'utf-8')
fs.writeFileSync(
kitPath,
kit.replace('if (!existsSync(template.src))', 'if (!existsSync(template.src) && !template.getContents)')
)