Add an option to enable/disable .babelrc lookup

Summary:
By default, when Babel transforms a file, it looks for .babelrc in the file's directory and all parent directories until it finds a .babelrc file. This means that when Metro tries to transform `<PROJECT_ROOT>/node_modules/dep/mod.js`, it searches for `<PROJECT_ROOT>/node_modules/dep/.babelrc`. If that .babelrc actually exists, Babel will try to apply it.

In practice, this often causes problems because packages that include .babelrc often do so unintentionally -- they don't intend for the package consumer to look at that .babelrc file. One thing we've done a lot is `rm` all .babelrc files under node_modules -- this commit effectively achieves the same in a less destructive way by telling Babel not to look up .babelrc files. (To clarify, Metro will still apply the .babelrc file in the project root.)

Since the current behavior is to look at .babelrc files, this commit keeps that behavior for now. We'll consider overriding the default behavior (that is, making Babel not lookup .babelrc files) in the default configuration of Expo/RN projects. If this goes well and we empirically find that people are having a better time, we may want to consider flipping this option's default in Metro, so that Metro tells Babel not to look up .babelrc files by default.
Closes https://github.com/facebook/metro-bundler/pull/31

Differential Revision: D5469620

Pulled By: jeanlauliac

fbshipit-source-id: fe7a1042feafff843e1a6d8cc9487eb6ff8e8358
This commit is contained in:
James Ide 2017-07-24 13:29:32 -07:00 committed by Facebook Github Bot
parent 42d61ea63a
commit 0f3d7117d4
6 changed files with 19 additions and 0 deletions

View File

@ -41,6 +41,7 @@ var commonOptions = {
allowBundleUpdates: false, allowBundleUpdates: false,
assetExts: defaults.assetExts, assetExts: defaults.assetExts,
cacheVersion: 'smth', cacheVersion: 'smth',
enableBabelRCLookup: true,
extraNodeModules: {}, extraNodeModules: {},
platforms: defaults.platforms, platforms: defaults.platforms,
resetCache: false, resetCache: false,
@ -176,6 +177,7 @@ describe('Bundler', function() {
minify: false, minify: false,
platform: undefined, platform: undefined,
transform: { transform: {
enableBabelRCLookup: true,
dev: true, dev: true,
generateSourceMaps: false, generateSourceMaps: false,
hot: false, hot: false,

View File

@ -124,6 +124,7 @@ type Options = {|
+assetServer: AssetServer, +assetServer: AssetServer,
+blacklistRE?: RegExp, +blacklistRE?: RegExp,
+cacheVersion: string, +cacheVersion: string,
+enableBabelRCLookup: boolean,
+extraNodeModules: {}, +extraNodeModules: {},
+getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>, +getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
+getTransformOptions?: GetTransformOptions, +getTransformOptions?: GetTransformOptions,
@ -545,6 +546,7 @@ class Bundler {
return this.getTransformOptions( return this.getTransformOptions(
entryFile, entryFile,
{ {
enableBabelRCLookup: this._opts.enableBabelRCLookup,
dev, dev,
generateSourceMaps, generateSourceMaps,
hot, hot,
@ -587,6 +589,7 @@ class Bundler {
const bundlingOptions: BundlingOptions = await this.getTransformOptions( const bundlingOptions: BundlingOptions = await this.getTransformOptions(
entryFile, entryFile,
{ {
enableBabelRCLookup: this._opts.enableBabelRCLookup,
dev, dev,
platform, platform,
hot, hot,
@ -797,6 +800,7 @@ class Bundler {
async getTransformOptions( async getTransformOptions(
mainModuleName: string, mainModuleName: string,
options: {| options: {|
enableBabelRCLookup: boolean,
dev: boolean, dev: boolean,
generateSourceMaps: boolean, generateSourceMaps: boolean,
hot: boolean, hot: boolean,
@ -822,6 +826,7 @@ class Bundler {
minify: options.minify, minify: options.minify,
platform, platform,
transform: { transform: {
enableBabelRCLookup: options.enableBabelRCLookup,
dev, dev,
generateSourceMaps: options.generateSourceMaps, generateSourceMaps: options.generateSourceMaps,
hot, hot,

View File

@ -43,6 +43,7 @@ export type Transformer<ExtraOptions: {} = {}> = {
}; };
export type TransformOptionsStrict = {| export type TransformOptionsStrict = {|
+enableBabelRCLookup: boolean,
+dev: boolean, +dev: boolean,
+generateSourceMaps: boolean, +generateSourceMaps: boolean,
+hot: boolean, +hot: boolean,
@ -52,6 +53,7 @@ export type TransformOptionsStrict = {|
|}; |};
export type TransformOptions = { export type TransformOptions = {
+enableBabelRCLookup?: boolean,
+dev?: boolean, +dev?: boolean,
+generateSourceMaps?: boolean, +generateSourceMaps?: boolean,
+hot?: boolean, +hot?: boolean,

View File

@ -70,6 +70,7 @@ type Options = {
assetExts?: Array<string>, assetExts?: Array<string>,
blacklistRE?: RegExp, blacklistRE?: RegExp,
cacheVersion?: string, cacheVersion?: string,
enableBabelRCLookup?: boolean,
extraNodeModules?: {}, extraNodeModules?: {},
getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>, getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
getTransformOptions?: GetTransformOptions, getTransformOptions?: GetTransformOptions,
@ -131,6 +132,7 @@ class Server {
assetExts: Array<string>, assetExts: Array<string>,
blacklistRE: void | RegExp, blacklistRE: void | RegExp,
cacheVersion: string, cacheVersion: string,
enableBabelRCLookup: boolean,
extraNodeModules: {}, extraNodeModules: {},
getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>, getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
getTransformOptions?: GetTransformOptions, getTransformOptions?: GetTransformOptions,
@ -176,6 +178,10 @@ class Server {
assetExts: options.assetExts || defaults.assetExts, assetExts: options.assetExts || defaults.assetExts,
blacklistRE: options.blacklistRE, blacklistRE: options.blacklistRE,
cacheVersion: options.cacheVersion || '1.0', cacheVersion: options.cacheVersion || '1.0',
enableBabelRCLookup:
options.enableBabelRCLookup != null
? options.enableBabelRCLookup
: true,
extraNodeModules: options.extraNodeModules || {}, extraNodeModules: options.extraNodeModules || {},
getPolyfills: options.getPolyfills, getPolyfills: options.getPolyfills,
getTransformOptions: options.getTransformOptions, getTransformOptions: options.getTransformOptions,

View File

@ -36,6 +36,7 @@ type Options = {|
+sourceExts: ?Array<string>, +sourceExts: ?Array<string>,
+transformCache: TransformCache, +transformCache: TransformCache,
+transformModulePath: string, +transformModulePath: string,
enableBabelRCLookup?: boolean,
getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>, getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
globalTransformCache: ?GlobalTransformCache, globalTransformCache: ?GlobalTransformCache,
hasteImpl?: HasteImpl, hasteImpl?: HasteImpl,

View File

@ -87,6 +87,9 @@ function buildBabelConfig(filename, options) {
const babelRC = getBabelRC(options.projectRoot); const babelRC = getBabelRC(options.projectRoot);
const extraConfig = { const extraConfig = {
babelrc: typeof options.enableBabelRCLookup === 'boolean'
? options.enableBabelRCLookup
: true,
code: false, code: false,
filename, filename,
}; };