Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Mix multiple entry points generates one manifest.js

I currently have a problem trying to use multiple entry points in my Mix file.

// Mix frontend resources.
mix.js('resources/assets/js/app.js', 'public/js')
    .extract([
        'jquery', 'bootstrap', 'aos', 'lity',
    ]);

...

// Mix app resources.
mix.js('resources/assets/app/js/app.js', 'public/app/js');

I have three entry points in my Mix file. One for frontend, backend and my "public app" file. The code above stores my frontend vendor.js and manifest.js file inside public/app/js when it should be inside public/js.

When I then try to reference

<script src="{{ mix('js/manifest.js') }}"></script>
<script src="{{ mix('app/js/app.js') }}"></script>

it throws webpack errors:

Uncaught TypeError: Cannot read property 'call' of undefined
    at __webpack_require__ (manifest.js?id=09ecc9b…:55)
    at Object../node_modules/vue/dist/vue.common.js (app.js?id=6431fd7…:sourcemap:28709)
    at __webpack_require__ (manifest.js?id=09ecc9b…:55)
    at Object../resources/assets/app/js/app.js (app.js?id=6431fd7…:sourcemap:37900)
    at __webpack_require__ (manifest.js?id=09ecc9b…:55)
    at Object.0 (app.js?id=6431fd7…:sourcemap:38015)
    at __webpack_require__ (manifest.js?id=09ecc9b…:55)
    at webpackJsonpCallback (manifest.js?id=09ecc9b…:26)
    at app.js?id=6431fd7…:sourcemap:1

Is there currently a way to use multiple entry points in a Mix file?

like image 651
Chris Avatar asked Jul 12 '17 01:07

Chris


People also ask

What does laravel mix do?

Laravel Mix, a package developed by Laracasts creator Jeffrey Way, provides a fluent API for defining webpack build steps for your Laravel application using several common CSS and JavaScript pre-processors.

What is webpack mix JS?

webpack. mix. js is where you compile your js. Example if you run php artisan preset vue npm install && npm run dev. you would have the necessary scaffolding for vue.

Where is webpack mix JS in laravel?

It should be in the root folder of your project, look at the laravel repo here.


1 Answers

In order to create a mix file for backend and frontend each (and other entry points if needed), adjust package.json:

"scripts": {
    "dev": "npm run development",
    "development": "cross-env process.env.section=website NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "cross-env process.env.section=website NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "cross-env process.env.section=website NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "admin-dev": "npm run admin-development",
    "admin-development": "cross-env process.env.section=admin NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "admin-watch": "cross-env process.env.section=admin NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "admin-prod": "npm run admin-production",
    "admin-production": "cross-env process.env.section=admin NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },

webpack.mix.js:

let mix = require('laravel-mix');

if (process.env.section) {
  require(`${__dirname}/webpack.mix.${process.env.section}.js`);
}

create webpack.mix.website.js:

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

mix
  .setPublicPath(path.normalize('public_html/assets/website'))
  .less('resources/assets/website/less/website.less', 'css/style.css')
  .options({
    processCssUrls: false
  })
  .js('resources/assets/website/js/website.js', 'js/global.js')
;

create webpack.mix.admin.js:

let mix = require('laravel-mix');

mix
  .setPublicPath(path.normalize('public_html/assets/admin'))
  .options({
    processCssUrls: false
  })
  .js('resources/assets/admin/js/admin.js', 'js/global.js')
  .less('resources/assets/admin/less/admin.less', 'css/style.css')
;

This is currently the only way to create multiple manifest files etc. for multiple entry points on different directory levels.

like image 76
Chris Avatar answered Nov 16 '22 03:11

Chris