fix(nuxt): don't return stub function from tree-shaken composables (#5786)

This commit is contained in:
Daniel Roe 2022-07-08 13:42:26 +01:00 committed by GitHub
parent 728a0a5ef4
commit b045b7141b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -10,7 +10,7 @@ interface TreeShakePluginOptions {
} }
export const TreeShakePlugin = createUnplugin((options: TreeShakePluginOptions) => { export const TreeShakePlugin = createUnplugin((options: TreeShakePluginOptions) => {
const COMPOSABLE_RE = new RegExp(`($|\\s*)(${options.treeShake.join('|')})(?=\\()`, 'g') const COMPOSABLE_RE = new RegExp(`($|\\s+)(${options.treeShake.join('|')})(?=\\()`, 'gm')
return { return {
name: 'nuxt:server-treeshake:transfrom', name: 'nuxt:server-treeshake:transfrom',
@ -35,7 +35,7 @@ export const TreeShakePlugin = createUnplugin((options: TreeShakePluginOptions)
const s = new MagicString(code) const s = new MagicString(code)
const strippedCode = stripLiteral(code) const strippedCode = stripLiteral(code)
for (const match of strippedCode.matchAll(COMPOSABLE_RE) || []) { for (const match of strippedCode.matchAll(COMPOSABLE_RE) || []) {
s.overwrite(match.index, match.index + match[0].length, `(() => {}) || /*#__PURE__*/ false && ${match[0]}`) s.overwrite(match.index, match.index + match[0].length, `${match[1]} /*#__PURE__*/ false && ${match[2]}`)
} }
if (s.hasChanged()) { if (s.hasChanged()) {