-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
283 additions
and
22 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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"name": "eslint-import-resolver-oxc", | ||
"type": "module", | ||
"version": "0.8.0", | ||
"packageManager": "[email protected].2", | ||
"packageManager": "[email protected].3", | ||
"description": "A simply wrapped `oxc-resolver` for `eslint-plugin-import-x` and `eslint-plugin-import`", | ||
"author": "Vida Xie<https://github.com/9romise>", | ||
"license": "MIT", | ||
|
@@ -45,14 +45,18 @@ | |
"peerDependencies": { | ||
"eslint": "*", | ||
"eslint-plugin-import": "*", | ||
"eslint-plugin-import-x": "*" | ||
"eslint-plugin-import-x": "*", | ||
"vite": "*" | ||
}, | ||
"peerDependenciesMeta": { | ||
"eslint-plugin-import": { | ||
"optional": true | ||
}, | ||
"eslint-plugin-import-x": { | ||
"optional": true | ||
}, | ||
"vite": { | ||
"optional": true | ||
} | ||
}, | ||
"dependencies": { | ||
|
@@ -61,6 +65,7 @@ | |
"devDependencies": { | ||
"@types/node": "^22.10.4", | ||
"@vida0905/eslint-config": "^1.2.0", | ||
"es-toolkit": "^1.31.0", | ||
"eslint": "^9.17.0", | ||
"eslint-vitest-rule-tester": "^0.7.1", | ||
"lint-staged": "^15.3.0", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,51 @@ | ||
import type { BundlerConfigTransformer, BundlerOption, SupportedBundler } from '@/typings' | ||
import { basename, resolve } from 'node:path' | ||
import { cwd } from 'node:process' | ||
import { detectFile, log } from '@/utils' | ||
import vite from './vite' | ||
|
||
const bundlerConfig: Record<SupportedBundler, BundlerConfigTransformer> = { | ||
vite, | ||
} | ||
|
||
export async function getBundlerConfig(options?: BundlerOption | null) { | ||
if (options == null) | ||
return {} | ||
|
||
if (typeof options === 'string') { | ||
options = { | ||
path: options, | ||
} | ||
} | ||
|
||
let { type, path } = options | ||
|
||
if (!type && !path) { | ||
log('either `type` or `path` must be specified') | ||
return {} | ||
} | ||
|
||
if (!type) { | ||
type = basename(path!).split('.')[0] as SupportedBundler | ||
} | ||
|
||
const config = bundlerConfig[type] | ||
|
||
if (!config) { | ||
log(`"${type}" config is not supported now`) | ||
return {} | ||
} | ||
|
||
if (!path) { | ||
const { filename, extensions } = config | ||
path = detectFile(extensions.map((ext) => `${filename}.${ext}`)) | ||
} | ||
|
||
if (!path) { | ||
log(`cannot find "${type}" config`) | ||
return {} | ||
} | ||
|
||
path = resolve(cwd(), path) | ||
return await config.transformConfig(path) | ||
} |
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,42 @@ | ||
import type { BundlerConfigTransformer } from '@/typings' | ||
import type { NapiResolveOptions } from 'oxc-resolver' | ||
import type { Alias } from 'vite' | ||
import { loadConfigFromFile } from 'vite' | ||
|
||
export async function transformViteConfig(path: string): Promise<NapiResolveOptions> { | ||
const { config } = await loadConfigFromFile({ command: 'build', mode: 'production' }, path) || {} | ||
|
||
if (!config?.resolve) | ||
return {} | ||
|
||
const { alias } = config.resolve | ||
|
||
const resolvedAlias: NapiResolveOptions['alias'] = {} | ||
|
||
if (Array.isArray(alias)) { | ||
const _alias = alias as Alias[] | ||
_alias.forEach(({ find, replacement }) => { | ||
if (typeof find === 'string') | ||
resolvedAlias[find] = [replacement] | ||
}) | ||
} else if (alias) { | ||
const _alias = alias as { [find: string]: string } | ||
for (const find in _alias) { | ||
resolvedAlias[find] = [_alias[find]] | ||
} | ||
} | ||
|
||
return { | ||
alias: resolvedAlias, | ||
mainFields: config.resolve.mainFields, | ||
extensions: config.resolve.extensions, | ||
conditionNames: config.resolve.conditions, | ||
symlinks: config.resolve.preserveSymlinks, | ||
} | ||
} | ||
|
||
export default { | ||
filename: 'vite.config', | ||
extensions: ['ts', 'js', 'mts', 'mjs', 'cts', 'cjs'], | ||
transformConfig: transformViteConfig, | ||
} as BundlerConfigTransformer |
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,25 @@ | ||
import type { NapiResolveOptions } from 'oxc-resolver' | ||
|
||
export interface ImportResolver { | ||
interfaceVersion: 3 | ||
name: string | ||
resolve: (source: string, file: string) => { found: boolean, path: string | null | undefined } | ||
} | ||
|
||
export type SupportedBundler = 'vite' | ||
|
||
export type BundlerOption = string | { | ||
type?: SupportedBundler | ||
path?: string | ||
} | ||
|
||
export interface BundlerConfigTransformer { | ||
filename: string | ||
extensions: string[] | ||
transformConfig: (conf: any) => Promise<NapiResolveOptions> | ||
} | ||
|
||
export interface OxcResolverOptions extends NapiResolveOptions { | ||
/** @experimental detect bundler's resolve config */ | ||
bundlerConfig?: BundlerOption | ||
} |
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
Oops, something went wrong.