mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
fix(nuxt): prevent treeshaking hooks with composable names (#20745)
This commit is contained in:
parent
eb8e9169d6
commit
80d7899f49
@ -15,7 +15,10 @@ export const TreeShakeComposablesPlugin = createUnplugin((options: TreeShakeComp
|
||||
* @todo Use the options import-path to tree-shake composables in a safer way.
|
||||
*/
|
||||
const composableNames = Object.values(options.composables).flat()
|
||||
const COMPOSABLE_RE = new RegExp(`($\\s+)(${composableNames.join('|')})(?=\\()`, 'gm')
|
||||
|
||||
const regexp = `(^\\s*)(${composableNames.join('|')})(?=\\((?!\\) \\{))`
|
||||
const COMPOSABLE_RE = new RegExp(regexp, 'm')
|
||||
const COMPOSABLE_RE_GLOBAL = new RegExp(regexp, 'gm')
|
||||
|
||||
return {
|
||||
name: 'nuxt:tree-shake-composables:transform',
|
||||
@ -24,11 +27,11 @@ export const TreeShakeComposablesPlugin = createUnplugin((options: TreeShakeComp
|
||||
return isVue(id, { type: ['script'] }) || isJS(id)
|
||||
},
|
||||
transform (code) {
|
||||
if (!code.match(COMPOSABLE_RE)) { return }
|
||||
if (!COMPOSABLE_RE.test(code)) { return }
|
||||
|
||||
const s = new MagicString(code)
|
||||
const strippedCode = stripLiteral(code)
|
||||
for (const match of strippedCode.matchAll(COMPOSABLE_RE) || []) {
|
||||
for (const match of strippedCode.matchAll(COMPOSABLE_RE_GLOBAL) || []) {
|
||||
s.overwrite(match.index!, match.index! + match[0].length, `${match[1]} /*#__PURE__*/ false && ${match[2]}`)
|
||||
}
|
||||
|
||||
|
9
test/fixtures/basic/plugins/style.ts
vendored
9
test/fixtures/basic/plugins/style.ts
vendored
@ -1,5 +1,14 @@
|
||||
import '~/assets/plugin.css'
|
||||
|
||||
export class OnMountedMethod {
|
||||
public onMounted () {
|
||||
console.log('public onMounted')
|
||||
}
|
||||
|
||||
onBeforeMount () {
|
||||
console.log('onBeforeMount')
|
||||
}
|
||||
}
|
||||
export default defineNuxtPlugin(() => {
|
||||
//
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user