Skip to content

Commit 6993931

Browse files
committed
refactor: logic to find babel config file
Use find-up matcher function to automatically stop looking for babel config file once cwd is reached, thus increasing performance.
1 parent a117d77 commit 6993931

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

lib/build.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ const BABEL_RELATIVE_CONFIG_FILENAMES = [
2525
'.babelrc.json',
2626
];
2727

28+
const BABEL_CONFIG_FILENAMES = [
29+
...BABEL_ROOT_CONFIG_FILENAMES,
30+
...BABEL_RELATIVE_CONFIG_FILENAMES,
31+
];
32+
2833
const testFilePattern = '\\.(test|spec)\\.?';
2934

3035
// custom babel target for each node version
@@ -38,9 +43,23 @@ function getBabelTarget(envConfig) {
3843

3944
function existsBabelConfig(functionsDir) {
4045
const babelConfigFile = findUp.sync(
41-
[...BABEL_ROOT_CONFIG_FILENAMES, ...BABEL_RELATIVE_CONFIG_FILENAMES],
46+
(dir) => {
47+
console.log('Dir ', dir);
48+
const babelConfigFile = BABEL_CONFIG_FILENAMES.find(
49+
(babelConfigFilename) =>
50+
findUp.sync.exists(path.join(dir, babelConfigFilename)),
51+
);
52+
if (babelConfigFile) {
53+
return path.join(dir, babelConfigFile);
54+
}
55+
// Don't search higher than wherever 'netlify-lambda build' was invoked from
56+
if (dir === process.cwd()) {
57+
return findUp.stop;
58+
}
59+
return undefined;
60+
},
4261
{
43-
cwd: path.join(process.cwd(), functionsDir),
62+
cwd: functionsDir,
4463
},
4564
);
4665
if (babelConfigFile) {

lib/build.spec.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -90,43 +90,50 @@ describe('build', () => {
9090
]);
9191
});
9292

93-
describe("babel config file resolution", () => {
94-
const findBabelLoaderRule = rules => rules.find(rule => rule.use.loader.includes("babel-loader"));
93+
describe('babel config file resolution', () => {
94+
const findBabelLoaderRule = (rules) =>
95+
rules.find((rule) => rule.use.loader.includes('babel-loader'));
9596

96-
it("should alter the default babelOpts when no valid babel config file is found", async () => {
97-
setupFunction("", "not-babel.config.js");
97+
it('should alter the default babelOpts when no valid babel config file is found', async () => {
98+
setupFunction('', 'not-babel.config.js');
9899

99100
const stats = await build.run(functions);
100-
const babelLoaderRuleOptions = findBabelLoaderRule(stats.compilation.options.module.rules).use.options;
101+
const babelLoaderRuleOptions = findBabelLoaderRule(
102+
stats.compilation.options.module.rules,
103+
).use.options;
101104

102105
expect(babelLoaderRuleOptions.presets).toBeDefined();
103106
expect(babelLoaderRuleOptions.plugins).toBeDefined();
104107
});
105108

106-
it("should not alter the default babelOpts when a valid babel config file is found in same directory as the functions directory", async () => {
107-
setupFunction("", "babel.config.js");
109+
it('should not alter the default babelOpts when a valid babel config file is found in same directory as the functions directory', async () => {
110+
setupFunction('', 'babel.config.js');
108111

109112
const stats = await build.run(functions);
110-
const babelLoaderRuleOptions = findBabelLoaderRule(stats.compilation.options.module.rules).use.options;
113+
const babelLoaderRuleOptions = findBabelLoaderRule(
114+
stats.compilation.options.module.rules,
115+
).use.options;
111116

112117
expect(babelLoaderRuleOptions.presets).toBeUndefined();
113118
expect(babelLoaderRuleOptions.plugins).toBeUndefined();
114119
});
115120

116-
it("should not alter the default babelOpts when a valid babel config is found in directory above the functions directory", async () => {
117-
setupFunction("", "babel.config.js");
121+
it('should not alter the default babelOpts when a valid babel config is found in directory above the functions directory', async () => {
122+
setupFunction('', 'babel.config.js');
118123

119-
const functionsSubDir = path.join(functions, "subdir");
124+
const functionsSubDir = path.join(functions, 'subdir');
120125

121126
fs.mkdirSync(functionsSubDir);
122-
fs.writeFileSync(path.join(functionsSubDir, "index.js"), "");
127+
fs.writeFileSync(path.join(functionsSubDir, 'index.js'), '');
123128

124129
const stats = await build.run(functionsSubDir);
125-
const babelLoaderRuleOptions = findBabelLoaderRule(stats.compilation.options.module.rules).use.options;
130+
const babelLoaderRuleOptions = findBabelLoaderRule(
131+
stats.compilation.options.module.rules,
132+
).use.options;
126133

127134
expect(babelLoaderRuleOptions.presets).toBeUndefined();
128135
expect(babelLoaderRuleOptions.plugins).toBeUndefined();
129136
});
130-
})
137+
});
131138
});
132139
});

0 commit comments

Comments
 (0)