Skip to content

Commit

Permalink
some upstream lib failed generate invalid dts, remove the any in the …
Browse files Browse the repository at this point in the history
…future
  • Loading branch information
imal1 committed Sep 19, 2024
1 parent 3ac3653 commit 4410c38
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 152 deletions.
2 changes: 1 addition & 1 deletion package.json
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",
Expand Down
2 changes: 1 addition & 1 deletion playground/iconfont.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from '../src'
import { defineConfig } from '../src/core/unplugin'

export default defineConfig([
{
Expand Down
4 changes: 1 addition & 3 deletions src/astro.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { Options } from './types'

import unplugin from '.'
import unplugin, { type Options } from '.'

export default (options: Options): any => ({
name: 'unplugin-starter',
Expand Down
127 changes: 127 additions & 0 deletions src/core/unplugin.ts
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
})
},
}
})
2 changes: 1 addition & 1 deletion src/utils.ts → src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IconfontConfig } from './types'
import type { IconfontConfig } from '../types'
import { existsSync, promises as fs, type PathLike } from 'node:fs'
import { dirname, join } from 'node:path'
import { Script } from 'node:vm'
Expand Down
4 changes: 2 additions & 2 deletions src/esbuild.ts
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
4 changes: 2 additions & 2 deletions src/farm.ts
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
134 changes: 2 additions & 132 deletions src/index.ts
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'
2 changes: 1 addition & 1 deletion src/nuxt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Options } from './types'
import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'
import vite from './vite'
import webpack from './webpack'
import type { Options } from './types'
import '@nuxt/schema'

export interface ModuleOptions extends Options {
Expand Down
4 changes: 2 additions & 2 deletions src/rollup.ts
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
4 changes: 2 additions & 2 deletions src/rspack.ts
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
4 changes: 2 additions & 2 deletions src/vite.ts
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
4 changes: 2 additions & 2 deletions src/webpack.ts
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
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Options } from 'tsup'

export default <Options>{
entryPoints: [
entry: [
'src/*.ts',
],
clean: true,
Expand Down

0 comments on commit 4410c38

Please sign in to comment.