-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e42e2cd
commit a2de117
Showing
2 changed files
with
58 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
按需引入组件的样式文件。 | ||
|
||
同时支持 vite、webpack、taro。 | ||
支持情况参考 [https://unplugin.unjs.io/guide/](https://unplugin.unjs.io/guide/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |