Skip to content

Commit

Permalink
refactor: 修改类型错误
Browse files Browse the repository at this point in the history
  • Loading branch information
oasis-cloud committed Sep 26, 2024
1 parent e42e2cd commit a2de117
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 58 deletions.
2 changes: 1 addition & 1 deletion packages/nutui-auto-import/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

按需引入组件的样式文件。

同时支持 vite、webpack、taro。
支持情况参考 [https://unplugin.unjs.io/guide/](https://unplugin.unjs.io/guide/)
114 changes: 57 additions & 57 deletions packages/nutui-auto-import/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
import { createFilter } from '@rollup/pluginutils'
import { createUnplugin, type UnpluginInstance } from 'unplugin'
import { createUnplugin } from 'unplugin'
import { UnpluginOptions } from 'unplugin/dist'
import { walk } from 'estree-walker'
import MagicString from 'magic-string'
import { getLibraryName, type Options, resolveOptions } from './core/options'
import { getLibraryName, resolveOptions } from './core/options'

export const NutUIAutoImport: UnpluginInstance<Options | undefined, false> =
createUnplugin(
(
rawOptions = {
libraryName: '',
style: true,
}
) => {
const options = resolveOptions(rawOptions)
const filter = createFilter(options.include, options.exclude)
const libraryName = getLibraryName(options)
const name = 'nutui-auto-import'
const unpluginFactory = (
rawOptions = {
libraryName: '',
style: true,
}
): UnpluginOptions => {
const options = resolveOptions(rawOptions)
const filter = createFilter(options.include, options.exclude)
const libraryName = getLibraryName(options)
const name = 'nutui-auto-import'

return {
name,
enforce: options.enforce,
transformInclude(id) {
return filter(id)
},
transform(code, id) {
const ast = this.parse(code)
const magicString = new MagicString(code)
walk(ast as any, {
enter(node, parent) {
if (node.type === 'ImportDeclaration') {
if (node.source.value !== libraryName) return
let absolutePath: string[] = []
try {
const resolvePath = require.resolve(libraryName)
absolutePath = resolvePath.split(libraryName)
} catch (e) {
/* empty */
console.log(`warn: cannot reslove ${libraryName}`)
return {
name,
enforce: options.enforce,
transformInclude(id) {
return filter(id)
},
transform(code, id) {
const ast = this.parse(code)
const magicString = new MagicString(code)
walk(ast as any, {
enter(node, parent) {
if (node.type === 'ImportDeclaration') {
if (node.source.value !== libraryName) return
let absolutePath: string[] = []
try {
const resolvePath = require.resolve(libraryName)
absolutePath = resolvePath.split(libraryName)
} catch (e) {
/* empty */
console.log(`warn: cannot reslove ${libraryName}`)
}
node.specifiers.forEach((specifier) => {
if (specifier.type === 'ImportSpecifier') {
// @ts-ignore
const name = specifier.imported.name.toLowerCase()
if (absolutePath.length === 0 || !absolutePath[0]) return
if (options.style === 'css') {
magicString.prepend(
`import "${absolutePath[0]}${libraryName}/dist/es/packages/${name}/style/css.js";`
)
} else if (options.style) {
magicString.prepend(
`import "${absolutePath[0]}${libraryName}/dist/es/packages/${name}/style/index.js";`
)
}
node.specifiers.forEach((specifier) => {
if (specifier.type === 'ImportSpecifier') {
// @ts-ignore
const name = specifier.imported.name.toLowerCase()
if (absolutePath.length === 0 || !absolutePath[0]) return
if (options.style === 'css') {
magicString.prepend(
`import "${absolutePath[0]}${libraryName}/dist/es/packages/${name}/style/css.js";`
)
} else if (options.style) {
magicString.prepend(
`import "${absolutePath[0]}${libraryName}/dist/es/packages/${name}/style/index.js";`
)
}
}
})
}
},
})
return {
code: magicString.toString(),
map: magicString.generateMap(),
})
}
},
})
return {
code: magicString.toString(),
map: magicString.generateMap(),
}
}
)
},
} as UnpluginOptions
}

export const NutUIAutoImport = createUnplugin(unpluginFactory)

0 comments on commit a2de117

Please sign in to comment.