How to build client template for production?

I’m trying to build the client template for production. The webpack.config.js already contains a lot of definitions that are related to production. However the package.json doesn’t provide a script and using webpack -p on the [client template 4.0.3]<INTERNAL_LINK> yields an error:

TypeError: Cannot read property 'profile' of undefined`

Using the command line webpack -p --env.profile="production" yields another error:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration[1].entry should be one of these:
   function | object { <key>: non-empty string | [non-empty string] } | non-empty string | [non-empty string]
   -> The entry point(s) of the compilation.
   Details:
    * configuration[1].entry should be an instance of function
      -> A Function returning an entry object, an entry string, an entry array or a promise to these things.
    * configuration[1].entry should not be empty.
      -> Multiple entry bundles are created. The key is the chunk name. The value can be a string or an array.
    * configuration[1].entry should be a string.
      -> An entry point without name. The string is resolved to a module which is loaded upon startup.
    * configuration[1].entry should be an array:
      [non-empty string]

So what command line should be used to build the client template for production?

For building a production build with the BAP Client template you should use:

NODE_ENV="production" webpack --config webpack.config.js -p

However, this assumes that you template was already build with the provided scripts initialize and compile. With this I was able to build a production build for 4.0.3.

Please note also that you have to adapt your webpack configuration in case you do not have any static pages. This is typically the cause of the error you presented.

Thanks, that helps a bit. Build runs through now, but it uses "mode": "development" in the resulting configs. I’ve found out that the variable isProduction initialized at the beginning is undefined due to process.env.NODE_ENV being undefined too. I’m on node v8.16.0 and npm 6.4.1. Do you know what the problem there is?

Seems like it’s this problem: https://github.com/webpack/webpack/issues/2537 - But i’m unable to make something usefull out of this flood of comments there. The closing comment of that issue indicates that webpack -p --env.profile="product" --mode="production" should solve the issue, but the isProduction variable is still undefined then.

You can of cause also adapt your build to your needs. :wink:

@stefan-cold-haze true, but by providing the production related config of webpack, didn’t the a12 team commit to provide at least something in the template that should be buildable in production mode?

@andreas-fresh-mesa You are right. I double checked our current template and it does not support any production profile. However, we enable production build by the standard convention of setting NODE_ENV.

Production deployments will vary in many ways, but a standard convention when deploying in production is to define an environment variable called NODE_ENV and set its value to “production” .

I adapted my answer. :wink: