Skip to content

Commit 0683d71

Browse files
authored
Merge branch 'master' into relative
2 parents d9ba628 + c8202d8 commit 0683d71

File tree

7 files changed

+64
-4
lines changed

7 files changed

+64
-4
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
<a name="3.1.0"></a>
6+
# [3.1.0](https://github.com/tleunen/babel-plugin-module-resolver/compare/v3.0.0...v3.1.0) (2018-02-08)
7+
8+
9+
### Bug Fixes
10+
11+
* Fix path transformation for dot files that are siblings ([#253](https://github.com/tleunen/babel-plugin-module-resolver/issues/253)) ([7dc2da6](https://github.com/tleunen/babel-plugin-module-resolver/commit/7dc2da6))
12+
13+
14+
### Features
15+
16+
* Ability to declare aliases in an array to preserve the order ([#243](https://github.com/tleunen/babel-plugin-module-resolver/issues/243)) ([d03715d](https://github.com/tleunen/babel-plugin-module-resolver/commit/d03715d))
17+
* Add stripExtensions option to determine which extensions get removed ([#247](https://github.com/tleunen/babel-plugin-module-resolver/issues/247)) ([fdf5da9](https://github.com/tleunen/babel-plugin-module-resolver/commit/fdf5da9))
18+
* Allow using a function to define the result of an alias ([#245](https://github.com/tleunen/babel-plugin-module-resolver/issues/245)) ([9299d9a](https://github.com/tleunen/babel-plugin-module-resolver/commit/9299d9a))
19+
* run plugin also on Program exit to handle dynamically added imports from other transforms ([#269](https://github.com/tleunen/babel-plugin-module-resolver/issues/269)) ([12a2d07](https://github.com/tleunen/babel-plugin-module-resolver/commit/12a2d07))
20+
21+
22+
523
<a name="3.0.0"></a>
624
# [3.0.0](https://github.com/tleunen/babel-plugin-module-resolver/compare/v3.0.0-beta.5...v3.0.0) (2017-11-08)
725

DOCS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ A string or an array of root directories. Specify the paths or a glob path (eg.
4747

4848
A map of alias. You can also alias `node_modules` dependencies, not just local files.
4949

50-
### Regular expressions
50+
### Regular expressions
5151

5252
It is possible to specify an alias using a regular expression. To do that, either start an alias with `'^'` or end it with `'$'`:
5353

@@ -69,7 +69,7 @@ You can reference the n-th matched group with `'\\n'` (`'\\0'` refers to the who
6969

7070
To use the backslash character (`\`) just escape it like so: `'\\\\'` (double escape is needed because of JSON already using `\` for escaping).
7171

72-
### Passing a substitute function
72+
### Passing a substitute function
7373

7474
If you need even more power over the aliased path, you can pass a function in the alias configuration:
7575

@@ -206,7 +206,7 @@ If you want to leave some paths as-is, then you can return `undefined` or any ot
206206

207207
create-react-app by default don't use .babelrc, so in webpack.config.dev.js, add plugins property within js loader like below. Note that plugins recieve an array.
208208

209-
```json
209+
```js
210210
// Process JS with Babel.
211211
{
212212
test: /\.(js|jsx|mjs)$/,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-plugin-module-resolver",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"main": "lib/index.js",
55
"description": "Module resolver plugin for Babel",
66
"repository": {

src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const visitor = {
1818
enter(programPath, state) {
1919
programPath.traverse(importVisitors, state);
2020
},
21+
exit(programPath, state) {
22+
programPath.traverse(importVisitors, state);
23+
},
2124
},
2225
};
2326

@@ -29,7 +32,14 @@ export default ({ types }) => ({
2932

3033
const currentFile = file.opts.filename;
3134
this.normalizedOpts = normalizeOptions(currentFile, this.opts);
35+
// We need to keep track of all handled nodes so we do not try to transform them twice,
36+
// because we run before (enter) and after (exit) all nodes are handled
37+
this.moduleResolverVisited = new Set();
3238
},
3339

3440
visitor,
41+
42+
post() {
43+
this.moduleResolverVisited.clear();
44+
},
3545
});

src/transformers/call.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ import {
66

77

88
export default function transformCall(nodePath, state) {
9+
if (state.moduleResolverVisited.has(nodePath)) {
10+
return;
11+
}
12+
913
const calleePath = nodePath.get('callee');
1014
const isNormalCall = state.normalizedOpts.transformFunctions.some(
1115
pattern => matchesPattern(state.types, calleePath, pattern),
1216
);
1317

1418
if (isNormalCall || isImportCall(state.types, nodePath)) {
19+
state.moduleResolverVisited.add(nodePath);
1520
mapPathString(nodePath.get('arguments.0'), state);
1621
}
1722
}

src/transformers/import.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@ import { mapPathString } from '../utils';
22

33

44
export default function transformImport(nodePath, state) {
5+
if (state.moduleResolverVisited.has(nodePath)) {
6+
return;
7+
}
8+
state.moduleResolverVisited.add(nodePath);
9+
510
mapPathString(nodePath.get('source'), state);
611
}

test/dynamicImport.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,26 @@ describe('import()', () => {
5656

5757
expect(result.code).toBe('import("").then(() => {}).catch(() => {});');
5858
});
59+
60+
it('should handle imports added by other transforms', () => {
61+
const options = {
62+
...transformerOpts,
63+
plugins: [
64+
function fakePlugin({ types }) {
65+
return {
66+
visitor: {
67+
Identifier(path) {
68+
path.replaceWith(types.Import());
69+
},
70+
},
71+
};
72+
},
73+
...transformerOpts.plugins,
74+
],
75+
};
76+
const code = 'boo("components/Header/SubHeader");';
77+
const result = transform(code, options);
78+
79+
expect(result.code).toBe('import("./test/testproject/src/components/Header/SubHeader");');
80+
});
5981
});

0 commit comments

Comments
 (0)