2022-02-25 19:11:01 +00:00
|
|
|
import { useNuxt } from '@nuxt/kit'
|
|
|
|
import VirtualModulesPlugin from 'webpack-virtual-modules'
|
|
|
|
|
|
|
|
export function registerVirtualModules () {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
|
|
|
|
// Initialize virtual modules instance
|
|
|
|
const virtualModules = new VirtualModulesPlugin(nuxt.vfs)
|
|
|
|
const writeFiles = () => {
|
|
|
|
for (const filePath in nuxt.vfs) {
|
2024-06-07 22:32:46 +00:00
|
|
|
virtualModules.writeModule(filePath, nuxt.vfs[filePath] || '')
|
2022-02-25 19:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Workaround to initialize virtual modules
|
2022-10-27 10:36:37 +00:00
|
|
|
nuxt.hook('webpack:compile', ({ compiler }) => {
|
2022-02-25 19:11:01 +00:00
|
|
|
if (compiler.name === 'server') { writeFiles() }
|
|
|
|
})
|
|
|
|
// Update virtual modules when templates are updated
|
|
|
|
nuxt.hook('app:templatesGenerated', writeFiles)
|
|
|
|
|
|
|
|
nuxt.hook('webpack:config', configs => configs.forEach((config) => {
|
|
|
|
// Support virtual modules (input)
|
2022-08-26 15:47:29 +00:00
|
|
|
config.plugins!.push(virtualModules)
|
2022-02-25 19:11:01 +00:00
|
|
|
}))
|
|
|
|
}
|