My Laravel project with multiple Vue.js applications. The project directory structure is as follows:
├── public
│ ├── js
│ │ ├── app_the_first.js
│ │ ├── app_the_second.js
├── resources
│ ├── assets
│ │ ├── js
│ │ │ ├── the_first_components
│ │ │ │ ├── // Vue component files
│ │ │ ├── the_second_components
│ │ │ │ ├── // Vue component files
│ │ │ ├── app_the_first.js
│ │ │ ├── app_the_second.js
│ ├── sass
│ │ ├── app_the_first.js
│ │ ├── app_the_second.js
├── views
│ ├── the_first.blade.php
│ ├── the_second.blade.php
├── package.json
├── webpack.mix.js
when I change the_first_component and run npm run dev/prod, both app_the_first.js and app_the_second.js are re-build
My webpack.mix.js
const mix = require('laravel-mix');
mix.js('resources/assets/js/app_the_first.js', 'public/js').vue()
.sass('resources/assets/sass/app_the_first.scss', 'public/css');
mix.js('resources/assets/js/app_the_second.js', 'public/js').vue()
.sass('resources/assets/sass/app_the_second.scss', 'public/css');
I want to rebuild the app_the_first.js only, not rebuild the app_the_second.js
Laravel Mix does not natively support building selected assets out of the box because running
npm rundev ornpm run prodwill trigger all tasks defined inwebpack.mix.js. You can define an environment variable to specify which app you want to build:Then, you can use an environment variable when running your npm scripts to specify which app to compile:
modify your package.json scripts to include these new commands:
Now you can use npm run dev-first to only compile the assets for the first app, and npm run dev-second for the second app.
If you don’t have cross-env installed, you can install it with: