Skip to content

Commit

Permalink
fix: resolvePath nesting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ricokahler committed Aug 30, 2021
1 parent b85da27 commit 1de7cef
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
4 changes: 0 additions & 4 deletions external.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,6 @@ declare module 'babel-plugin-module-resolver' {
* [0]: https://docs.npmjs.com/misc/config#loglevel
*/
logLevel?: string;
/**
* @internal
*/
_originalResolvePath?: ResolvePath;
}

export const resolvePath: ResolvePath;
Expand Down
11 changes: 5 additions & 6 deletions src/babel-plugin-tsconfig-paths-module-resolver.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { babelPluginTsconfigPathsModuleResolver } from "./babel-plugin-tsconfig-paths-module-resolver";
import { babelPluginTsconfigPathsModuleResolver } from './babel-plugin-tsconfig-paths-module-resolver';

const mockManipulateOptions = jest.fn(function (this: any, ...args) {
return {
Expand All @@ -16,17 +16,17 @@ const mockPre = jest.fn(function (this: any, ...args) {
};
});

jest.mock("babel-plugin-module-resolver", () =>
jest.mock('babel-plugin-module-resolver', () =>
jest.fn((...args) => ({
args,
extraOption: true,
manipulateOptions: mockManipulateOptions,
pre: mockPre,
}))
})),
);

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

Expand Down Expand Up @@ -76,7 +76,6 @@ describe("babelPluginTsconfigPathsModuleResolver", () => {
Object {
"mockThis": true,
"opts": Object {
"_originalResolvePath": [Function],
"extensions": Array [
".ts",
".tsx",
Expand Down
3 changes: 0 additions & 3 deletions src/babel-plugin-tsconfig-paths-module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ function babelPluginTsconfigPathsModuleResolver(

opts.extensions = opts.extensions || defaultExtensions;
opts.resolvePath = opts.resolvePath || createResolvePath();
if (!opts._originalResolvePath && opts.resolvePath) {
opts._originalResolvePath = opts.resolvePath;
}
}

return {
Expand Down
6 changes: 4 additions & 2 deletions src/create-resolve-path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('createResolvePath', () => {

const resolvePath = createResolvePath();
const result = resolvePath('./source-path', './current-file', {
_originalResolvePath: () => 'example result',
resolvePath: () => 'example result',
});

expect(result).toBe('example result');
Expand Down Expand Up @@ -148,7 +148,9 @@ describe('createResolvePath', () => {
Array [
"./source-path",
"./current-file",
Object {},
Object {
"_fromNested": true,
},
],
]
`);
Expand Down
14 changes: 9 additions & 5 deletions src/create-resolve-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createMatchPath, loadConfig } from 'tsconfig-paths';
import {
resolvePath as defaultResolvePath,
ResolvePath,
BabelPluginModuleResolveOptions,
} from 'babel-plugin-module-resolver';
import { defaultExtensions } from './default-extensions';

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

return function resolvePath(...args) {
const [sourcePath, currentFile, opts] = args;
const fallbackResolvePath = opts._originalResolvePath || defaultResolvePath;
return function resolvePath(sourcePath, currentFile, opts) {
const fallbackResolvePath = (opts as any)._fromNested
? defaultResolvePath
: opts.resolvePath || defaultResolvePath;

const extensions = opts.extensions || defaultExtensions;
const nextOpts = { ...opts, _fromNested: true };

if (!matchPath) {
if (opts.logLevel !== 'silent') {
Expand All @@ -32,7 +36,7 @@ export function createResolvePath(): ResolvePath {
);
}

return fallbackResolvePath(...args);
return fallbackResolvePath(sourcePath, currentFile, nextOpts);
}

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

return fallbackResolvePath(...args);
return fallbackResolvePath(sourcePath, currentFile, nextOpts);
};
}

0 comments on commit 1de7cef

Please sign in to comment.