fix(nuxt3): only treeshake macro exports when building (#2809)

This commit is contained in:
Daniel Roe 2022-01-19 18:07:54 +00:00 committed by GitHub
parent 7f62ed1e32
commit 95d8205cc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import { findStaticImports, findExports } from 'mlly'
export interface TransformMacroPluginOptions { export interface TransformMacroPluginOptions {
macros: Record<string, string> macros: Record<string, string>
dev?: boolean
} }
export const TransformMacroPlugin = createUnplugin((options: TransformMacroPluginOptions) => { export const TransformMacroPlugin = createUnplugin((options: TransformMacroPluginOptions) => {
@ -42,7 +43,7 @@ export const TransformMacroPlugin = createUnplugin((options: TransformMacroPlugi
if (match.specifier && match._type === 'named') { if (match.specifier && match._type === 'named') {
// [webpack] Export named exports rather than the default (component) // [webpack] Export named exports rather than the default (component)
return code.replace(match.code, `export {${Object.values(options.macros).join(', ')}} from "${match.specifier}"`) return code.replace(match.code, `export {${Object.values(options.macros).join(', ')}} from "${match.specifier}"`)
} else { } else if (!options.dev) {
// ensure we tree-shake any _other_ default exports out of the macro script // ensure we tree-shake any _other_ default exports out of the macro script
code = code.replace(match.code, '/*#__PURE__*/ false &&') code = code.replace(match.code, '/*#__PURE__*/ false &&')
code += '\nexport default {}' code += '\nexport default {}'

View File

@ -47,6 +47,7 @@ export default defineNuxtModule({
// Extract macros from pages // Extract macros from pages
const macroOptions: TransformMacroPluginOptions = { const macroOptions: TransformMacroPluginOptions = {
dev: nuxt.options.dev,
macros: { macros: {
definePageMeta: 'meta' definePageMeta: 'meta'
} }