-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some upstream lib failed generate invalid dts, remove the any in the …
…future
- Loading branch information
Showing
14 changed files
with
147 additions
and
152 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "unplugin-iconfont", | ||
"type": "module", | ||
"version": "1.2.1-beta.6", | ||
"version": "1.2.1-beta.5", | ||
"packageManager": "[email protected]", | ||
"description": "", | ||
"license": "MIT", | ||
|
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
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
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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import type { IndexHtmlTransformResult } from 'vite' | ||
import type { Options } from '../types' | ||
import { loadConfig } from 'unconfig' | ||
import { createUnplugin } from 'unplugin' | ||
import { downloadSymbol, generateDts, generateJson, getURLContent, injectHtml } from './utils' | ||
|
||
export const defineConfig = (configs: Options): Options => configs | ||
|
||
let frameConfig: any | ||
|
||
const PLUGIN_NAME = 'unplugin-iconfont' | ||
|
||
export default createUnplugin<Options | undefined>((options, meta) => { | ||
// const filter = createFilter( | ||
// options?.configFile || [/iconfont\.config\.(c|m)?js|ts$/], | ||
// ) | ||
|
||
return { | ||
name: PLUGIN_NAME, | ||
enforce: 'post', | ||
async transform() { | ||
const isVite = meta.framework === 'vite' | ||
const isRspack = meta.framework === 'rspack' | ||
|
||
let config = Array.isArray(options) ? options : options ? [options] : [] as any[] | ||
config = (await loadConfig({ | ||
sources: [ | ||
{ | ||
files: 'iconfont.config', | ||
extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs', 'json', ''], | ||
rewrite(config) { | ||
return (config as any).default | ||
}, | ||
}, | ||
], | ||
defaults: config, | ||
})).config | ||
if (!config.length || !config.every(c => c.url || c.configFile)) | ||
this.error(`Options url parameter is required`) | ||
|
||
config = config.filter(c => c.url).map((c) => { | ||
const urlArr = c.url.split(/\//g) | ||
return Object.assign( | ||
{ | ||
url: '', | ||
fileName: urlArr[urlArr.length - 1], | ||
filePath: 'iconfonts', | ||
inject: true, | ||
dts: false, | ||
iconJson: false, | ||
prefix: '', | ||
separator: '-', | ||
trimStart: '', | ||
iconifyJson: false, | ||
}, | ||
c, | ||
) | ||
}) | ||
|
||
for (const [i, c] of config.entries()) { | ||
const url = c.url | ||
let URL_CONTENT = await getURLContent(url) | ||
|
||
if (c.trimStart) { | ||
URL_CONTENT = URL_CONTENT.replace( | ||
new RegExp(`id="${c.trimStart}`, 'g'), | ||
'id="', | ||
) | ||
} | ||
|
||
if (c.prefix) { | ||
URL_CONTENT = URL_CONTENT.replace( | ||
/<symbol id="/g, | ||
`<symbol id="${c.prefix}${c.separator}`, | ||
) | ||
} | ||
|
||
const iconList = URL_CONTENT.match(/(?<=id=").+?(?=")/g) || [] | ||
|
||
if (c.iconJson) | ||
await generateJson(c) | ||
|
||
if (c.dts) | ||
generateDts(c, i, config, iconList) | ||
|
||
if (c.inject) { | ||
if (isVite) { | ||
const injectArr: IndexHtmlTransformResult = [] | ||
|
||
injectHtml(c.url, frameConfig, c, injectArr) | ||
} | ||
} | ||
|
||
if (c.fileName) { | ||
let publicDir: string = '' | ||
|
||
if (isVite) | ||
publicDir = frameConfig.publicDir | ||
|
||
if (isRspack) { | ||
publicDir = frameConfig.output.publicPath === 'auto' ? frameConfig.output.path : frameConfig.output.publicPath | ||
} | ||
|
||
if (publicDir) | ||
downloadSymbol(publicDir, c, URL_CONTENT) | ||
} | ||
} | ||
|
||
return null | ||
}, | ||
vite: { | ||
configResolved(config) { | ||
frameConfig = config | ||
}, | ||
}, | ||
webpack(compiler) { | ||
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { | ||
frameConfig = compilation.options | ||
}) | ||
}, | ||
rspack(compiler) { | ||
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { | ||
frameConfig = compilation.options | ||
}) | ||
}, | ||
} | ||
}) |
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
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,3 +1,3 @@ | ||
import unplugin from '.' | ||
import unplugin, { type Options } from '.' | ||
|
||
export default unplugin.esbuild | ||
export default unplugin.esbuild as (options?: Options) => any |
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,3 +1,3 @@ | ||
import unplugin from '.' | ||
import unplugin, { type Options } from '.' | ||
|
||
export default unplugin.farm | ||
export default unplugin.farm as (options?: Options) => any |
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,132 +1,2 @@ | ||
import type { UnpluginFactory } from 'unplugin' | ||
import type { IndexHtmlTransformResult } from 'vite' | ||
import type { Options } from './types' | ||
import { loadConfig } from 'unconfig' | ||
import { createUnplugin } from 'unplugin' | ||
import { downloadSymbol, generateDts, generateJson, getURLContent, injectHtml } from './utils' | ||
|
||
export const defineConfig = (configs: Options): Options => configs | ||
|
||
let frameConfig: any | ||
|
||
const PLUGIN_NAME = 'unplugin-iconfont' | ||
|
||
export const unpluginFactory: UnpluginFactory<Options | undefined> = (options, meta) => { | ||
// const filter = createFilter( | ||
// options?.configFile || [/iconfont\.config\.(c|m)?js|ts$/], | ||
// ) | ||
|
||
return { | ||
name: PLUGIN_NAME, | ||
enforce: 'post', | ||
async transform() { | ||
const isVite = meta.framework === 'vite' | ||
const isRspack = meta.framework === 'rspack' | ||
|
||
let config = Array.isArray(options) ? options : options ? [options] : [] as any[] | ||
config = (await loadConfig({ | ||
sources: [ | ||
{ | ||
files: 'iconfont.config', | ||
extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs', 'json', ''], | ||
rewrite(config) { | ||
return (config as any).default | ||
}, | ||
}, | ||
], | ||
defaults: config, | ||
})).config | ||
if (!config.length || !config.every(c => c.url || c.configFile)) | ||
this.error(`Options url parameter is required`) | ||
|
||
config = config.filter(c => c.url).map((c) => { | ||
const urlArr = c.url.split(/\//g) | ||
return Object.assign( | ||
{ | ||
url: '', | ||
fileName: urlArr[urlArr.length - 1], | ||
filePath: 'iconfonts', | ||
inject: true, | ||
dts: false, | ||
iconJson: false, | ||
prefix: '', | ||
separator: '-', | ||
trimStart: '', | ||
iconifyJson: false, | ||
}, | ||
c, | ||
) | ||
}) | ||
|
||
for (const [i, c] of config.entries()) { | ||
const url = c.url | ||
let URL_CONTENT = await getURLContent(url) | ||
|
||
if (c.trimStart) { | ||
URL_CONTENT = URL_CONTENT.replace( | ||
new RegExp(`id="${c.trimStart}`, 'g'), | ||
'id="', | ||
) | ||
} | ||
|
||
if (c.prefix) { | ||
URL_CONTENT = URL_CONTENT.replace( | ||
/<symbol id="/g, | ||
`<symbol id="${c.prefix}${c.separator}`, | ||
) | ||
} | ||
|
||
const iconList = URL_CONTENT.match(/(?<=id=").+?(?=")/g) || [] | ||
|
||
if (c.iconJson) | ||
await generateJson(c) | ||
|
||
if (c.dts) | ||
generateDts(c, i, config, iconList) | ||
|
||
if (c.inject) { | ||
if (isVite) { | ||
const injectArr: IndexHtmlTransformResult = [] | ||
|
||
injectHtml(c.url, frameConfig, c, injectArr) | ||
} | ||
} | ||
|
||
if (c.fileName) { | ||
let publicDir: string = '' | ||
|
||
if (isVite) | ||
publicDir = frameConfig.publicDir | ||
|
||
if (isRspack) { | ||
publicDir = frameConfig.output.publicPath === 'auto' ? frameConfig.output.path : frameConfig.output.publicPath | ||
} | ||
|
||
if (publicDir) | ||
downloadSymbol(publicDir, c, URL_CONTENT) | ||
} | ||
} | ||
|
||
return null | ||
}, | ||
vite: { | ||
configResolved(config) { | ||
frameConfig = config | ||
}, | ||
}, | ||
webpack(compiler) { | ||
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { | ||
frameConfig = compilation.options | ||
}) | ||
}, | ||
rspack(compiler) { | ||
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { | ||
frameConfig = compilation.options | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
export const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory) | ||
|
||
export default unplugin | ||
export { default } from './core/unplugin' | ||
export * from './types' |
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
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,3 +1,3 @@ | ||
import unplugin from '.' | ||
import unplugin, { type Options } from '.' | ||
|
||
export default unplugin.rollup | ||
export default unplugin.rollup as (options?: Options) => any |
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,3 +1,3 @@ | ||
import unplugin from '.' | ||
import unplugin, { type Options } from '.' | ||
|
||
export default unplugin.rspack | ||
export default unplugin.rspack as (options?: Options) => any |
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,3 +1,3 @@ | ||
import unplugin from '.' | ||
import unplugin, { type Options } from '.' | ||
|
||
export default unplugin.vite | ||
export default unplugin.vite as (options?: Options) => any |
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,3 +1,3 @@ | ||
import unplugin from '.' | ||
import unplugin, { type Options } from '.' | ||
|
||
export default unplugin.webpack | ||
export default unplugin.webpack as (options?: Options) => any |
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