forked from nuxt/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): tree-shake client and server-only composables (nuxt#5749)
- Loading branch information
Showing
7 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { pathToFileURL } from 'node:url' | ||
import { stripLiteral } from 'strip-literal' | ||
import { parseQuery, parseURL } from 'ufo' | ||
import MagicString from 'magic-string' | ||
import { createUnplugin } from 'unplugin' | ||
|
||
interface TreeShakePluginOptions { | ||
sourcemap?: boolean | ||
treeShake: string[] | ||
} | ||
|
||
export const TreeShakePlugin = createUnplugin((options: TreeShakePluginOptions) => { | ||
const COMPOSABLE_RE = new RegExp(`($|\\s*)(${options.treeShake.join('|')})(?=\\()`, 'g') | ||
|
||
return { | ||
name: 'nuxt:server-treeshake:transfrom', | ||
enforce: 'post', | ||
transformInclude (id) { | ||
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href)) | ||
const { type, macro } = parseQuery(search) | ||
|
||
// vue files | ||
if (pathname.endsWith('.vue') && (type === 'script' || macro || !search)) { | ||
return true | ||
} | ||
|
||
// js files | ||
if (pathname.match(/\.((c|m)?j|t)sx?$/g)) { | ||
return true | ||
} | ||
}, | ||
transform (code, id) { | ||
if (!code.match(COMPOSABLE_RE)) { return } | ||
|
||
const s = new MagicString(code) | ||
const strippedCode = stripLiteral(code) | ||
for (const match of strippedCode.matchAll(COMPOSABLE_RE) || []) { | ||
s.overwrite(match.index, match.index + match[0].length, `(() => {}) || /*#__PURE__*/ false && ${match[0]}`) | ||
} | ||
|
||
if (s.hasChanged()) { | ||
return { | ||
code: s.toString(), | ||
map: options.sourcemap && s.generateMap({ source: id, includeContent: true }) | ||
} | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// @ts-ignore | ||
window.test = true | ||
|
||
export default () => ({ | ||
render: () => 'hi' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<script setup lang="ts"> | ||
onMounted(() => import('~/components/BreaksServer')) | ||
onBeforeMount(() => import('~/components/BreaksServer')) | ||
onBeforeUpdate(() => import('~/components/BreaksServer')) | ||
onRenderTracked(() => import('~/components/BreaksServer')) | ||
onRenderTriggered(() => import('~/components/BreaksServer')) | ||
onActivated(() => import('~/components/BreaksServer')) | ||
onDeactivated(() => import('~/components/BreaksServer')) | ||
onBeforeUnmount(() => import('~/components/BreaksServer')) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
This page should not crash when rendered. | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters