Centralized project configuration for react projects.
yarn add @clearc2/c2-react-config
Install the eslint config and parser package:
yarn add --dev @clearc2/eslint-config-c2-react
Create a .eslintrc
file in the root of your project with the following contents:
{
"extends": [
"@clearc2/c2-react"
]
}
Create a babel.config.js
file in the root of your project with the following contents:
// <project-dir>/babel.config.js
module.exports = require('@clearc2/c2-react-config').babelConfig
Create a postcss.config.js
file in the root your project with the following contents:
module.exports = require('@clearc2/c2-react-config').postCSSConfig
Create a webpack.config.js
file in the root of your project with the following contents:
// <project-dir>/webpack.config.js
const path = require('path')
const {webpackConfig} = require('@clearc2/c2-react-config')
module.exports = (env) => {
env.presetDir = path.join(__dirname, 'webpack')
env.projectDir = __dirname
return webpackConfig(env)
}
This project contains several partial webpack configurations called "presets". These presets are merged together with the webpack-merge utility.
Presets can have both development
and production
versions. This is specified by the --env.mode <mode>
command line
argument when using webpack or webpack-dev-server. Example:
npx webpack --env mode=development
common
presets apply to both development
and production
modes.
Provides the entry points of the project. common
preset enabled by default.
Configures the output. common
preset enabled by default.
Runs the babel-loader
on .js
files. common
preset enabled by default.
Handles font, images, audio files. common
preset enabled by default.
Converts css to js in development. Extracts/optimizes css in production. common
preset enabled by default.
Enables css modules. Use <name>.module.css
file naming. common
preset enabled by default.
Configures webpack-dev-server. development
preset enabled by default.
Uses the html-webpack-plugin
and uses <project-root>/src/index.html
as the template. common
preset enabled by default.
Configures the appropriate source map for development/production. common
preset. Not enabled by default.
Outputs the build progress in the cli. common
preset enabled by default.
Cleans the dist folder before builds using clean-webpack-plugin. common
preset enabled by default.
Minify/uglify output. production
preset enabled by default.
Inspect bundle output with the webpack-bundle-analyzer. Not enabled by default.
Common presets:
input
output
assets
babel
css
css-modules
html
progress
clean
souce-map
- Not enabled by defaultanalyzer
- Not enabled by default
Development presets:
dev-server
Production presets:
optimize
When you created your project's webpack.config.js
, you provided a presetDir
. This is where you can hold your project specific configurations.
For example, if you want to change the webpack-dev-server port:
// <project-dir>/webpack/dev-server.development.js
const {webpackUtils} = require('@clearc2/c2-react-config')
module.exports = (env) => webpackUtils.extendPreset(env, 'dev-server.development',
{
devServer: {
port: 8089
}
}
)
// you could choose not to `extendPreset` and return a completely new configuration.
Presets are functions that accept the env
object and return configuration.
If you have project specific configuration that applies to both development and production, add a <preset-name>.production.js
file. Example:
// <project-dir>/webpack/provide.production.js
const webpack = require('webpack')
module.exports = (env) => ({
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.$': 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default']
})
]
})
And then modify your webpack.config.js
to customize the presets:
// <project-dir>/webpack.config.js
const path = require('path')
const {webpackConfig} = require('@clearc2/c2-react-config')
const {presets} = webpackConfig
// add "provide" preset to common presets
presets.common = presets.common.concat(['provide'])
// add analyzer to inspect bundle on production output
presets.production = presets.production.concat(['analyzer'])
module.exports = (env) => {
env.presetDir = path.join(__dirname, 'webpack')
env.projectDir = __dirname
return webpackConfig(env)
}
Presets will fall back to the production version if a development version is not found in development mode.
There is an example in the example
directory which shows off css modules, a custom preset(provide
), and illustrates how to install bootstrap.