Dynamically change Webpack build path via CLI

I’m currently in a project where we’re looking to dynamically build styles using Sage 9 and webpack. Here’s a brief run down of requirements:

  • Build styles as normal locally using yarn run build - This works
  • On the app server build a variables.scss file dynamically based on options stored in the DB - This works
  • Build a new stylesheet on the app server with yarn / webpack. Then upload that to /wp-content/uploads/SITE-ID/dist/ - Help
  • Load that stylesheet instead of the one found at theme level - This works

Everything is working as expected until I want to pass the new path to the webpack build process dynamically.

The ideal build command would be:
webpack --progress -p --config resources/assets/build/webpack.config.js --env.custompath="/wp-content/uploads/SITE-ID/dist/"

Where according to webpack documentation env.custompath should equal /wp-content/uploads/SITE-ID/dist/.

Unfortunately env.custompath is always undefined. But in this discussion it appears to be possible. Can anyone shine a light on what I’m missing here?

Just to clarify I’m looking to change the dist output path dynamically only - all other aspects of the build process I’m cool with

Thanks a lot in advance

In my case I fixed the same problem another way:
I added new env custom_prod.env and added new variable outputPath.
`var merge = require(‘webpack-merge’)
var prodEnv = require(’./prod.env’)
var path = require(‘path’)

module.exports = merge(prodEnv, {
outputPath: path.resolve(__dirname, ‘…/…/open/public_html/mobile/’)
})`
After that inside build settings for prod I included new custom_prod.env file
var customEnv = require(’./custom_prod.evn’)
and set new path like this
index: path.resolve(customEnv.outputPath, ‘./dist/index.html’),
assetsRoot: path.resolve(customEnv.outputPath, ‘./dist’)
This file won’t be under version control so anyone can setup own build path. But under the version will be for custom_prod.evn.example so if someone want to build he/she need to copy and rename an example file and change outputPath.