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): automatically generate unique keys for keyed composables #4955
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
435a331
feat(nuxt): automatically generate unique keys for keyed composables
danielroe fcbb164
docs: update and fix typings
danielroe ca20190
fix: update lazy fetch types
danielroe 8f709e3
fix: add acorn and estree-walker to dependencies
danielroe be98e92
refactor: rename magic keys -> composable keys
danielroe cf0e2da
perf: use regexp for early return
danielroe 1c1bac4
fix: normalise id into relative path
danielroe 52d531d
fix: improve readability
danielroe ae61515
perf: use hash map for generating short keys
danielroe 231c5a5
fix: remove cjs from transformed extensions
danielroe bf02262
docs: apply suggestions
danielroe f818c8c
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe 4b20d10
fix: remove changed files
danielroe 88cf9f0
docs: update counter example
danielroe da9cf27
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe 040acb2
refactor: simplify handling of fallback
danielroe bf63061
refactor: use `ohash` for hashing
danielroe 92bd6dc
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe 585c284
Merge branch 'main' into feat/magic-keys
danielroe 4bb6cfa
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe 9ced5f2
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe b3bde78
Merge remote-tracking branch 'origin/main' into feat/magic-keys
danielroe eab5ec3
fix playground,, asyncData with key, improve runtime checks and smallβ¦
pi0 502cc11
fix lint issue
pi0 f116ff6
fix: useState with explicit key
pi0 faab8b1
refactor: use unplugin's built-in parser
danielroe 5964216
chore: update lockfile
danielroe 4c42bc6
add workaround for autogenerated key
pi0 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
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
2 changes: 1 addition & 1 deletion
2
examples/composables/use-async-data/components/CounterExample.vue
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
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
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,55 @@ | ||
| import { pathToFileURL } from 'node:url' | ||
| import { createUnplugin } from 'unplugin' | ||
| import { isAbsolute, relative } from 'pathe' | ||
| import { walk } from 'estree-walker' | ||
| import MagicString from 'magic-string' | ||
| import { hash } from 'ohash' | ||
| import type { CallExpression } from 'estree' | ||
| import { parseURL } from 'ufo' | ||
|
|
||
| export interface ComposableKeysOptions { | ||
| sourcemap?: boolean | ||
| rootDir?: string | ||
| } | ||
|
|
||
| const keyedFunctions = [ | ||
| 'useState', 'useFetch', 'useAsyncData', 'useLazyAsyncData', 'useLazyFetch' | ||
| ] | ||
| const KEYED_FUNCTIONS_RE = new RegExp(`(${keyedFunctions.join('|')})`) | ||
|
|
||
| export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptions = {}) => { | ||
| return { | ||
| name: 'nuxt:composable-keys', | ||
| enforce: 'post', | ||
| transform (code, id) { | ||
| const { pathname } = parseURL(decodeURIComponent(pathToFileURL(id).href)) | ||
| if (!pathname.match(/\.(m?[jt]sx?|vue)/)) { return } | ||
| if (!KEYED_FUNCTIONS_RE.test(code)) { return } | ||
| const { 0: script = code, index: codeIndex = 0 } = code.match(/(?<=<script[^>]*>)[\S\s.]*?(?=<\/script>)/) || [] | ||
| const s = new MagicString(code) | ||
| // https://github.com/unjs/unplugin/issues/90 | ||
| const relativeID = isAbsolute(id) ? relative(options.rootDir, id) : id | ||
| walk(this.parse(script, { | ||
| sourceType: 'module', | ||
| ecmaVersion: 'latest' | ||
| }), { | ||
| enter (node: CallExpression) { | ||
| if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier') { return } | ||
| if (keyedFunctions.includes(node.callee.name) && node.arguments.length < 4) { | ||
| const end = (node as any).end | ||
| s.appendLeft( | ||
| codeIndex + end - 1, | ||
| (node.arguments.length ? ', ' : '') + "'$" + hash(`${relativeID}-${codeIndex + end}`) + "'" | ||
| ) | ||
| } | ||
| } | ||
| }) | ||
| 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
Oops, something went wrong.
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.