Skip to content

Commit 1de7cef

Browse files
committed
fix: resolvePath nesting issue
1 parent b85da27 commit 1de7cef

5 files changed

+18
-20
lines changed

external.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,6 @@ declare module 'babel-plugin-module-resolver' {
205205
* [0]: https://docs.npmjs.com/misc/config#loglevel
206206
*/
207207
logLevel?: string;
208-
/**
209-
* @internal
210-
*/
211-
_originalResolvePath?: ResolvePath;
212208
}
213209

214210
export const resolvePath: ResolvePath;

src/babel-plugin-tsconfig-paths-module-resolver.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { babelPluginTsconfigPathsModuleResolver } from "./babel-plugin-tsconfig-paths-module-resolver";
1+
import { babelPluginTsconfigPathsModuleResolver } from './babel-plugin-tsconfig-paths-module-resolver';
22

33
const mockManipulateOptions = jest.fn(function (this: any, ...args) {
44
return {
@@ -16,17 +16,17 @@ const mockPre = jest.fn(function (this: any, ...args) {
1616
};
1717
});
1818

19-
jest.mock("babel-plugin-module-resolver", () =>
19+
jest.mock('babel-plugin-module-resolver', () =>
2020
jest.fn((...args) => ({
2121
args,
2222
extraOption: true,
2323
manipulateOptions: mockManipulateOptions,
2424
pre: mockPre,
25-
}))
25+
})),
2626
);
2727

28-
describe("babelPluginTsconfigPathsModuleResolver", () => {
29-
it("returns a pre-configured version of babel-plugin-module-resolve", () => {
28+
describe('babelPluginTsconfigPathsModuleResolver', () => {
29+
it('returns a pre-configured version of babel-plugin-module-resolve', () => {
3030
const mockArgs = { mockArgs: true };
3131
const result = babelPluginTsconfigPathsModuleResolver(mockArgs);
3232

@@ -76,7 +76,6 @@ describe("babelPluginTsconfigPathsModuleResolver", () => {
7676
Object {
7777
"mockThis": true,
7878
"opts": Object {
79-
"_originalResolvePath": [Function],
8079
"extensions": Array [
8180
".ts",
8281
".tsx",

src/babel-plugin-tsconfig-paths-module-resolver.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ function babelPluginTsconfigPathsModuleResolver(
1919

2020
opts.extensions = opts.extensions || defaultExtensions;
2121
opts.resolvePath = opts.resolvePath || createResolvePath();
22-
if (!opts._originalResolvePath && opts.resolvePath) {
23-
opts._originalResolvePath = opts.resolvePath;
24-
}
2522
}
2623

2724
return {

src/create-resolve-path.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('createResolvePath', () => {
4747

4848
const resolvePath = createResolvePath();
4949
const result = resolvePath('./source-path', './current-file', {
50-
_originalResolvePath: () => 'example result',
50+
resolvePath: () => 'example result',
5151
});
5252

5353
expect(result).toBe('example result');
@@ -148,7 +148,9 @@ describe('createResolvePath', () => {
148148
Array [
149149
"./source-path",
150150
"./current-file",
151-
Object {},
151+
Object {
152+
"_fromNested": true,
153+
},
152154
],
153155
]
154156
`);

src/create-resolve-path.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createMatchPath, loadConfig } from 'tsconfig-paths';
33
import {
44
resolvePath as defaultResolvePath,
55
ResolvePath,
6+
BabelPluginModuleResolveOptions,
67
} from 'babel-plugin-module-resolver';
78
import { defaultExtensions } from './default-extensions';
89

@@ -16,10 +17,13 @@ export function createResolvePath(): ResolvePath {
1617
configLoaderResult.paths,
1718
);
1819

19-
return function resolvePath(...args) {
20-
const [sourcePath, currentFile, opts] = args;
21-
const fallbackResolvePath = opts._originalResolvePath || defaultResolvePath;
20+
return function resolvePath(sourcePath, currentFile, opts) {
21+
const fallbackResolvePath = (opts as any)._fromNested
22+
? defaultResolvePath
23+
: opts.resolvePath || defaultResolvePath;
24+
2225
const extensions = opts.extensions || defaultExtensions;
26+
const nextOpts = { ...opts, _fromNested: true };
2327

2428
if (!matchPath) {
2529
if (opts.logLevel !== 'silent') {
@@ -32,7 +36,7 @@ export function createResolvePath(): ResolvePath {
3236
);
3337
}
3438

35-
return fallbackResolvePath(...args);
39+
return fallbackResolvePath(sourcePath, currentFile, nextOpts);
3640
}
3741

3842
const matchPathResult = matchPath(
@@ -50,6 +54,6 @@ export function createResolvePath(): ResolvePath {
5054
return relativePath.startsWith('./') ? relativePath : `./${relativePath}`;
5155
}
5256

53-
return fallbackResolvePath(...args);
57+
return fallbackResolvePath(sourcePath, currentFile, nextOpts);
5458
};
5559
}

0 commit comments

Comments
 (0)