Skip to content

Commit

Permalink
feat!: Now use in resolvers options instead of imports option.
Browse files Browse the repository at this point in the history
  • Loading branch information
s3xysteak committed May 3, 2024
1 parent 7660d28 commit 0dace9b
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 32 deletions.
42 changes: 34 additions & 8 deletions src/core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,23 @@ ${content.substring(lastComment).trim()}
const __UnExportList = ${JSON.stringify(exportList)} as const
/**
* @returns Use in \`imports\` option of unplugin-auto-import.
* @returns - Call in \`resolvers\` option of \`unplugin-auto-import\`.
*/
export function ${rename}(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>): Record<string, (string | [string, string])[]> {
return {
'${pkgName}': __UnExportList.map(v => map && map[v] ? [v, map[v]] as [string, string] : v),
export function ${rename}(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>) {
return (name: string) => {
if (!__UnExportList.includes(name as any))
return
return map && (map as any)[name]
? {
name,
as: (map as any)[name],
from: '${pkgName}',
}
: {
name,
from: '${pkgName}',
}
}
}
`
Expand All @@ -108,12 +120,26 @@ const __UnExportList = /** @type {const} */ (${JSON.stringify(exportList)})
/**
* @param {Partial<{ [K in typeof __UnExportList[number]]: string }>} [map]
* @returns {Record<string, (string | [string, string])[]>} Use in \`imports\` option of \`unplugin-auto-import\`.
*/
* @returns - Call in \`resolvers\` option of \`unplugin-auto-import\`.
*/
export function ${rename}(map) {
return {
'${pkgName}': __UnExportList.map(v => map && map[v] ? [v, map[v]] : v),
/** @param {string} name */
const func = (name) => {
if (!__UnExportList.includes(name))
return
return map && map[name]
? {
name,
as: map[name],
from: '${pkgName}',
}
: {
name,
from: '${pkgName}',
}
}
return func
}
`
const JS = getTemplate(rawJs)
Expand Down
62 changes: 50 additions & 12 deletions test/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,23 @@ export * from '@s3xysteak/utils'
const __UnExportList = ${JSON.stringify(exportList)} as const
/**
* @returns Use in \`imports\` option of unplugin-auto-import.
* @returns - Call in \`resolvers\` option of \`unplugin-auto-import\`.
*/
export function autoImport(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>): Record<string, (string | [string, string])[]> {
return {
'unplugin-export-collector': __UnExportList.map(v => map && map[v] ? [v, map[v]] as [string, string] : v),
export function autoImport(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>) {
return (name: string) => {
if (!__UnExportList.includes(name as any))
return
return map && (map as any)[name]
? {
name,
as: (map as any)[name],
from: 'unplugin-export-collector',
}
: {
name,
from: 'unplugin-export-collector',
}
}
}
Expand Down Expand Up @@ -65,12 +77,26 @@ const __UnExportList = /** @type {const} */ (${JSON.stringify(exportList)})
/**
* @param {Partial<{ [K in typeof __UnExportList[number]]: string }>} [map]
* @returns {Record<string, (string | [string, string])[]>} Use in \`imports\` option of \`unplugin-auto-import\`.
*/
* @returns - Call in \`resolvers\` option of \`unplugin-auto-import\`.
*/
export function autoImport(map) {
return {
'unplugin-export-collector': __UnExportList.map(v => map && map[v] ? [v, map[v]] : v),
/** @param {string} name */
const func = (name) => {
if (!__UnExportList.includes(name))
return
return map && map[name]
? {
name,
as: map[name],
from: 'unplugin-export-collector',
}
: {
name,
from: 'unplugin-export-collector',
}
}
return func
}
// --- Auto-Generated By Unplugin-Export-Collector ---
Expand All @@ -91,11 +117,23 @@ export function autoImport(map) {
const __UnExportList = ${JSON.stringify(exportList)} as const
/**
* @returns Use in \`imports\` option of unplugin-auto-import.
* @returns - Call in \`resolvers\` option of \`unplugin-auto-import\`.
*/
export function autoImport(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>): Record<string, (string | [string, string])[]> {
return {
'unplugin-export-collector': __UnExportList.map(v => map && map[v] ? [v, map[v]] as [string, string] : v),
export function autoImport(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>) {
return (name: string) => {
if (!__UnExportList.includes(name as any))
return
return map && (map as any)[name]
? {
name,
as: (map as any)[name],
from: 'unplugin-export-collector',
}
: {
name,
from: 'unplugin-export-collector',
}
}
}
Expand Down
22 changes: 18 additions & 4 deletions test/parser-lab/generatorTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,26 @@ const __UnExportList = /** @type {const} */ (["ClassIndex","custom","fRe","func1

/**
* @param {Partial<{ [K in typeof __UnExportList[number]]: string }>} [map]
* @returns {Record<string, (string | [string, string])[]>} Use in `imports` option of `unplugin-auto-import`.
*/
* @returns - Call in `resolvers` option of `unplugin-auto-import`.
*/
export function autoImport(map) {
return {
'unplugin-export-collector': __UnExportList.map(v => map && map[v] ? [v, map[v]] : v),
/** @param {string} name */
const func = (name) => {
if (!__UnExportList.includes(name))
return

return map && map[name]
? {
name,
as: map[name],
from: 'unplugin-export-collector',
}
: {
name,
from: 'unplugin-export-collector',
}
}
return func
}

// --- Auto-Generated By Unplugin-Export-Collector ---
20 changes: 16 additions & 4 deletions test/parser-lab/generatorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ export * from '@s3xysteak/utils'
const __UnExportList = ["ClassIndex","custom","fRe","func1","func2","func3","funcIndex","getThree","two"] as const

/**
* @returns Use in `imports` option of unplugin-auto-import.
* @returns - Call in `resolvers` option of `unplugin-auto-import`.
*/
export function autoImport(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>): Record<string, (string | [string, string])[]> {
return {
'unplugin-export-collector': __UnExportList.map(v => map && map[v] ? [v, map[v]] as [string, string] : v),
export function autoImport(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>) {
return (name: string) => {
if (!__UnExportList.includes(name as any))
return

return map && (map as any)[name]
? {
name,
as: (map as any)[name],
from: 'unplugin-export-collector',
}
: {
name,
from: 'unplugin-export-collector',
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions test/parser-lab/generatorTestWithWriteTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@
const __UnExportList = ["ClassIndex","custom","fRe","func1","func2","func3","funcIndex","getThree","two"] as const

/**
* @returns Use in `imports` option of unplugin-auto-import.
* @returns - Call in `resolvers` option of `unplugin-auto-import`.
*/
export function autoImport(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>): Record<string, (string | [string, string])[]> {
return {
'unplugin-export-collector': __UnExportList.map(v => map && map[v] ? [v, map[v]] as [string, string] : v),
export function autoImport(map?: Partial<{ [K in typeof __UnExportList[number]]: string }>) {
return (name: string) => {
if (!__UnExportList.includes(name as any))
return

return map && (map as any)[name]
? {
name,
as: (map as any)[name],
from: 'unplugin-export-collector',
}
: {
name,
from: 'unplugin-export-collector',
}
}
}

Expand Down

0 comments on commit 0dace9b

Please sign in to comment.