2022-07-07 16:26:04 +00:00
|
|
|
import { pathToFileURL } from 'node:url'
|
|
|
|
import { createUnplugin } from 'unplugin'
|
|
|
|
import { isAbsolute, relative } from 'pathe'
|
2023-01-20 16:17:31 +00:00
|
|
|
import type { Node } from 'estree-walker'
|
2022-07-07 16:26:04 +00:00
|
|
|
import { walk } from 'estree-walker'
|
|
|
|
import MagicString from 'magic-string'
|
|
|
|
import { hash } from 'ohash'
|
|
|
|
import type { CallExpression } from 'estree'
|
2022-09-08 08:55:30 +00:00
|
|
|
import { parseQuery, parseURL } from 'ufo'
|
2023-03-07 21:06:15 +00:00
|
|
|
import escapeRE from 'escape-string-regexp'
|
2023-02-03 11:55:58 +00:00
|
|
|
import { findStaticImports, parseStaticImport } from 'mlly'
|
2022-07-07 16:26:04 +00:00
|
|
|
|
|
|
|
export interface ComposableKeysOptions {
|
2022-08-15 13:40:06 +00:00
|
|
|
sourcemap: boolean
|
|
|
|
rootDir: string
|
2023-03-07 21:06:15 +00:00
|
|
|
composables: Array<{ name: string, argumentLength: number }>
|
2022-07-07 16:26:04 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 09:34:42 +00:00
|
|
|
const stringTypes = ['Literal', 'TemplateLiteral']
|
2022-07-07 16:26:04 +00:00
|
|
|
|
2022-08-15 13:40:06 +00:00
|
|
|
export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptions) => {
|
2023-03-07 21:06:15 +00:00
|
|
|
const composableMeta = Object.fromEntries(options.composables.map(({ name, ...meta }) => [name, meta]))
|
|
|
|
|
|
|
|
const maxLength = Math.max(...options.composables.map(({ argumentLength }) => argumentLength))
|
|
|
|
const keyedFunctions = new Set(options.composables.map(({ name }) => name))
|
|
|
|
const KEYED_FUNCTIONS_RE = new RegExp(`\\b(${[...keyedFunctions].map(f => escapeRE(f)).join('|')})\\b`)
|
|
|
|
|
2022-07-07 16:26:04 +00:00
|
|
|
return {
|
|
|
|
name: 'nuxt:composable-keys',
|
|
|
|
enforce: 'post',
|
2022-09-19 09:34:42 +00:00
|
|
|
transformInclude (id) {
|
2022-09-08 08:55:30 +00:00
|
|
|
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
|
2022-11-02 10:28:41 +00:00
|
|
|
return !pathname.match(/node_modules\/nuxt3?\//) && pathname.match(/\.(m?[jt]sx?|vue)/) && parseQuery(search).type !== 'style' && !parseQuery(search).macro
|
2022-09-19 09:34:42 +00:00
|
|
|
},
|
|
|
|
transform (code, id) {
|
2022-07-07 16:26:04 +00:00
|
|
|
if (!KEYED_FUNCTIONS_RE.test(code)) { return }
|
2022-11-16 00:02:13 +00:00
|
|
|
const { 0: script = code, index: codeIndex = 0 } = code.match(/(?<=<script[^>]*>)[\S\s.]*?(?=<\/script>)/) || { index: 0, 0: code }
|
2022-07-07 16:26:04 +00:00
|
|
|
const s = new MagicString(code)
|
|
|
|
// https://github.com/unjs/unplugin/issues/90
|
2023-02-03 11:55:58 +00:00
|
|
|
let imports: Set<string> | undefined
|
2022-07-29 09:40:04 +00:00
|
|
|
let count = 0
|
2022-07-07 16:26:04 +00:00
|
|
|
const relativeID = isAbsolute(id) ? relative(options.rootDir, id) : id
|
|
|
|
walk(this.parse(script, {
|
|
|
|
sourceType: 'module',
|
|
|
|
ecmaVersion: 'latest'
|
2023-01-20 16:17:31 +00:00
|
|
|
}) as Node, {
|
2022-08-15 13:40:06 +00:00
|
|
|
enter (_node) {
|
|
|
|
if (_node.type !== 'CallExpression' || (_node as CallExpression).callee.type !== 'Identifier') { return }
|
|
|
|
const node: CallExpression = _node as CallExpression
|
2022-09-19 09:34:42 +00:00
|
|
|
const name = 'name' in node.callee && node.callee.name
|
2023-03-07 21:06:15 +00:00
|
|
|
if (!name || !keyedFunctions.has(name) || node.arguments.length >= maxLength) { return }
|
2022-09-19 09:34:42 +00:00
|
|
|
|
2023-02-03 11:55:58 +00:00
|
|
|
imports = imports || detectImportNames(script)
|
|
|
|
if (imports.has(name)) { return }
|
|
|
|
|
2023-03-07 21:06:15 +00:00
|
|
|
const meta = composableMeta[name]
|
|
|
|
|
|
|
|
if (node.arguments.length >= meta.argumentLength) { return }
|
|
|
|
|
2022-09-19 09:34:42 +00:00
|
|
|
switch (name) {
|
|
|
|
case 'useState':
|
2023-03-07 21:06:15 +00:00
|
|
|
if (stringTypes.includes(node.arguments[0]?.type)) { return }
|
2022-09-19 09:34:42 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
case 'useFetch':
|
|
|
|
case 'useLazyFetch':
|
2023-03-07 21:06:15 +00:00
|
|
|
if (stringTypes.includes(node.arguments[1]?.type)) { return }
|
2022-09-19 09:34:42 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
case 'useAsyncData':
|
|
|
|
case 'useLazyAsyncData':
|
2023-03-07 21:06:15 +00:00
|
|
|
if (stringTypes.includes(node.arguments[0]?.type) || stringTypes.includes(node.arguments[node.arguments.length - 1]?.type)) { return }
|
2022-09-19 09:34:42 +00:00
|
|
|
break
|
2022-07-07 16:26:04 +00:00
|
|
|
}
|
2022-09-19 09:34:42 +00:00
|
|
|
|
2022-11-02 10:15:33 +00:00
|
|
|
// TODO: Optimize me (https://github.com/nuxt/framework/pull/8529)
|
|
|
|
const endsWithComma = code.slice(codeIndex + (node as any).start, codeIndex + (node as any).end - 1).trim().endsWith(',')
|
|
|
|
|
2022-09-19 09:34:42 +00:00
|
|
|
s.appendLeft(
|
|
|
|
codeIndex + (node as any).end - 1,
|
2022-11-02 10:15:33 +00:00
|
|
|
(node.arguments.length && !endsWithComma ? ', ' : '') + "'$" + hash(`${relativeID}-${++count}`) + "'"
|
2022-09-19 09:34:42 +00:00
|
|
|
)
|
2022-07-07 16:26:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if (s.hasChanged()) {
|
|
|
|
return {
|
|
|
|
code: s.toString(),
|
2022-08-15 13:40:06 +00:00
|
|
|
map: options.sourcemap
|
|
|
|
? s.generateMap({ source: id, includeContent: true })
|
|
|
|
: undefined
|
2022-07-07 16:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-02-03 11:55:58 +00:00
|
|
|
|
|
|
|
const NUXT_IMPORT_RE = /nuxt|#app|#imports/
|
|
|
|
|
|
|
|
function detectImportNames (code: string) {
|
|
|
|
const imports = findStaticImports(code)
|
|
|
|
const names = new Set<string>()
|
|
|
|
for (const i of imports) {
|
|
|
|
if (NUXT_IMPORT_RE.test(i.specifier)) { continue }
|
|
|
|
const { namedImports, defaultImport, namespacedImport } = parseStaticImport(i)
|
|
|
|
for (const name in namedImports || {}) {
|
|
|
|
names.add(namedImports![name])
|
|
|
|
}
|
|
|
|
if (defaultImport) {
|
|
|
|
names.add(defaultImport)
|
|
|
|
}
|
|
|
|
if (namespacedImport) {
|
|
|
|
names.add(namespacedImport)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|