Build configuration plugins

ScandiPWA introduces concept of "build plugins". This is a feature of ScandiPWA extensions.

To create a build plugin - means to create extend the application build configuration. The build plugins can modify: Webpack, Webpack dev-server, Craco configurations. You can also provide commands to run before starting the build.

To create a build plugin

  1. Define an empty object as a value of the scandipwa.build field in your extension's package.json

    1. Populate the configuration object with cracoPlugins field, set it to empty array.

  2. Create a new folder in your extension's root, name it build-config:

    1. Create a new file in it, name it to represent it's build modification purpose

    2. Populate an array of cracoPlugins in your package.json with a path to your new file (relative to the extension root).

  3. Inside of your plugin file, define a module.exports equal to an object. This object may have two keys:

    1. plugin (required) where the plugin implementation will be defined

    2. options (optional) where the plugin options will be defined

  4. Implement a plugin in the object created at the step 3). To do it, follow these guidelines.

You can also use the existing Craco extensions. You can find the list of them here. You can import them and use as the plugin implementation passing into the plugin key value on the step 3).

To define a before run script

  1. Define an empty object as a value of the scandipwa.build field in your extension's package.json

    1. Populate the configuration object with before field, set it to empty string.

  2. Create a new folder in your extension's root, name it build-config:

    1. Create a new file in it, name it to represent before-run script's purpose

    2. Set the before field in your package.json equal to a path to your new file (relative to the extension root).

  3. Inside of your plugin file, define a module.exports equal to a function.

  4. Implement a script based on your needs. No guidelines here, it's up to you!

Build plugin implementation syntax

The plugin syntax of Create ScandiPWA App is based on the Craco (Create React App Configuration Override) plugin syntax.

Each plugin is an object, which might contain following keys (you can combine them):

  • overrideCracoConfig - allows to customize the Craco configuration object.

  • overrideWebpackConfig - allows to customize the Webpack config.

  • overrideDevServerConfig - allows to customize the Webpack dev-server config

The values of this object keys are functions, which implement an override for the configuration. The function may be asynchronous.

Craco configuration plugin

The function overrideCracoConfig allows a plugin override the configuration object before it's process by Craco.

The function must return a valid Craco configuration object. The function will be called with a single object argument having the following structure:

{
    cracoConfig, // The original CRACO configuration object
    pluginOptions, // The plugin options provided by the consumer
    context: {
        env, // The current NODE_ENV (development, production, etc..)
        paths // An object that contains all the paths used by CRA
    }
}

The cracoConfig is passed into you plugin implementation by reference, which means you can modify it's values directly.

Webpack configuration plugin

The function overrideWebpackConfig allows plugin override the Webpack configuration object after it's been customized by Craco.

The function must return a valid Webpack configuration object. The function will be called with a single object argument having the following structure:

{
    webpackConfig, // The webpack config object already customized by craco
    cracoConfig, // The configuration object read from the craco.config.js file provided by the consumer
    pluginOptions, // The plugin options provided by the consumer
    context: {
        env, // The current NODE_ENV (development, production, etc..)
        paths //An object that contains all the paths used by CRA
    }
}

The webpackConfig is passed into you plugin implementation by reference, which means you can modify it's values directly.

Webpack dev-server configuration plugin

The function overrideDevServerConfig let a plugin override the dev server config object after it's been customized by Craco.

The function must return a valid Webpack dev-server configuration object. The function will be called with a single object argument having the following structure:

{
    devServerConfig, // The dev server config object already customized by craco
    cracoConfig, // The configuration object read from the craco.config.js file provided by the consumer
    pluginOptions, // The plugin options provided by the consumer
    context: {
        env, // The current NODE_ENV (development, production, etc..)
        paths, // An object that contains all the paths used by CRA
        allowedHost // Provided by CRA
    }
}

Last updated