getriebesand's avatar

Laravel-mix with Modules or packages

I have splitted my app into modules. Every module has its own asset folder where I will putt the js and scss files. I want to haven one compiled asset per module and type in the public folder. For example the user module should have an public/js/user.js and an public/css/user.css file. But only one vender.js for alle modules.

How can I do that with laravel-mix? I want to run "npm run watch" and mix should watch all my files in the modules. But I don't want to manage all modules in the webpack.mix.js file. Is it possible that mix search in the modules folder if it finds a webpack.mix.js or an json file and include that?

0 likes
2 replies
t0ne's avatar

I would also like to know...

getriebesand's avatar
getriebesand
OP
Best Answer
Level 1

answer my own question :)

If you have a folder Modules, and for example the user module in Modules/User and every Module has Assets like Modules/User/Asset/js/user.js and scss/user.scss, you can do it like that:

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


const moduleFolder = './Modules';
    
const dirs = p => fs.readdirSync(p).filter(f => fs.statSync(path.resolve(p,f)).isDirectory())

let modules = dirs(moduleFolder);

modules.forEach(function(m){
  let js = path.resolve(moduleFolder,m,'Asset','js', m.toLowerCase()+'.js');
  mix.js(js);
  let scss = path.resolve(moduleFolder,m,'Asset','scss', m.toLowerCase()+'.scss');
  mix.sass(scss);
});

6 likes

Please or to participate in this conversation.