hpiaia's avatar
Level 33

Mix extract with multiple files

Hey everyone, i have an issue with mix when extracting plugins...

So, i am trying to extract vue and other plugins from the main file and put it on vendor.js but i have a second js file which i dont use vue.

Is there a way to use mix extraction in just one js file?

Heres my webpack.mix.js file

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

// Public site scripts, i dont need vue here.
mix.js('resources/assets/web/js/app-web.js', 'public/assets/web/js');

// Admin panel resources, i want to extract plugins from here
mix.js('resources/assets/panel/js/app-panel.js', 'public/assets/panel/js')
    .extract('vue');

The way it is, mix try to extract vue from both files and i cant use app-web.js alone without linking the manifest and vendor files too.

Hope someone can help me. Thanks!

0 likes
7 replies
1000ml's avatar

I also have this issue. I can't separate backend from frontend when extracting o versioning assets...

gorodezkiy's avatar

After some small research I'm now using two mix config files which are included depending on environment variable and then switched inside custom webpack.config.js with modified Paths:

webpack.config.js

Paths.mix = () => {
    return Paths.root(process.env.MIX_CONFIG);
};

package.json

"scripts": {
    "admin-dev": "node node_modules/cross-env/dist/bin/cross-env.js MIX_CONFIG=admin.mix NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=webpack.config.js",
    "admin-watch": "node node_modules/cross-env/dist/bin/cross-env.js MIX_CONFIG=admin.mix NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=webpack.config.js",
    "admin-hot": "node node_modules/cross-env/dist/bin/cross-env.js MIX_CONFIG=admin.mix NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=webpack.config.js",
    "admin-production": "node node_modules/cross-env/dist/bin/cross-env.js MIX_CONFIG=admin.mix NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=webpack.config.js",
    "dev": "node node_modules/cross-env/dist/bin/cross-env.js MIX_CONFIG=frontend.mix NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=webpack.config.js",
    "watch": "node node_modules/cross-env/dist/bin/cross-env.js MIX_CONFIG=frontend.mix NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=webpack.config.js",
    "hot": "node node_modules/cross-env/dist/bin/cross-env.js MIX_CONFIG=frontend.mix NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=webpack.config.js",
    "production": "node node_modules/cross-env/dist/bin/cross-env.js MIX_CONFIG=frontend.mix NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=webpack.config.js"
  },

admin.mix.js (new config file instead of default webpack.mix.js)

mix
  .setPublicPath(__dirname + '/public/assets/admin')

admin-layout.blade.php

<link rel="stylesheet" href="{{ mix('css/vendor.css', 'assets/admin') }}" />

frontend.mix.js (new config file instead of default webpack.mix.js)

mix
  .setPublicPath(__dirname + '/public/assets/frontend')

frontend-layout.blade.php

<link rel="stylesheet" href="{{ mix('css/vendor.css', 'assets/frontend') }}" />
1 like
FHoulbreque's avatar

Hello @gorodezkiy ,

Currently running the latest version of mix 1.4.2, I get an issue with what you suggests...

In fact, I can't get it to work, and it's related to my custom webpack.config.js

I tried to create mine from the main one and just update the require() lines and the path to point at the right libraries, but it always load the webpack.mix and never my MIX_CONFIG designated files.

Could you help me there ?

Thanks

Colin's avatar

Doesnt work in 5.6.

After copying the webpack.config.js you have to alter the require paths as well.

I suggest people dont go this way.. it feels hacky. Once an update will be available things will break and you forgot how you fixed them in earlier versions.

Colin's avatar

For those coming from google..

I took a closer look.. I found out Paths.js has a mix function:

    mix() {
        return this.root(
            argv.env && argv.env.mixfile ? argv.env.mixfile : 'webpack.mix'
        );
    }

So we simply add an extra parameter to the package.json line:

"admin-dev": "node node_modules/cross-env/dist/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --env.mixfile=admin.mix.js --progress --hide-modules --config=webpack.config.js"

create the file admin.mix.js in the root and it works like a charm!

3 likes

Please or to participate in this conversation.