Laravel-mix: versioning doesn't work when called inside of an if(production) conditionnal

Created on 11 Feb 2017  路  7Comments  路  Source: JeffreyWay/laravel-mix

Using ^7.0

This works

mix.combine([
  'resources/assets/landing/css/devices/style.css',
  'resources/assets/landing/css/animate.css',
], 'public/dist/app.css').version();

mix.combine([
  'resources/assets/landing/js/main.js',
  'resources/assets/landing/vendor/push-menu/js/jasny-bootstrap.min.js',
], 'public/dist/app.js').version();

if (mix.inProduction) {
  mix.minify();
}

This doesn't work

mix.combine([
  'resources/assets/landing/css/devices/style.css',
  'resources/assets/landing/css/animate.css',
], 'public/dist/app.css');

mix.combine([
  'resources/assets/landing/js/main.js',
  'resources/assets/landing/vendor/push-menu/js/jasny-bootstrap.min.js',
], 'public/dist/app.js');

if (mix.inProduction) {
  mix.minify();
  mix.version();
}

This last one never applies the versioning. I would like to only apply minification and versioning when I run npm run production, not when I run npm run dev

How can I achieve this?

In the first case, it does versioning whether I run dev or production

Most helpful comment

The mix used in webpack.mix.js is not an instance of Mix but a entry to specify webpack config.
You should use

if (mix.config.inProduction) {
    mix.minify();
    mix.version();
}

All 7 comments

The mix used in webpack.mix.js is not an instance of Mix but a entry to specify webpack config.
You should use

if (mix.config.inProduction) {
    mix.minify();
    mix.version();
}

ahh.. I see..

mix.minify(); throws an error now. But I now understand that 'npm run production' has minification implied.. so there's no need to add it manually

the versionning now works inside the if

I was using the version found at the top of https://github.com/JeffreyWay/laravel-mix/blob/master/docs/mix-helpers.md

But I guess this conditionnal is meant for other uses

thanks

@vesper8 That document is right.

Though you likely won't need to reference many of these helpers directly, they, nonetheless, are available to you, should you wish to manually configure/modify the provided webpack.config.js file.

You can see that mix helpers are provided for webpack.config.js if you want to custom it, not for webpack.mix.js.

I know this is closed but just wanted to point out that the use of mix.inProduction() for environment detection is still included on the laravel 5.4 docs. (see: https://laravel.com/docs/5.4/mix#versioning-and-cache-busting)

As of today I could not get the following to work:

if (mix.inProduction()) {
    mix.version();
}

but, as shown above, this works just fine:

if (mix.config.inProduction) {
    mix.version();
}

It may be worth updating the docs to reflect this?

@hootener You are probably using an outdated version of Laravel Mix. inProduction() was introduced in Laravel Mix ^1.0.0 and mix.config.inProduction() no longer works. Documentation is correct.

Still appears to not be working for me with Mix 1.0.7

$~:/var/www$ npm list | grep mix
    +-- [email protected]
    | | |   `-- [email protected]

with the following in my webpack.mix.js

if (mix.inProduction()) {
    mix.version();
}

output of npm run prod is:

$~:/var/www$ npm run prod

> @ prod /var/www
> npm run production


> @ production /var/www
> cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-
modules --config=node_modules/laravel-mix/setup/webpack.config.js

 95% emitting                                                                         

 DONE  Compiled successfully in 11853ms                                                                          2:48:40 PM

      Asset     Size  Chunks                    Chunk Names
 /js/app.js   267 kB       0  [emitted]  [big]  /js/app
 /js/vendor.js  82.4 kB       1  [emitted]         /js/vendor
 /js/manifest.js  1.38 kB       2  [emitted]         /js/manifest
 /css/app.css   171 kB       0  [emitted]         /js/app

With mix 1.0.7 and the following in my webpack.mix.js file, I get:

$~:/var/www$ npm run prod

> @ prod /var/www
> npm run production


> @ production /var/www
> cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-
modules --config=node_modules/laravel-mix/setup/webpack.config.js

/var/www/webpack.mix.js:18
if (mix.config.inProduction) {
          ^

TypeError: Cannot read property 'inProduction' of undefined
at Object.<anonymous> (/var/www/webpack.mix.js:18:15)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/var/www/node_modules/laravel-mix/setup/webpack.config.js:9:1)
at Module._compile (module.js:571:32)

So it looks like even the mix.config approach isn't working with the 1.0.7 version of Mix? Is there something else I'm doing incorrectly here? Any help is appreciated.

Using the latest version 1.7.2, I believe this has changed again.
I had to use the following to apply versioning to production.

if (mix.config.production) {
  mix.version();
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wendt88 picture wendt88  路  3Comments

stefensuhat picture stefensuhat  路  3Comments

pixieaka picture pixieaka  路  3Comments

dtheb picture dtheb  路  3Comments

RomainGoncalves picture RomainGoncalves  路  3Comments