2021-08-10 00:27:23 +00:00
|
|
|
import { createUnplugin } from 'unplugin'
|
|
|
|
import { parseQuery, parseURL } from 'ufo'
|
|
|
|
import { IdentifierMap } from './types'
|
|
|
|
|
2021-08-30 11:55:57 +00:00
|
|
|
const excludeRE = [
|
2021-08-10 00:27:23 +00:00
|
|
|
// imported from other module
|
2021-10-12 12:24:43 +00:00
|
|
|
/\bimport\s*([\w_$]*?),?\s*(?:\{([\s\S]*?)\})?\s*from\b/g,
|
2021-08-10 00:27:23 +00:00
|
|
|
// defined as function
|
2021-10-12 12:24:43 +00:00
|
|
|
/\bfunction\s*([\w_$]+?)\s*\(/g,
|
2021-08-10 00:27:23 +00:00
|
|
|
// defined as local variable
|
2021-10-12 12:24:43 +00:00
|
|
|
/\b(?:const|let|var)\s+?(\[[\s\S]*?\]|\{[\s\S]*?\}|[\s\S]+?)\s*?[=;\n]/g
|
2021-08-10 00:27:23 +00:00
|
|
|
]
|
|
|
|
|
2021-10-12 12:24:43 +00:00
|
|
|
const importAsRE = /^.*\sas\s+/
|
|
|
|
const seperatorRE = /[,[\]{}\n]/g
|
2021-08-30 11:55:57 +00:00
|
|
|
const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm
|
2021-10-02 16:59:32 +00:00
|
|
|
const singlelineCommentsRE = /^\s*\/\/.*$/gm
|
2021-08-30 11:55:57 +00:00
|
|
|
|
|
|
|
function stripeComments (code: string) {
|
|
|
|
return code
|
|
|
|
.replace(multilineCommentsRE, '')
|
|
|
|
.replace(singlelineCommentsRE, '')
|
|
|
|
}
|
|
|
|
|
2021-08-24 07:49:03 +00:00
|
|
|
export const TransformPlugin = createUnplugin((map: IdentifierMap) => {
|
2021-08-30 11:55:57 +00:00
|
|
|
const matchRE = new RegExp(`\\b(${Object.keys(map).join('|')})\\b`, 'g')
|
2021-08-10 00:27:23 +00:00
|
|
|
|
|
|
|
return {
|
2021-10-11 08:07:27 +00:00
|
|
|
name: 'nuxt-auto-imports-transform',
|
2021-08-10 00:27:23 +00:00
|
|
|
enforce: 'post',
|
|
|
|
transformInclude (id) {
|
|
|
|
const { pathname, search } = parseURL(id)
|
2021-10-13 20:24:13 +00:00
|
|
|
const { type } = parseQuery(search)
|
2021-08-10 00:27:23 +00:00
|
|
|
|
|
|
|
if (id.includes('node_modules')) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// vue files
|
2021-10-13 20:24:13 +00:00
|
|
|
if (
|
|
|
|
pathname.endsWith('.vue') &&
|
|
|
|
(type === 'template' || type === 'script' || !search)
|
|
|
|
) {
|
2021-08-10 00:27:23 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// js files
|
2021-10-13 21:03:36 +00:00
|
|
|
if (pathname.match(/\.((c|m)?j|t)sx?$/g)) {
|
2021-08-10 00:27:23 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
transform (code) {
|
2021-08-30 11:55:57 +00:00
|
|
|
// strip comments so we don't match on them
|
|
|
|
const withoutComment = stripeComments(code)
|
|
|
|
|
2021-08-10 00:27:23 +00:00
|
|
|
// find all possible injection
|
2021-08-30 11:55:57 +00:00
|
|
|
const matched = new Set(Array.from(withoutComment.matchAll(matchRE)).map(i => i[1]))
|
2021-08-10 00:27:23 +00:00
|
|
|
|
|
|
|
// remove those already defined
|
2021-08-30 11:55:57 +00:00
|
|
|
for (const regex of excludeRE) {
|
|
|
|
Array.from(withoutComment.matchAll(regex))
|
2021-10-12 12:24:43 +00:00
|
|
|
.flatMap(i => [
|
|
|
|
...(i[1]?.split(seperatorRE) || []),
|
|
|
|
...(i[2]?.split(seperatorRE) || [])
|
|
|
|
])
|
|
|
|
.map(i => i.replace(importAsRE, '').trim())
|
|
|
|
.filter(Boolean)
|
|
|
|
.forEach(i => matched.delete(i))
|
2021-08-10 00:27:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 11:55:57 +00:00
|
|
|
if (!matched.size) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-08-10 00:27:23 +00:00
|
|
|
const modules: Record<string, string[]> = {}
|
|
|
|
|
|
|
|
// group by module name
|
|
|
|
Array.from(matched).forEach((name) => {
|
|
|
|
const moduleName = map[name]!
|
|
|
|
if (!modules[moduleName]) {
|
|
|
|
modules[moduleName] = []
|
|
|
|
}
|
|
|
|
modules[moduleName].push(name)
|
|
|
|
})
|
|
|
|
|
2021-10-02 16:59:32 +00:00
|
|
|
// Needed for webpack4/bridge support
|
|
|
|
const isCJSContext = code.includes('require(')
|
|
|
|
|
2021-08-10 00:27:23 +00:00
|
|
|
// stringify import
|
2021-10-02 16:59:32 +00:00
|
|
|
const imports = !isCJSContext
|
|
|
|
? Object.entries(modules)
|
|
|
|
.map(([moduleName, names]) => `import { ${names.join(',')} } from '${moduleName}';`)
|
|
|
|
.join('')
|
|
|
|
: Object.entries(modules)
|
|
|
|
.map(([moduleName, names]) => `const { ${names.join(',')} } = require('${moduleName}');`)
|
|
|
|
.join('')
|
2021-08-10 00:27:23 +00:00
|
|
|
|
|
|
|
return imports + code
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|