A webpack 5 configuration for fast development and production ready optimizations.
(For webpack 4 compatible config, use @anansi/webpack-config@5.4.1)
yarn add --dev webpack webpack-cli webpack-dev-server react-refresh @anansi/webpack-config
or
npm install --save-dev webpack webpack-cli webpack-dev-server react-refresh @anansi/webpack-config
If using sass, be sure to install it as well
yarn add --dev sass
/webpack.config.js
const { makeConfig } = require('@anansi/webpack-config');
// See #options below
const options = {
basePath: 'src',
buildDir: 'generated_assets/',
};
module.exports = { options };
module.exports = makeConfig(options);
Advanced configuration /webpack.config.js
const { makeConfig } = require('@anansi/webpack-config');
// See #options below
const options = {
basePath: 'src',
buildDir: 'generated_assets/',
};
module.exports = { options };
const baseConfig = makeConfig(options);
module.exports = (env, argv) => {
const config = baseConfig(env, argv);
// Config is fully available for modification
// Adding any custom plugins is simple
config.plugins.push(
new CspHtmlWebpackPlugin()
);
return config;
}
See Options for more options to configure
/package.json
{
"scripts": {
"start": "webpack serve --mode=development",
"build": "webpack --mode=production",
"build:server": "webpack --mode=production --target=node",
"build:analyze": "webpack --mode=production --env analyze",
"build:profile": "webpack --mode=production --env profile",
"test:pkg": "webpack --env check=nobuild"
}
}
(--env
requires webpack-cli >= v4)
See cmd-line-arguments for more detail.
See ENV to use environmental variables to customize builds
/tsconfig.js
{
"compilerOptions": {
"types": ["@anansi/webpack-config/types"],
}
}
npm install --dev storybook @storybook/react @anansi/storybook
Set @anansi/storybook as the framework
/.storybook/main.js
module.exports = {
framework: {
name: '@anansi/storybook'
},
};
yarn add --dev jest @anansi/jest-preset
/jest.config.js
module.exports = {
preset: "@anansi/jest-preset"
}
Install react-refresh as a dev-dependency in your project and it will automatically
be detected and enabled. Be sure to use the anansi babel
or include react-refresh/babel
in your own babel configuration.
yarn add --dev react-refresh
Production builds optimize all files for size, delivery and runtime performance
Development optimized for quick iteration and debuggability
- JavaScript
- Styling (CSS)
-
SCSS with CSS modules
-
By default both .css and .scss files work as CSS Modules
.button { border-radius: 6px; color: black; background: gray; }
import * as styles from './myfile.scss'; export default function MyComponent() { return <div className={styles.button}>Hello world</div>; }
-
Export SCSS variables into javascript via icss
:export { bodyColor: $body-color; backgroundColor: $background-color; }
-
Provide variables/mixins to every sass file by adding sassResource
const { makeConfig } = require('@anansi/webpack-config'); const options = { basePath: 'src', buildDir: 'dist/', sassResources: [`${__dirname}/src/style/export.scss`], }; module.exports = makeConfig(options); module.exports.options = options;
-
Apply global styles to every file in globalStyleDir
const { makeConfig } = require('@anansi/webpack-config'); const options = { basePath: 'src', buildDir: 'dist/', globalStyleDir: 'style', }; module.exports = makeConfig(options); module.exports.options = options;
-
- CSS in JS via Linaria
-
SCSS with CSS modules
- Media
- All font formats
- Any media files
-
svg|png|apng|jpg|avif|gif|ico|webp|cur|ani|otf|eot|woff2|woff|ttf|pdf|mp4|webm|wav|mp3|m4a|aac|oga as file urls anywhere
-
SVG as file urls or components
import starUrl, { ReactComponent as Star } from './star.svg' const App = () => ( <div> <img src={starUrl} alt="star" /> <Star /> </div> )
-
- Raw string data
- .md, .txt
- JSON as POJO
- HTML via html-loader
If set, will build a static treemap visualization of your packages. Highly recommended to run in production mode to get accurate results.
webpack --mode=production --env analyze
If set will run package checks to check for duplicates or ciruclar dependencies. Set equal to 'nobuild' for a standalone run where build output is not needed.
Examples:
webpack --mode=production --env check
or webpack --env check=nobuild
If set, will enable React DevTools Profiler. This feature is only available in production mode since it is enabled in development by default.
webpack --mode=production --env profile
Skips minification. This is useful when trying to debug production code.
webpack --mode=production --env readable
To target node instead of default of web. This is useful when building a node server for SSR alongside the client bundle.
webpack --mode=production --target=node
This is useful for diffing bundle filesizes as it removes cache busting filenames - keeping the name the same as their contents change.
For example compressed-size-action can track bundle size changes.
webpack --mode=production --env nohash
Environmental variable control is sometimes useful in CI pipelines to distinguish between various deploy targets.
Sets domain of served files. This is useful to distinguish between different build environments that will serve assets from different locations like a CDN.
Serves as first half of publicPath
Note: dev mode sets its own path based assets serving is completely controlled by webpack-dev-server.
Forms the second half of the publicPath. Can be useful when assets are served in subdirectories as opposed to custom domains like in the case of CDNs.
Applies to webpack-dev-server only
HTTPS=true
enables self-signed cert
SSL_CRT_FILE
and SSL_KEY_FILE
enable using certificates stored in files
Setting to 'true' will disable all hot reloading functionality (only enabled by default in dev mode).
Sets webpack cache type.
Pass these to makeConfig.
Use this option to compile for libraries rather than web applications.
Options are from https://webpack.js.org/configuration/output/#outputlibrary
const myConfig = makeConfig({
library: {
type: 'commonjs2',
},
});
Regex to match libraries to include in the normal build process. This is useful for
locally developed modules or yarn workspaces
. Not this should point to the installed
location, rather than the actual target. Note you'll need both the positive and negative
regexes.
libraryExclude defaults to /node_modules/
, which will exclude libraries from expensive
and potentially incorrect processing from babel loaders.
With a monorepo layout like so
/
/packages
/package-a
/package-b
/myapp
webpack.config.js
Where package-a
and package-b
are namespaced under @myspacespace
:
const myConfig = makeConfig({
libraryInclude: path.resolve(__dirname, '../packages'),
libraryExclude: /node_modules(?!\/(@mynamespace\/))/,
});
Marks the base directory inside the package for javascript source files. This is used to make it easy to import from the root.
Example:
-package.json
-/src
-/components
-/pages
-/utils
-network.js
Then you can do
import fetch from 'network';
from any file.
babelRoot
should be where the root babel configuration file is in your repo. Usually this is CWD, but while setting up a monorepo with multiple babel configurations, you may need to change this value.
Root path should be the root path of your project. Usually where your package.json and webpack.config.js are. This defaults to the current working directory you are running commands from. However, if you need to run things from another directory, you can send __dirname into this option from your webpack.config.js.
Output directory for production build files
Output directory for production server builds. Used when using --target=node
cli option.
Override the mode
If specified, uses html-webpack-plugin to build a static html file including all assets. Using this makes development easy, and allows for cheap static deploys to CDNs like S3+cloudfront.
SVG files are optimized automatically in production builds to minimize size. SVGO is used for this. Set this to specify specific options. Alternatively set to false to disable SVGO altogether. (Note: SVGO will never run in dev mode)
SVGR enables importing SVG as a file or a component. This option allows further customizing how that is achieved.
Can configure how wyw-in-js operates, and thus control things like linaria. Set to false
to disable wyw-in-js altogether. Note that
wyw-in-js has its own config files it can use, and it is recommended to use those instead.
Can configure how MiniCssExtractPlugin operates. Set to false
to disable CSS extraction altogether.
Enabled by default. Uses any module resolution specifications like aliases in tsconfig
.
Set to false
to disable; or set to object to configure the options.
Configuring tsconfig path options
If specified, will preload web fonts. Choice
determines the rel
attribute of the link
tag.
This usually provides benefits when serving assets over HTTP/2. If using HTTP1.1 this is typically detrimental.
Customize how to analyze your bundles
Determines the filename for the stats file that includes mappings to all filenames.
Override any babel loader specific options.
Any extra loaders to use on JavaScript/TypeScript files.
Customize css module options.
Directory where only global styles are defined (no css modules)
Set to false
to disable
Used to customize terser in prod builds.
false
disables sass altogether.
Otherwise, these configure the sass-loader
For instance, using sass-embedded can be done like so:
/webpack.config.js
const { makeConfig } = require('@anansi/webpack-config');
// See #options below
const options = {
sassOptions: {
implementation: require("sass-embedded"),
}
};
module.exports = { options };
module.exports = makeConfig(options);
resources
option from https://github.com/shakacode/sass-resources-loader#readme
This is useful to specify global variables and mixins to be included in every sass file.
Add @anansi/webpack-config/types
to the types in tsconfig.json
{
"compilerOptions": {
"types": ["@anansi/webpack-config/types"],
}
}
This makes all imports of supported formats typed correctly, including svgs, media files and workers.
e.g.,
import * as plain from './plain.css';
import angleDownUrl, {
ReactComponent as AngleDown,
} from './angle-down-solid.svg';
const worker = new Worker(new URL('./echo.js', import.meta.url));
worker.postMessage({ message: 'rendered' });
export default function MyComponent() {
return (
<>
<AngleDown className={plain.svg} />
<img src={angleDownUrl} />
</>
);
}
Note: Importing workers using URL is now recommended. However, it means the file cannot be named .worker.(js|ts) This is to keep backwards compatibility with workers imported via worker-loader
When testing modules that use Linaria, it's important to add the wyw-in-js babel preset to the babel config. This is because webpack is not run, and thus will not properly process the embeds.
- Install linaria:
yarn add --dev @linaria/core @linaria/react @wyw-in-js/babel-preset
- Add
@wyw-in-js
to babel presets.
module.exports = {
presets: [
'@anansi',
'@wyw-in-js',
],
};