Skip to content

Commit 1f6245b

Browse files
Moe Sattlertleunen
authored andcommitted
feat: Add glob support in the root config (#78)
1 parent 922857d commit 1f6245b

3 files changed

Lines changed: 54 additions & 23 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"import"
2828
],
2929
"dependencies": {
30+
"glob": "^7.0.6",
3031
"resolve": "^1.1.7"
3132
},
3233
"devDependencies": {

src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'path';
22
import resolve from 'resolve';
3+
import glob from 'glob';
34
import mapToRelative from './mapToRelative';
45

56
function createAliasFileMap(pluginOpts) {
@@ -110,6 +111,17 @@ export default ({ types: t }) => {
110111
}
111112

112113
return {
114+
manipulateOptions(babelOptions) {
115+
const findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
116+
if (findPluginOptions.root) {
117+
findPluginOptions.root = findPluginOptions.root.reduce((resolvedDirs, dirPath) => {
118+
if (glob.hasMagic(dirPath)) {
119+
return resolvedDirs.concat(glob.sync(dirPath));
120+
}
121+
return resolvedDirs.concat(dirPath);
122+
}, []);
123+
}
124+
},
113125
visitor: {
114126
CallExpression: {
115127
exit(nodePath, state) {

test/index.js

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
/* eslint-env mocha */
2+
/* eslint-disable prefer-arrow-callback */
3+
/* eslint-disable func-names */
24
import assert from 'assert';
35
import { transform } from 'babel-core'; // eslint-disable-line import/no-extraneous-dependencies
46
import plugin from '../src';
57

68
function testRequireImport(source, output, transformerOpts) {
7-
it('with a require statement', () => {
9+
it('with a require statement', function () {
810
const code = `var something = require("${source}");`;
911
const result = transform(code, transformerOpts);
1012

1113
assert.strictEqual(result.code, `var something = require("${output}");`);
1214
});
1315

14-
it('with an import statement', () => {
16+
it('with an import statement', function () {
1517
const code = `import something from "${source}";`;
1618
const result = transform(code, transformerOpts);
1719

1820
assert.strictEqual(result.code, `import something from "${output}";`);
1921
});
2022
}
2123

22-
describe('root', () => {
24+
describe('root', function () {
2325
const transformerOpts = {
2426
plugins: [
2527
[plugin, {
@@ -28,48 +30,64 @@ describe('root', () => {
2830
]
2931
};
3032

31-
describe('should rewrite the file path inside a root directory', () => {
33+
const transformerOptsGlob = {
34+
plugins: [
35+
[plugin, {
36+
root: ['./test/**/components']
37+
}]
38+
]
39+
};
40+
41+
describe('should rewrite the file path inside a root directory', function () {
3242
testRequireImport(
3343
'c1',
3444
'./test/examples/components/c1',
3545
transformerOpts
3646
);
3747
});
3848

39-
describe('should rewrite the sub file path inside a root directory', () => {
49+
describe('should rewrite the sub file path inside a root directory', function () {
4050
testRequireImport(
4151
'sub/sub1',
4252
'./test/examples/components/sub/sub1',
4353
transformerOpts
4454
);
4555
});
4656

47-
describe('should rewrite the file while keeping the extension', () => {
57+
describe('should rewrite the file while keeping the extension', function () {
4858
testRequireImport(
4959
'sub/sub1.css',
5060
'./test/examples/components/sub/sub1.css',
5161
transformerOpts
5262
);
5363
});
5464

55-
describe('should rewrite the file with a filename containing a dot', () => {
65+
describe('should rewrite the file with a filename containing a dot', function () {
5666
testRequireImport(
5767
'sub/custom.modernizr3',
5868
'./test/examples/components/sub/custom.modernizr3',
5969
transformerOpts
6070
);
6171
});
6272

63-
describe('should not rewrite a path outisde of the root directory', () => {
73+
describe('should not rewrite a path outisde of the root directory', function () {
6474
testRequireImport(
6575
'example-file',
6676
'example-file',
6777
transformerOpts
6878
);
6979
});
80+
81+
describe('should rewrite the file path inside a root directory according to glob', function () {
82+
testRequireImport(
83+
'c1',
84+
'./test/examples/components/c1',
85+
transformerOptsGlob
86+
);
87+
});
7088
});
7189

72-
describe('alias', () => {
90+
describe('alias', function () {
7391
const transformerOpts = {
7492
plugins: [
7593
[plugin, {
@@ -83,17 +101,17 @@ describe('alias', () => {
83101
]
84102
};
85103

86-
describe('should alias a known path', () => {
87-
describe('using a simple exposed name', () => {
88-
describe('when requiring the exact name', () => {
104+
describe('should alias a known path', function () {
105+
describe('using a simple exposed name', function () {
106+
describe('when requiring the exact name', function () {
89107
testRequireImport(
90108
'utils',
91109
'./src/mylib/subfolder/utils',
92110
transformerOpts
93111
);
94112
});
95113

96-
describe('when requiring a sub file of the exposed name', () => {
114+
describe('when requiring a sub file of the exposed name', function () {
97115
testRequireImport(
98116
'utils/my-util-file',
99117
'./src/mylib/subfolder/utils/my-util-file',
@@ -102,16 +120,16 @@ describe('alias', () => {
102120
});
103121
});
104122

105-
describe('using a "complex" exposed name', () => {
106-
describe('when requiring the exact name', () => {
123+
describe('using a "complex" exposed name', function () {
124+
describe('when requiring the exact name', function () {
107125
testRequireImport(
108126
'awesome/components',
109127
'./src/components',
110128
transformerOpts
111129
);
112130
});
113131

114-
describe('when requiring a sub file of the exposed name', () => {
132+
describe('when requiring a sub file of the exposed name', function () {
115133
testRequireImport(
116134
'awesome/components/my-comp',
117135
'./src/components/my-comp',
@@ -120,7 +138,7 @@ describe('alias', () => {
120138
});
121139
});
122140

123-
describe('with a dot in the filename', () => {
141+
describe('with a dot in the filename', function () {
124142
testRequireImport(
125143
'utils/custom.modernizr3',
126144
'./src/mylib/subfolder/utils/custom.modernizr3',
@@ -129,24 +147,24 @@ describe('alias', () => {
129147
});
130148
});
131149

132-
describe('should alias the path with its extension', () => {
150+
describe('should alias the path with its extension', function () {
133151
testRequireImport(
134152
'awesome/components/my-comp.css',
135153
'./src/components/my-comp.css',
136154
transformerOpts
137155
);
138156
});
139157

140-
describe('should not alias a unknown path', () => {
141-
describe('when requiring a node module', () => {
158+
describe('should not alias a unknown path', function () {
159+
describe('when requiring a node module', function () {
142160
testRequireImport(
143161
'other-lib',
144162
'other-lib',
145163
transformerOpts
146164
);
147165
});
148166

149-
describe('when requiring a specific un-mapped file', () => {
167+
describe('when requiring a specific un-mapped file', function () {
150168
testRequireImport(
151169
'./l/otherLib',
152170
'./l/otherLib',
@@ -155,15 +173,15 @@ describe('alias', () => {
155173
});
156174
});
157175

158-
describe('(legacy) should support aliasing a node module with "npm:"', () => {
176+
describe('(legacy) should support aliasing a node module with "npm:"', function () {
159177
testRequireImport(
160178
'abstract/thing',
161179
'concrete/thing',
162180
transformerOpts
163181
);
164182
});
165183

166-
describe('should support aliasing a node modules', () => {
184+
describe('should support aliasing a node modules', function () {
167185
testRequireImport(
168186
'underscore/map',
169187
'lodash/map',

0 commit comments

Comments
 (0)