Skip to content

Commit

Permalink
feat: respect the vite config when generating the output
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Mar 8, 2021
1 parent 07ebd12 commit b9e1734
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const defaultOptions: PluginOptions = {
export default function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
const pluginOptions = { ...defaultOptions, ...userOptions }

let viteConfig: ResolvedConfig

const filter = createFilter(pluginOptions.include, pluginOptions.exclude)

const directives = [...Object.values(builtinDiretcives), ...pluginOptions.customDirectives]
Expand All @@ -29,6 +31,9 @@ export default function imagetools(userOptions: Partial<PluginOptions> = {}): Pl
return {
name: 'imagetools',
enforce: 'pre',
configResolved(cfg) {
viteConfig = cfg
},
async load(id) {
const src = new URL(id, 'file://')

Expand Down Expand Up @@ -93,7 +98,13 @@ export default function imagetools(userOptions: Partial<PluginOptions> = {}): Pl
.map(f => f(src, outputMetadatas))
.find(res => !!res)

return dataToEsm(output)
// output as JSON or esm depending on the vite config
return viteConfig.json?.stringify
? `export default = JSON.parse(${JSON.stringify(JSON.stringify(output))})`
: dataToEsm(output, {
namedExports: viteConfig.json.namedExports,
compact: !!viteConfig.build.minify
})
}
}
}
Expand Down

0 comments on commit b9e1734

Please sign in to comment.