This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 984
feat(nuxt): tree-shake client and server-only composables #5749
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12ab7a7
feat(nuxt): add plugin to tree-shake composables
danielroe fa303f1
test: add test suite
danielroe e62fb98
test: ignore type error
danielroe 0182613
refactor: use `addVitePlugin` and `addWebpackPlugin`
danielroe 37d6823
fix: return no-op, perf improvements, and preset list
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.