Skip to content

Instantly share code, notes, and snippets.

@jed
Created November 9, 2021 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jed/83241e637501acf6073170c9c1bc8632 to your computer and use it in GitHub Desktop.
Save jed/83241e637501acf6073170c9c1bc8632 to your computer and use it in GitHub Desktop.
export default (build, namespace = 'view-source') => ({
name: namespace,
setup({initialOptions, onLoad, onResolve}) {
let options = {...initialOptions, write: false}
let filter = new RegExp(`^${namespace}:`)
onResolve({filter}, ({path, importer}) => {
path = path.replace(filter, '')
try { importer = new URL(importer) }
catch (e) { importer = new URL(`${namespace}://${importer}`) }
let {href, protocol, pathname} = new URL(path, importer)
path = protocol === `${namespace}:` ? pathname : href
return {namespace, path}
})
onLoad({filter: /.*/, namespace}, async ({path}) => {
let result = await build({...options, entryPoints: [path]})
let contents = JSON.stringify(result.outputFiles[0].text)
return {contents, loader: 'json'}
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment