2022-04-15 15:19:05 +00:00
|
|
|
import { pathToFileURL } from 'node:url'
|
2021-08-10 00:27:23 +00:00
|
|
|
import { createUnplugin } from 'unplugin'
|
|
|
|
import { parseQuery, parseURL } from 'ufo'
|
2022-03-11 08:09:11 +00:00
|
|
|
import { Unimport } from 'unimport'
|
2022-08-23 14:22:11 +00:00
|
|
|
import { ImportsOptions } from '@nuxt/schema'
|
2022-07-21 14:27:23 +00:00
|
|
|
import { normalize } from 'pathe'
|
2021-08-10 00:27:23 +00:00
|
|
|
|
2022-08-23 14:22:11 +00:00
|
|
|
export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: { ctx: Unimport, options: Partial<ImportsOptions>, sourcemap?: boolean }) => {
|
2021-08-10 00:27:23 +00:00
|
|
|
return {
|
2022-08-23 14:22:11 +00:00
|
|
|
name: 'nuxt:imports-transform',
|
2021-08-10 00:27:23 +00:00
|
|
|
enforce: 'post',
|
|
|
|
transformInclude (id) {
|
2022-03-16 13:41:37 +00:00
|
|
|
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
|
2022-07-22 08:28:16 +00:00
|
|
|
const query = parseQuery(search)
|
2021-08-10 00:27:23 +00:00
|
|
|
|
2022-07-21 14:27:23 +00:00
|
|
|
// Included
|
|
|
|
if (options.transform?.include?.some(pattern => id.match(pattern))) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Excluded
|
|
|
|
if (options.transform?.exclude?.some(pattern => id.match(pattern))) {
|
2022-06-10 13:33:16 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-21 14:27:23 +00:00
|
|
|
// Vue files
|
2021-10-13 20:24:13 +00:00
|
|
|
if (
|
2022-07-22 08:28:16 +00:00
|
|
|
id.endsWith('.vue') ||
|
|
|
|
'macro' in query ||
|
|
|
|
('vue' in query && (query.type === 'template' || query.type === 'script' || 'setup' in query))
|
2021-10-13 20:24:13 +00:00
|
|
|
) {
|
2021-08-10 00:27:23 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-21 14:27:23 +00:00
|
|
|
// JavaScript 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
|
|
|
|
}
|
|
|
|
},
|
2022-07-21 14:27:23 +00:00
|
|
|
async transform (code, id) {
|
|
|
|
id = normalize(id)
|
|
|
|
const isNodeModule = id.match(/[\\/]node_modules[\\/]/) && !options.transform?.include?.some(pattern => id.match(pattern))
|
2022-08-23 14:22:11 +00:00
|
|
|
// For modules in node_modules, we only transform `#imports` but not doing imports
|
2022-07-21 14:27:23 +00:00
|
|
|
if (isNodeModule && !code.match(/(['"])#imports\1/)) {
|
2022-03-11 08:09:11 +00:00
|
|
|
return
|
2021-08-10 00:27:23 +00:00
|
|
|
}
|
2022-07-21 14:27:23 +00:00
|
|
|
|
2022-08-24 08:44:38 +00:00
|
|
|
const { s } = await ctx.injectImports(code, id, { autoImport: options.autoImport && !isNodeModule })
|
2022-07-21 14:27:23 +00:00
|
|
|
if (s.hasChanged()) {
|
|
|
|
return {
|
|
|
|
code: s.toString(),
|
2022-08-22 10:12:02 +00:00
|
|
|
map: sourcemap
|
|
|
|
? s.generateMap({ source: id, includeContent: true })
|
|
|
|
: undefined
|
2022-07-21 14:27:23 +00:00
|
|
|
}
|
2021-08-30 11:55:57 +00:00
|
|
|
}
|
2021-08-10 00:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|