Skip to content

Commit

Permalink
feat!: remove importx, rollback to jiti (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz authored Feb 7, 2025
1 parent 87b7d2b commit c05fe66
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 92 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"dependencies": {
"@antfu/utils": "^8.1.0",
"defu": "^6.1.4",
"importx": "^0.5.1"
"jiti": "^2.4.2"
},
"devDependencies": {
"@antfu/eslint-config": "^3.14.0",
Expand Down
37 changes: 3 additions & 34 deletions pnpm-lock.yaml

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

44 changes: 18 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function createConfigLoader<T>(options: LoadConfigOptions) {
continue

if (!merge) {
const result = await loadConfigFile(files[0], source, options)
const result = await loadConfigFile(files[0], source)
if (result) {
return {
config: applyDefaults(result.config, defaults),
Expand All @@ -64,7 +64,7 @@ export function createConfigLoader<T>(options: LoadConfigOptions) {
else {
results.push(
...(await Promise.all(
files.map(file => loadConfigFile(file, source, options)),
files.map(file => loadConfigFile(file, source)),
)
).filter(notNullish),
)
Expand Down Expand Up @@ -104,7 +104,6 @@ export async function loadConfig<T>(options: LoadConfigOptions<T>): Promise<Load
async function loadConfigFile<T>(
filepath: string,
source: LoadConfigSource<T>,
options: LoadConfigOptions,
): Promise<LoadConfigResult<T> | undefined> {
let config: T | undefined

Expand Down Expand Up @@ -145,30 +144,23 @@ async function loadConfigFile<T>(
if (typeof parser === 'function') {
config = await parser(filepath)
}
else if (parser === 'require' || parser === 'import') {
config = await import('importx')
.then(async (r) => {
let mod = await r.import(bundleFilepath, {
parentURL: filepath,
cache: false,
loader: source.loader,
fallbackLoaders: source.fallbackLoaders,
loaderOptions: {
...options.importx?.loaderOptions,
...source.importx?.loaderOptions,
jiti: {
interopDefault: true,
...options.importx?.loaderOptions?.jiti,
...source.importx?.loaderOptions?.jiti,
},
},
...options.importx,
...source.importx,
})
dependencies = r.getModuleInfo(mod)?.dependencies
mod = interopDefault(mod)
return interopDefault(mod)
else if (parser === 'import') {
if (process.features.typescript || process.versions.bun || process.versions.deno) {
const defaultImport = await import(filepath)
config = interopDefault(defaultImport)
}
else {
const { createJiti } = await import('jiti')
const jiti = createJiti(import.meta.url, {
fsCache: false,
moduleCache: false,
interopDefault: true,
})
config = interopDefault(await jiti.import(bundleFilepath, { default: true }))
dependencies = Object.values(jiti.cache)
.map(i => i.filename)
.filter(Boolean)
}
}
else if (parser === 'json') {
config = JSON.parse(await read())
Expand Down
32 changes: 1 addition & 31 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { Arrayable, Awaitable } from '@antfu/utils'
import type { ImportxOptions as ImportxOptionsFull, SupportedLoader } from 'importx'

export const defaultExtensions = ['mts', 'cts', 'ts', 'mjs', 'cjs', 'js', 'json', '']

export type BuiltinParsers = 'require' | 'json' | 'import'
export type BuiltinParsers = 'json' | 'import'

export type CustomParser<T> = (filepath: string) => Awaitable<T | undefined>

export type ImportxOptions = Partial<Omit<ImportxOptionsFull, 'parentURL'>>

export interface LoadConfigSource<T = any> {
files: Arrayable<string>

Expand All @@ -24,29 +21,6 @@ export interface LoadConfigSource<T = any> {
*/
parser?: BuiltinParsers | CustomParser<T> | 'auto'

/**
* Importx options for loading TS files.
*/
importx?: ImportxOptions

/**
* Loader for importing JS and TS files.
*
* @default 'auto'
* @deprecated use `importx.loader` instead
*/
loader?: SupportedLoader | 'auto'

/**
* Fallback loaders when the previous loader failed.
*
* Set to `false` to disable fallback.
*
* Default to importx's default.
* @deprecated use `importx.fallbackLoaders` instead
*/
fallbackLoaders?: SupportedLoader[] | false

/**
* Rewrite the config object,
* return nullish value to bypassing loading the file
Expand Down Expand Up @@ -91,10 +65,6 @@ export interface SearchOptions {
export interface LoadConfigOptions<T = any> extends SearchOptions {
sources: Arrayable<LoadConfigSource<T>>
defaults?: T
/**
* Importx options for loading TS files.
*/
importx?: ImportxOptions
}

export interface LoadConfigResult<T> {
Expand Down

0 comments on commit c05fe66

Please sign in to comment.