This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): automatically generate unique keys for keyed composables (#…
…4955) Co-authored-by: Pooya Parsa <pyapar@gmail.com>
- Loading branch information
Showing
19 changed files
with
255 additions
and
39 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
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
2 changes: 1 addition & 1 deletion
2
examples/composables/use-async-data/components/CounterExample.vue
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
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
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,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 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.