Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
customize-cra
Advanced tools
This project provides a set of utilities to customize the Create React App v2 configurations leveraging [`react-app-rewired`](https://github.com/timarney/react-app-rewired/) core functionalities.
The customize-cra package is a utility for customizing Create React App (CRA) configurations without ejecting. It allows you to override the default Webpack configuration, Babel configuration, and other settings in a CRA project.
Override Webpack Configuration
This feature allows you to override the default Webpack configuration. In this example, a Webpack alias is added to simplify imports from the 'src/components' directory.
const { override, addWebpackAlias } = require('customize-cra');
const path = require('path');
module.exports = override(
addWebpackAlias({
['@components']: path.resolve(__dirname, 'src/components')
})
);
Modify Babel Configuration
This feature allows you to modify the Babel configuration. In this example, the 'babel-plugin-styled-components' plugin is added to the Babel configuration.
const { override, addBabelPlugin } = require('customize-cra');
module.exports = override(
addBabelPlugin('babel-plugin-styled-components')
);
Add PostCSS Plugins
This feature allows you to add PostCSS plugins to the configuration. In this example, the 'postcss-preset-env' plugin is added.
const { override, addPostcssPlugins } = require('customize-cra');
module.exports = override(
addPostcssPlugins([require('postcss-preset-env')])
);
react-app-rewired is a package that allows you to override Create React App configurations without ejecting. It is similar to customize-cra but provides a more general approach to modifying CRA configurations. While customize-cra provides specific functions for common modifications, react-app-rewired allows for more manual and flexible configuration changes.
CRACO (Create React App Configuration Override) is another package that allows you to customize Create React App configurations without ejecting. It is similar to customize-cra but offers a more structured and extensible way to modify CRA configurations. CRACO supports plugins and provides a more organized way to manage configuration changes.
This project provides a set of utilities to customize the Create React App v2
configurations leveraging react-app-rewired
core functionalities.
⚠️ make sure you have react-app-rewired installed. You need to use this project with react-app-rewired
; be sure to read their docs if you never have. The code in this project, documented below, is designed to work inside of react-app-rewired
's config-overrides.js
file.
npm install customize-cra --save-dev
yarn add customize-cra --dev
"Stuff can break" - Dan Abramov
Using this library will override default behavior and configuration of create-react-app, and therefore invalidate the guarantees that come with it. Use with discretion!
To start, this project will export methods I need for what I'm using CRA for, but PRs will of course be welcome.
The functions documented below can be imported by name, and used in your config-overrides.js
file, as explained below.
Adds a babel plugin. Whatever you pass for plugin
will be added to Babel's plugins
array. Consult their docs for more info.
Note that this rewirer will not add the plugin to the yarn test
's Babel configuration. See useBabelRc()
to learn more.
A simple helper that calls addBabelPlugin
for each plugin you pass in here. Make sure you use the spread operator when using this, for example
module.exports = override(
disableEsLint(),
...addBabelPlugins(
"polished",
"emotion",
"babel-plugin-transform-do-expressions"
),
fixBabelImports("lodash", {
libraryDirectory: "",
camel2DashComponentName: false
}),
fixBabelImports("react-feather", {
libraryName: "react-feather",
libraryDirectory: "dist/icons"
})
);
Adds a babel plugin. Whatever you pass for preset
will be added to Babel's preset
array. Consult their docs for more info.
Note that this rewirer will not add the preset to the yarn test
's Babel configuration. See useBabelRc()
to learn more.
A simple helper that calls addBabelPreset
for each preset you pass in here. Make sure you use the spread operator when using this, for example
module.exports = override(
...addBabelPresets([
[
"@babel/env",
{
targets: {
browsers: ["> 1%", "last 2 versions"]
},
modules: "commonjs"
}
],
"@babel/preset-flow",
"@babel/preset-react"
])
);
Overwites the include
option for babel loader, for when you need to transpile a module in your node_modules
folder.
module.exports = override(
babelInclude([
path.resolve('src'), // make sure you link your own source
path.resolve('node_modules/native-base-shoutem-theme'),
path.resolve('node_modules/react-navigation'),
path.resolve('node_modules/react-native-easy-grid')
])
);
Adds the babel-plugin-import plugin. See above for an example.
Add decorators in legacy mode. Be sure to have @babel/plugin-proposal-decorators
installed.
Use a .babelrc file for Babel configuration.
Does what it says. You may need this along with addDecoratorsLegacy
in order to get decorators and exports to parse together.
If you want use @babel/plugin-proposal-decorators
with EsLint, you can enable useEslintRc
, described below, with the follow configuration in your .eslintrc
or package.json
:
{
"extends": "react-app",
"parserOptions": {
"ecmaFeatures": {
"legacyDecorators": true
}
}
}
Causes your .eslintrc file to be used, rather than the config CRA ships with.
Updates Webpack eslint-loader to lint both .js(x) and .ts(x) files and show linting errors/warnings in console.
Adds the provided alias info into webpack's alias section. Pass an object literal with as many entries as you'd like, and the whole object will be merged in.
Adds the bundle visualizer plugin to your webpack config. Be sure to have webpack-bundle-analyzer
installed. By default, the options passed to the plugin will be:
{
"analyzerMode": "static",
"reportFilename": "report.html"
}
You can hide this plugin behind a command line flag (--analyze
) by passing true
as second argument.
addBundleVisualizer({}, true);
Causes your .babelrc (or .babelrc.js) file to be used, this is especially useful
if you'd rather override the CRA babel configuration and make sure it is consumed
both by yarn start
and yarn test
(along with yarn build
).
// config-overrides.js
module.exports = override(
useBabelRc()
);
// .babelrc
{
"presets": ["babel-preset-react-app"],
"plugins": ["emotion"]
}
{
analyzerMode: "static",
reportFilename: "report.html"
}
which can be overridden with the (optional) options argument.
Adjusts Workbox configuration. Pass a function which will be called with the current Workbox configuration, in which you can mutate the config object as needed. See below for an example.
adjustWorkbox(wb =>
Object.assign(wb, {
skipWaiting: true,
exclude: (wb.exclude || []).concat("index.html")
})
);
First, install less
and less-loader
packages:
yarn add less
yarn add --dev less-loader
or:
npm i less
npm i -D less-loader
After it's done, call addLessLoader
in override
like below:
const { addLessLoader } = require("customize-cra");
module.exports = override(addLessLoader(loaderOptions));
loaderOptions
is optional. If you have Less specific options, you can pass to it. For example:
const { addLessLoader } = require("customize-cra");
module.exports = override(
addLessLoader({
strictMath: true,
noIeCompat: true
})
);
Check Less document for all available specific options you can use.
Once less-loader
is enabled, you can import .less
file in your project.
To use these plugins, import the override
function, and call it with whatever plugins you need. Each of these plugin invocations will return a new function, that override
will call with the newly modified config object. Falsy values will be ignored though, so if you need to conditionally apply any of these plugins, you can do so like below.
For example
const {
override,
addDecoratorsLegacy,
disableEsLint,
addBundleVisualizer,
addWebpackAlias,
adjustWorkbox
} = require("customize-cra");
const path = require("path");
module.exports = override(
addDecoratorsLegacy(),
disableEsLint(),
process.env.BUNDLE_VISUALIZE == 1 && addBundleVisualizer(),
addWebpackAlias({
["ag-grid-react$"]: path.resolve(__dirname, "src/shared/agGridWrapper.js")
}),
adjustWorkbox(wb =>
Object.assign(wb, {
skipWaiting: true,
exclude: (wb.exclude || []).concat("index.html")
})
)
);
If you want CRA 2 to work with MobX, use the addDecoratorsLegacy
and disableEsLint
.
To override the webpack dev server configuration, you can use the overrideDevServer
utility:
const {
override,
disableEsLint,
overrideDevServer,
watchAll
} = require("customize-cra");
module.exports = {
webpack: override(
// usual webpack plugin
disableEsLint()
),
devServer: overrideDevServer(
// dev server plugin
watchAll()
)
};
When applied, CRA will watch all the project's files, included node_modules
.
To use it, just apply it and run the dev server with yarn start --watch-all
.
watchAll();
FAQs
[![All Contributors](https://img.shields.io/badge/all_contributors-17-orange.svg?style=flat-square)](#contributors-)
The npm package customize-cra receives a total of 108,813 weekly downloads. As such, customize-cra popularity was classified as popular.
We found that customize-cra demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.