mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-15 18:34:50 +00:00
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { pathToFileURL } from 'node:url'
|
|
import { createUnplugin } from 'unplugin'
|
|
import { parseQuery, parseURL } from 'ufo'
|
|
import { Unimport } from 'unimport'
|
|
import { AutoImportsOptions } from '@nuxt/schema'
|
|
|
|
export const TransformPlugin = createUnplugin(({ ctx, options }: {ctx: Unimport, options: Partial<AutoImportsOptions> }) => {
|
|
return {
|
|
name: 'nuxt:auto-imports-transform',
|
|
enforce: 'post',
|
|
transformInclude (id) {
|
|
const { pathname, search } = parseURL(decodeURIComponent(pathToFileURL(id).href))
|
|
const { type, macro } = parseQuery(search)
|
|
|
|
const exclude = options.transform?.exclude || [/[\\/]node_modules[\\/]/]
|
|
|
|
// Exclude node_modules by default
|
|
if (exclude.some(pattern => id.match(pattern))) {
|
|
return false
|
|
}
|
|
|
|
// vue files
|
|
if (
|
|
pathname.endsWith('.vue') &&
|
|
(type === 'template' || type === 'script' || macro || !search)
|
|
) {
|
|
return true
|
|
}
|
|
|
|
// js files
|
|
if (pathname.match(/\.((c|m)?j|t)sx?$/g)) {
|
|
return true
|
|
}
|
|
},
|
|
async transform (_code, id) {
|
|
const { code, s } = await ctx.injectImports(_code)
|
|
if (code === _code) {
|
|
return
|
|
}
|
|
return {
|
|
code,
|
|
map: s.generateMap({ source: id, includeContent: true })
|
|
}
|
|
}
|
|
}
|
|
})
|