Last active
May 12, 2020 02:06
-
-
Save PierBover/a0752f468aba1e6ca34024509b4164ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const routesFilesPlugin = { | |
name: 'route-files', | |
buildStart: async (options) => { | |
console.log('Generating routeModules.js'); | |
const routesDir = path.resolve(__dirname, 'src/components/routes'); | |
const routeFiles = fs.readdirSync(routesDir); | |
const routesString = routeFiles.map((filename) => { | |
const name = filename.replace('.svelte', ''); | |
return ` | |
{ | |
name: '${name}', | |
async load () { | |
if (loadedModules['${name}']) return loadedModules['${name}']; | |
const imported = await import('routes/${filename}'); | |
const module = imported.default; | |
loadedModules[name] = module; | |
return module; | |
} | |
} | |
`; | |
}); | |
const script = ` | |
// FILE GENERATED DYNAMICALLY BY ROLLUP!!! | |
const loadedModules = {}; | |
const routeLoaders = [${routesString}]; | |
export function getRouteLoader (name) { | |
const loader = routeLoaders.find((route) => route.name === name); | |
if (!loader) throw 'Route does not exist! ' + name; | |
return loader; | |
} | |
`; | |
fs.writeFileSync('src/router/routeModules.js', script, 'utf-8'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment