Created
November 9, 2021 04:25
-
-
Save jed/83241e637501acf6073170c9c1bc8632 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
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