-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to customize Babel/Webpack configs (#3)
- Loading branch information
Showing
10 changed files
with
228 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
bin/react-app.js | ||
config/env.js | ||
config/webpack.config.dev.js | ||
config/webpack.config.prod.js | ||
scripts/build.js | ||
scripts/start.js | ||
scripts/test.js | ||
README.md | ||
WebpackDevServerUtils.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright © 2016-present Kriasoft. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE.txt file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const HtmlWebpackPlugin = require('html-webpack-plugin'); | ||
const paths = require('./config/paths'); | ||
|
||
// Make sure that including paths.js after env.js will read .env variables. | ||
delete require.cache[require.resolve('react-scripts/config/paths')]; | ||
delete require.cache[require.resolve('./config/paths')]; | ||
|
||
const override = fs.existsSync(paths.override) ? require(paths.override) : null; | ||
|
||
function overrideBabel(config, options) { | ||
const newConfig = Object.assign({}, config); | ||
|
||
if (options.target === 'node') { | ||
newConfig.compact = false; | ||
} | ||
|
||
return override && typeof override.babel === 'function' | ||
? override.babel(newConfig, options) | ||
: newConfig; | ||
} | ||
|
||
module.exports = function customize(name, config, options) { | ||
const newConfig = Object.assign({}, config, { | ||
// Remove HtmlWebpackPlugin | ||
plugins: config.plugins.filter(x => !(x instanceof HtmlWebpackPlugin)), | ||
|
||
// Find Babel config | ||
module: Object.assign({}, config.module, { | ||
rules: config.module.rules.map( | ||
x => | ||
x.oneOf | ||
? Object.assign({}, x, { | ||
oneOf: x.oneOf.map( | ||
y => | ||
y.use | ||
? Object.assign({}, y, { | ||
use: y.use.map( | ||
z => | ||
z.loader === require.resolve('babel-loader') && | ||
z.options.presets[0] === | ||
require.resolve('babel-preset-react-app') | ||
? Object.assign({}, z, { | ||
options: overrideBabel(z.options, options), | ||
}) | ||
: z | ||
), | ||
}) | ||
: y | ||
), | ||
}) | ||
: x | ||
), | ||
}), | ||
}); | ||
|
||
return override && typeof override[name] === 'function' | ||
? override[name](newConfig, options) | ||
: newConfig; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.