Last active
August 3, 2017 12:02
-
-
Save nadav-dav/57b1e07794c6d52eedf6dbf5f4e2e1eb to your computer and use it in GitHub Desktop.
creating custom react-scripts (without ejecting) - it is ugly but it works
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
usage: | |
node ./custom-react-scripts start |
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
switch (process.argv[2]) { | |
// The "start" script is run during development mode | |
case 'start': | |
customize({ | |
"react-scripts/config/webpack.config.dev.js": loadCustomizer('./webpack-overrides.dev') | |
}); | |
require('react-scripts/scripts/start.js'); | |
break; | |
// The "build" script is run to produce a production bundle | |
case 'build': | |
customize({ | |
"react-scripts/config/webpack.config.prod.js": loadCustomizer('./webpack-overrides.prod') | |
}); | |
require('react-scripts/scripts/build.js'); | |
break; | |
default: | |
console.log('customized-config only supports "start", "build", and "test" options.'); | |
process.exit(-1); | |
} | |
// Attempt to load the given module and return null if it fails. | |
function loadCustomizer(module) { | |
try { | |
return require(module); | |
} catch (e) { | |
if (e.code !== "MODULE_NOT_FOUND") { | |
throw e; | |
} | |
} | |
// If the module doesn't exist, return a | |
// noop that simply returns the config it's given. | |
return config => config; | |
} | |
function customize(customizers) { | |
Object.keys(customizers).forEach(file => { | |
const _module = require(file); | |
customizers[file](_module); | |
}) | |
} |
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
const path = require('path'); | |
const util = require("util"); | |
module.exports = function (config) { | |
config.node = { | |
__dirname: true | |
}; | |
const babelLoader = config.module.rules.find(rule => rule.loader && rule.loader.includes("babel-loader")); | |
babelLoader.options.plugins = babelLoader.options.plugins || []; | |
babelLoader.options.plugins.push("transform-decorators-legacy"); | |
return config; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment