Skip to content

Latest commit

 

History

History
35 lines (30 loc) · 837 Bytes

cra-4.x.x.md

File metadata and controls

35 lines (30 loc) · 837 Bytes

Code snippets for create-react-app 1.x.x

useful codes you can copy and use in webpack.monkey.js file.

In real project I copy some of them in other file and use require function:

webpack plugin

Add webpack plugin

function addPlugin(webpackConfig, plugin) {
    webpackConfig.plugins.push(plugin);
}

Find Rule

function findRule(webpackConfig, callback) {
    const rules = webpackConfig.module.rules[1].oneOf;
    const index = rules.findIndex(callback);
    if (index === -1) throw Error('Loader not found');
    return rules[index]
}

find Babel rule

requirement: findRule

function findBabelRule(webpackConfig, plugins) {
    // find babel rule
    const babelRule = findRule(webpackConfig, (rule) => {
        return ('' + rule.test === '' + /\.(js|jsx)$/)
    });
    return babelRule;
}