Search code examples
webpack-2npm-scripts

Can I use npm custom flags to set the value of a configuration variable usable in webpack.config?


I'd like to achieve something like this:

npm run build --path custom

in order to set a different output.path that will be used in webpack.config.js

Does something like this exists?


Solution

  • The solution used in the end uses this command:

    DIST=[YOUR-DIRECTORY] npm run build
    

    The webpack.config handles the value of DIST like:

    const target = process.env.DIST
      ? process.env.DIST
      : 'dist';
    

    and in the output we go with: path: path.resolve(__dirname, target)