|
1 | 1 | import pathLib from 'node:path'; |
2 | 2 |
|
3 | | -import { OptionManager } from '@babel/core'; |
| 3 | +import { loadOptions } from '@babel/core'; |
4 | 4 | import defu from '@dword-design/defu'; |
5 | 5 | import { resolvePath as defaultResolvePath } from 'babel-plugin-module-resolver'; |
6 | 6 |
|
| 7 | +interface BabelPlugin { |
| 8 | + key: string; |
| 9 | + options?: Record<string, unknown>; |
| 10 | +} |
| 11 | +interface BabelConfig { |
| 12 | + plugins?: BabelPlugin[]; |
| 13 | +} |
7 | 14 | const isParentImport = path => /^(\.\/)?\.\.\//.test(path); |
8 | 15 |
|
9 | 16 | const findMatchingAlias = (sourcePath, currentFile, options) => { |
@@ -32,16 +39,18 @@ export default { |
32 | 39 | const folder = pathLib.dirname(currentFile); |
33 | 40 | // can't check a non-file |
34 | 41 | if (currentFile === '<text>') return {}; |
35 | | - const manager = new OptionManager(); |
| 42 | + const optionsFromRule = context.options[0] ?? {}; |
36 | 43 |
|
37 | | - const babelConfig = manager.init({ |
| 44 | + const babelConfig = (loadOptions({ |
38 | 45 | filename: currentFile, |
39 | | - ...context.options[0]?.babelOptions, |
40 | | - }); |
| 46 | + ...optionsFromRule.babelOptions, |
| 47 | + }) || {}) as BabelConfig; |
41 | 48 |
|
42 | | - const plugin = babelConfig.plugins.find(_ => _.key === 'module-resolver'); |
| 49 | + const optionsFromPlugin = |
| 50 | + babelConfig?.plugins?.find(_ => _.key === 'module-resolver')?.options ?? |
| 51 | + {}; |
43 | 52 |
|
44 | | - const options = defu(context.options[0] || {}, plugin?.options || {}, { |
| 53 | + const options = defu(optionsFromRule, optionsFromPlugin, { |
45 | 54 | alias: [], |
46 | 55 | cwd: context.cwd, |
47 | 56 | }); |
|
0 commit comments