mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
fix(nuxt3): only treeshake macro exports when building (#2809)
This commit is contained in:
parent
7f62ed1e32
commit
95d8205cc3
@ -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 {}'
|
||||||
|
@ -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'
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user