samedi 20 avril 2019

Laravel - can't get any package to work using Laravel Mix

I really can't figure out how to include JS packages in my Laravel project! I've been trying to install simple packages for HOURS and I just can't figure out how it works.

For example: Let's say I want to install Bootstrap Confirmation (http://bootstrap-confirmation.js.org/).

I install it like this from my project root folder:

$ npm install bootstrap-confirmation2

After that my package.json file looks like this:

{
    "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": {
        "axios": "^0.18",
        "bootstrap": "^4.0.0",
        "cross-env": "^5.2.0",
        "jquery": "^3.2",
        "laravel-mix": "^4.0.7",
        "lodash": "^4.17.5",
        "popper.js": "^1.15.0",
        "resolve-url-loader": "^2.3.1",
        "sass": "^1.15.2",
        "sass-loader": "^7.1.0",
        "vue": "^2.5.17",
        "vue-template-compiler": "^2.6.10"
    },
    "dependencies": {
        "@types/bootbox": "^4.4.36",
        "bootbox": "^5.1.2",
        "bootstrap-confirmation2": "^4.1.0"
    }
}

I then add the package to app.js:

require('bootstrap-confirmation2');

And my webpack.mix.js looks like this:

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

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css');

After that, I run this from my console:

npm run dev

And if I try to use my (Bootstrap confirmation) package like this:

<button class="btn btn-default" data-toggle="confirmation">Confirmation</button>

it doesn't work. No errors, nothing.

I know the problem is not in the package but in the way I try to install packages and including them in my app.js file that is loaded once I open the app.

I checked the 'Network' tab in Chrome dev tools, checked that 'app.js' is loaded, searched the file and the package I installed and included is INSIDE the JS file and it still doesn't work!

Any help is greatly appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire