
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
configs-webpack-plugin
Advanced tools
A simplified AoT runtime config solution for your webpack builds
A simplified AoT runtime config solution for your webpack builds
yarn add --dev configs-webpack-plugin
This is a webpack plugin that helps simplify the management and orchestration of multi-environment or multi-target webpack bundles, where you want one bundle to travel between environments. That treeshakes your config to only what is being consumed.
A use case could be where you have a traditional staging and production
environment. Where you might to send api traffic to a different server in
staging than in production, but instead of sending in a target name, and
if else'ing that domain.
You can with this plugin; simply: import { apiDomain } from '@my-org/config,
and have that made avaliable to the bundle at runtime irrespective of config
being ran.
Few benefits:
I do this by collecting all request's that you're wanting to be a config. So
from the above example; @my-org/config is trapped by webpack and converted
into what I call a Config Query module.
This Config Query module, can either be concatenated by a single consumer of
config, or referenced by all your modules.
This Config Query module then requires a chunked of module called a Config
module, that actually houses the config. The trick here is that this isnt a
traditional async chunk, as it needs to exist AoT! So include the chunk before
the bootstrap chunk.
webpack.config.js
const { RuntimeConfigsPlugin } = require('configs-webpack-plugin');
module.exports = {
entry: 'index.js',
output: {
chunkFilename: '[name]-[contenthash].js',
},
plugins: [
new RuntimeConfigsPlugin({
request: '@my-org/config',
configs: [
{
name: 'staging',
config: { apiDomain: 'https://staging-server.com' },
},
{
name: 'production',
config: { apiDomain: 'https://production-server.com' },
},
],
}),
],
};
Will note that it is important you're using
[name]or something in thechunkFilenameoutput option, as each config chunk will have the same internal id.
| Name | Type | Description |
|---|---|---|
request | string | The import token that you're using to import your configs. Eq. the @my-org/config in import { apiDomain } from '@my-org/config |
configs | { name: string, config: any[] }[] | An array of Config objects a name to json serializable values. That'll be given to the issuing module at runtime. Root values must be object keys. |
also; configs can be a
generator.
The yield'ing is done only once, and deferred to just when it's needed.
There are also hooks, so you can couple it with html-webpack-plugin to inject config chunks. Example
configChunk SyncHook<Options['configs'], Chunk> calls for every
config chunk that will emit.configChunks SyncHook<Options['configs'][], Chunk[]> calls once all
config chunks have been created; coupled with the array of configs. Matching
index with the chunk to config.These get called during the
afterChunkshook; so be sure to tap in themakehook or alike. If you needfilestap in one of theemittype hooks, and then get thefilesproperty from the chunk.
RuntimeConfigsPlugin.getHooks(compilation).configChunks.tap(
My_PLUGIN,
(configs, chunks) => {
// Assuming you're using the example plugin config from above.
expect(chunks).toHaveLength(2);
},
);
FAQs
A simplified AoT runtime config solution for your webpack builds
We found that configs-webpack-plugin 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
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.