Skip to content

Instantly share code, notes, and snippets.

@rap2hpoutre
Last active November 22, 2023 00:30
Show Gist options
  • Save rap2hpoutre/e53728c96f551c42a352f1484bbf4934 to your computer and use it in GitHub Desktop.
Save rap2hpoutre/e53728c96f551c42a352f1484bbf4934 to your computer and use it in GitHub Desktop.
Laravel 5.4: migrate from gulp to webpack
  • Create a webpack.mix.js file in root directory:
const { mix } = require('laravel-mix');

mix.js('resources/assets/js/app.js', 'public/js')
  .sass('resources/assets/sass/app.scss', 'public/css');
  
/* Optional: uncomment for bootstrap fonts */
// mix.copy('node_modules/bootstrap-sass/assets/fonts/bootstrap/','public/fonts/bootstrap');
  • Delete gulpfile.js

  • Replace package.json content with (don't forget to keep your own dependencies):

{
  "private": true,
  "scripts": {
    "dev": "node node_modules/cross-env/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "node node_modules/cross-env/bin/cross-env.js NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "hot": "node node_modules/cross-env/bin/cross-env.js NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "production": "node node_modules/cross-env/bin/cross-env.js NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },
  "devDependencies": {
    "axios": "^0.15.2",
    "bootstrap-sass": "^3.3.7",
    "jquery": "^3.1.0",
    "laravel-mix": "^0.5.0",
    "lodash": "^4.16.2",
    "vue": "^2.0.1"
  }
}
  • Remove require('vue-resource'); if present in assets/js/bootstrap.js

  • Replace:

Vue.http.interceptors.push((request, next) => {
    request.headers.set('X-CSRF-TOKEN', Laravel.csrfToken);

    next();
});

By:

window.axios = require('axios');

window.axios.defaults.headers.common = {
    'X-CSRF-TOKEN': window.Laravel.csrfToken,
    'X-Requested-With': 'XMLHttpRequest'
};
  • Run yarn (or npm install)

  • Run npm run watchto start working.

@michaelsanford
Copy link

Nice write-up! FYI gup-to-webpack.md => gulp-to-webpack.md ?

@thinckx
Copy link

thinckx commented Nov 3, 2017

Thanks!

@Alhiane
Copy link

Alhiane commented Nov 12, 2020

merci bro

@Alhiane
Copy link

Alhiane commented Nov 12, 2020

very important to upgrade laravel mix and take note of @mkjangir also

@Edison0315
Copy link

I had problems with: npm run watch command. So copy/paste a package.json code from another laravel project (6.0) and works it! (The scripts part was the conflict)

{
  "private": true,
  "scripts": {
    "dev": "npm run development",
    "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "npm run development -- --watch",
    "watch-poll": "npm run watch -- --watch-poll",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },
  "devDependencies": {
    "bootstrap-sass": "^3.3.7",
    "gulp": "^3.9.1",
    "jquery": "^3.1.0",
    "laravel-echo": "^1.10.0",
    "laravel-elixir": "^6.0.0-14",
    "laravel-elixir-vue-2": "^0.2.0",
    "laravel-elixir-webpack-official": "^1.0.2",
    "laravel-mix": "^0.10.0",
    "lodash": "^4.16.2",
    "pusher-js": "^7.0.2",
    "socket.io-client": "^3.0.4",
    "vue": "^2.0.1",
    "vue-resource": "^1.0.3"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment