Skip to content

Instantly share code, notes, and snippets.

@nadav-dav
Last active August 3, 2017 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nadav-dav/57b1e07794c6d52eedf6dbf5f4e2e1eb to your computer and use it in GitHub Desktop.
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
usage:
node ./custom-react-scripts start
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);
})
}
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