Skip to content

Commit

Permalink
fix: fixes a resolve path loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ricokahler committed Aug 30, 2021
1 parent c1e244d commit b698f02
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 18 deletions.
15 changes: 15 additions & 0 deletions examples/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ describe('examples', () => {
]
`);
});

test('with-custom-resolve-path-fn', () => {
const standard = jest.requireActual(
'@babel-plugin-tsconfig-paths-module-resolver/with-custom-resolve-path-fn',
).default;

expect(standard).toMatchInlineSnapshot(`
Array [
"from src/bar/a",
"from src/foo/b",
"from umbrella/baz/c",
"from special",
]
`);
});
});
25 changes: 25 additions & 0 deletions examples/with-custom-resolve-path-fn/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { createResolvePath } = require('../..');

const resolvePath = createResolvePath();

function customResolvePath(...args) {
const [sourceFile] = args;

if (sourceFile === 'needs-a-custom-resolver') return './special';
return resolvePath(...args);
}

module.exports = {
presets: [
'@babel/preset-typescript',
['@babel/preset-env', { targets: { node: true } }],
],
plugins: [
[
require.resolve('../..'),
{
resolvePath: customResolvePath,
},
],
],
};
10 changes: 10 additions & 0 deletions examples/with-custom-resolve-path-fn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@babel-plugin-tsconfig-paths-module-resolver/with-custom-resolve-path-fn",
"private": true,
"version": "1.0.0",
"main": "./dist/index.js",
"scripts": {
"build": "babel -x .ts ./src --out-dir ./dist"
},
"license": "MIT"
}
1 change: 1 addition & 0 deletions examples/with-custom-resolve-path-fn/src/bar/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'from src/bar/a';
1 change: 1 addition & 0 deletions examples/with-custom-resolve-path-fn/src/foo/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'from src/foo/b';
7 changes: 7 additions & 0 deletions examples/with-custom-resolve-path-fn/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import a from '@/bar/a';
import b from '@/foo/b';
import c from '@/baz/c';
// @ts-expect-error
import d from 'needs-a-custom-resolver';

export default [a, b, c, d];
1 change: 1 addition & 0 deletions examples/with-custom-resolve-path-fn/src/special.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'from special';
1 change: 1 addition & 0 deletions examples/with-custom-resolve-path-fn/src/umbrella/baz/c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'from umbrella/baz/c';
13 changes: 13 additions & 0 deletions examples/with-custom-resolve-path-fn/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node",
"baseUrl": "./src",
"paths": {
"@/*": ["umbrella/*", "*"]
}
},
"include": ["./src/**/*.ts"],
"exclude": ["**/node_modules"]
}
4 changes: 4 additions & 0 deletions external.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ 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: 11 additions & 0 deletions package-lock.json

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

33 changes: 17 additions & 16 deletions src/babel-plugin-tsconfig-paths-module-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,22 @@ describe("babelPluginTsconfigPathsModuleResolver", () => {
`);

expect(mockThis).toMatchInlineSnapshot(`
Object {
"mockThis": true,
"opts": Object {
"extensions": Array [
".ts",
".tsx",
".js",
".jsx",
".es",
".es6",
".mjs",
],
"resolvePath": [Function],
},
}
`);
Object {
"mockThis": true,
"opts": Object {
"_originalResolvePath": [Function],
"extensions": Array [
".ts",
".tsx",
".js",
".jsx",
".es",
".es6",
".mjs",
],
"resolvePath": [Function],
},
}
`);
});
});
3 changes: 3 additions & 0 deletions src/babel-plugin-tsconfig-paths-module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ function babelPluginTsconfigPathsModuleResolver(

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

return {
Expand Down
2 changes: 1 addition & 1 deletion 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', {
resolvePath: () => 'example result',
_originalResolvePath: () => 'example result',
});

expect(result).toBe('example result');
Expand Down
2 changes: 1 addition & 1 deletion src/create-resolve-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function createResolvePath(): ResolvePath {

return function resolvePath(...args) {
const [sourcePath, currentFile, opts] = args;
const fallbackResolvePath = opts.resolvePath || defaultResolvePath;
const fallbackResolvePath = opts._originalResolvePath || defaultResolvePath;
const extensions = opts.extensions || defaultExtensions;

if (!matchPath) {
Expand Down

0 comments on commit b698f02

Please sign in to comment.