Skip to content

Commit

Permalink
feat: try unplugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
s3xysteak committed Mar 19, 2024
1 parent 19345d4 commit a91a62c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@
"utils"
],
"exports": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./vite": {
"types": "./dist/vite.d.ts",
"import": "./dist/vite.mjs",
"require": "./dist/vite.cjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -51,6 +58,7 @@
"pathe": "^1.1.2",
"typescript": "^5.4.2",
"unbuild": "^2.0.0",
"unplugin": "^1.10.0",
"vitest": "^1.4.0"
}
}
69 changes: 69 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/core/unplugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import process from 'node:process'
import type { UnpluginFactory } from 'unplugin'
import { createUnplugin } from 'unplugin'
import { resolve } from 'pathe'
import { addExtension } from '../utils/general'
import { type ExpGeneratorOptions, expGeneratorData } from './generator'

export interface UnpluginFactoryOptions extends ExpGeneratorOptions {
entries: string[]
}

export const unpluginFactory: UnpluginFactory<Partial<UnpluginFactoryOptions> | undefined> = (options = {}) => {
const {
entries = ['./src/index.ts'],
} = options
const _entries = entries.map(addExtension).map(entry => resolve(process.cwd(), entry))

return {
name: 'unplugin-auto-import-generator',
transformInclude: id => _entries.includes(id),
transform: async (_, id) => (await expGeneratorData(id, options)).data,
}
}

const unplugin = /* #__PURE__ */ createUnplugin(unpluginFactory)

export default unplugin
3 changes: 3 additions & 0 deletions src/vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import unplugin from './core/unplugin'

export default unplugin.vite

0 comments on commit a91a62c

Please sign in to comment.