question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to set a custom webpack.mix.js

See original GitHub issue
  • Laravel Mix Version: 0.9.0
  • Node Version 6.10.0
  • NPM Version 4.2.0
  • OS: Mac OS

Description:

This is a request to be able to specify a custom webpack.mix.js file at runtime. for example

It’s important to note that this change is transparent and has no BC breaks. if you don’t explicitly use it, it won’t make any difference.

// this will load the webpack.mix.js
npm run dev

// the following will look for a custom.mix.js file instead of the webpack.mix.js file
// if not found it wont error just like webpack.mix.js works
npm run dev -- --env.mixfile=custom.mix

This allows cases where there are more than one site to compile assets for.

For example, in our case we have 4 sites, each has it’s own compile process and code, we have everything in webpack.mix.js file, however, it most cases when we release we only want to compile one site not all of them. This change will allow us to split webpack.mix.js into multiple files and call the one we need at the time.

PR is being submitted.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

20reactions
QWp6tcommented, Mar 23, 2017

For your specific use case, you could already do this if you want. Use webpack.mix.js as a single point of entry for Mix, but then pass an env flag as you mentioned.

Given that you have multiple webpack.mix.js files that each correspond to different sites, e.g.,

├── webpack.mix.js        # → Mix entrypoint
├── webpack.mix.site1.js  # → Mix for site1
├── webpack.mix.site2.js  # → Mix for site2
├── webpack.mix.site3.js  # → Mix for site3
└── webpack.mix.site4.js  # → Mix for site4

And you can pass an environment variable to node, e.g.,

npm run dev -- --env.site=site1

Then webpack.mix.js could have something like:

const { mix } = require('laravel-mix');
const { env } = require('minimist')(process.argv.slice(2));

/* do stuff with mix that's common to all sites, like maybe mix.options() */

// load site-specific config
if (env && env.site) {
    require(`${__dirname}/webpack.mix.${env.site}.js`);
}

And webpack.mix.site1.js, site2, site3, etc. could be normal mix config files:

const { mix } = require('laravel-mix');

mix.js('resources/assets/js/app.js', 'public/site1/js')
   .sass('resources/assets/sass/app.scss', 'public/site1/css');

I’m using minimist and sniffing out the --env argument, but as an alternative, you could use cross-env combined with process.env. (Another way of doing it would be to use your own webpack.config.js and export it as a function, but it would require a number of extra steps that make it not worth the effort.)

2reactions
lukas-piercecommented, Jul 22, 2022

The modern solution use --mix-config option:

Using in CLI:

mix --mix-config=webpack-custom.mix.js

Example package.json

{
  "scripts": {
    "dev": "mix", // it will use default webpack.mix.js file
    "dev-custom": "mix --mix-config=webpack-custom.mix.js"
  }
}

Official docs here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ability to set a custom webpack.mix.js · Issue #608 - GitHub
This allows cases where there are more than one site to compile assets for. For example, in our case we have 4 sites,...
Read more >
Compiling Assets (Mix) - The PHP Framework For Web Artisans
Your application's webpack.mix.js file is your entry point for all asset compilation. Think of it as a light configuration wrapper around webpack. Mix...
Read more >
Quick Webpack Configuration | Laravel Mix Documentation
As an example, perhaps you want to add a custom array of modules that should be automatically loaded by webpack. We'll use Laravel...
Read more >
How can I run only running a subset of my webpack.mix.js file
Back to the original question: It is possible to run a subset, but you will need to publish the webpack.config file, copy it...
Read more >
configure Laravel mix mix.webpackConfig({}) with entry points ...
I need to know how to use mix.webpackConfig({}) method to use another webpack configurations like use babel-loader, riot-tag-loader etc. Is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found