Helper functions for navigating webpack configurations and debugging.
Extract the babel loader configuration from a webpack config object.
/**
* Extracts babel loader from webpack config
* Create-react-app defines two babel configurations: one for src/ files and one for external files
* @param {Object} config - Webpack configuration object to search
* @param {boolean} isOutsideOfApp - Whether to get loader for files outside src/ (default: false)
* @returns {Object} Babel loader configuration object with options, include/exclude, etc.
*/
function getBabelLoader(config, isOutsideOfApp);Usage Examples:
const { getBabelLoader } = require("customize-cra");
// Custom babel customizer using getBabelLoader
const addCustomBabelOption = () => config => {
const babelLoader = getBabelLoader(config);
babelLoader.options.customOption = true;
return config;
};
// Get external babel loader (for node_modules)
const customizeExternalBabel = () => config => {
const externalBabelLoader = getBabelLoader(config, true);
if (!externalBabelLoader.options.plugins) {
externalBabelLoader.options.plugins = [];
}
externalBabelLoader.options.plugins.push("some-plugin");
return config;
};Debug utility for inspecting configuration objects during the override process.
/**
* Debug utility for inspecting configuration objects
* Logs configuration state to console or file without modifying the config
* @param {Object} options - Options object for controlling tap behavior
* @param {string} [options.message] - Message to print before configuration dump
* @param {string} [options.dest] - File path to append logs to (in addition to console)
* @returns {Function} Configuration pass-through function with logging side effects
*/
function tap(options);Usage Examples:
const { override, tap, addLessLoader, disableEsLint } = require("customize-cra");
module.exports = override(
// Log initial config state
tap({ message: "Initial configuration" }),
disableEsLint(),
// Log config after disabling ESLint
tap({ message: "After disabling ESLint" }),
addLessLoader(),
// Log final config to both console and file
tap({
message: "Final configuration",
dest: "customize-cra.log"
})
);Advanced Usage:
// Log only to file (no message)
module.exports = override(
addDecoratorsLegacy(),
tap({ dest: "build-config.json" }),
disableEsLint()
);
// Simple message logging
module.exports = override(
tap({ message: "Debug: webpack config state" }),
// ... other customizers
);The getBabelLoader function handles the differences between create-react-app versions:
oneOf array within module rulesuse arraysThe function:
oneOf array for babel loadersuse arrays and searches thereThe tap function:
message and dest propertiesJSON.stringify with 2-space indentation for readable outputfs.appendFile when dest is specifiedgetBabelLoader may return undefined if:
isOutsideOfApp parameter doesn't match any existing babel loader configurationAlways check the return value before accessing properties:
const customBabelCustomizer = () => config => {
const babelLoader = getBabelLoader(config);
if (babelLoader && babelLoader.options) {
babelLoader.options.customOption = true;
}
return config;
};tap function errors are primarily related to file system operations:
destdest optionThese errors are not caught internally, so wrap in try-catch if file writing is critical:
const safeTap = (options) => (config) => {
try {
return tap(options)(config);
} catch (error) {
console.warn("Tap logging failed:", error.message);
return config;
}
};