feat(nitro): raw loader (#75)

This commit is contained in:
pooya parsa 2021-04-11 20:20:41 +02:00 committed by GitHub
parent 8fbefa309e
commit 2d60e71fcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import { autoMock } from './plugins/automock'
import { staticAssets, dirnames } from './plugins/static'
import { middleware } from './plugins/middleware'
import { esbuild } from './plugins/esbuild'
import { raw } from './plugins/raw'
export type RollupConfig = InputOptions & { output: OutputOptions }
@ -109,6 +110,9 @@ export const getRollupConfig = (nitroContext: NitroContext) => {
rollupConfig.plugins.push(timing())
}
// Raw asset loader
rollupConfig.plugins.push(raw())
// https://github.com/rollup/plugins/tree/master/packages/replace
rollupConfig.plugins.push(replace({
// @ts-ignore https://github.com/rollup/plugins/pull/810

View File

@ -0,0 +1,20 @@
import { extname } from 'upath'
import type { Plugin } from 'rollup'
export interface RawOptions {
extensions?: string[]
}
export function raw (opts: RawOptions = {}): Plugin {
const extensions = new Set(['.md', '.mdx', '.yml', '.txt', '.css', '.htm', '.html']
.concat(opts.extensions || []))
return {
name: 'raw',
transform (code, id) {
if (id[0] !== '\0' && extensions.has(extname(id))) {
return `// ${id}\nexport default ${JSON.stringify(code)}`
}
}
}
}