Loading ...

What's your function in life?

Ours is writing random blog articles about web development, TYPO3 and more.

Running bootstrap 4 with webpack

For this website I was trying to figure out how to get bootstrap 4 running via webpack, and came up with the following. This is most likely not a best practice but is what is currently working on my machine.

File: webpack.config.js

 

const webpack = require('webpack');
const path = require("path");

module.exports = {
    mode: 'development',
    entry: ['./js/index.js', './scss/layout.scss'],
    output: {
        filename: 'scripts.js',
        path: path.resolve(__dirname, '../Resources/Public/JavaScript')
    },
    module: {
        rules: [
            {
                test: /\.(css)/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader'
                    }
                ]
            },
            {
                test: /\.(scss)$/,
                use: [
                    {
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader'
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            plugins: function () {
                                return [
                                    require('autoprefixer')
                                ];
                            }
                        }
                    },
                    {
                        loader: 'sass-loader'
                    }
                ]
            }
        ],

    },
    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery",
            Tether: "tether",
            "window.Tether": "tether",
            Popper: ['popper.js', 'default'],
            Alert: "exports-loader?Alert!bootstrap/js/dist/alert",
            Button: "exports-loader?Button!bootstrap/js/dist/button",
            Carousel: "exports-loader?Carousel!bootstrap/js/dist/carousel",
            Collapse: "exports-loader?Collapse!bootstrap/js/dist/collapse",
            Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown",
            Modal: "exports-loader?Modal!bootstrap/js/dist/modal",
            Popover: "exports-loader?Popover!bootstrap/js/dist/popover",
            Scrollspy: "exports-loader?Scrollspy!bootstrap/js/dist/scrollspy",
            Tab: "exports-loader?Tab!bootstrap/js/dist/tab",
            Tooltip: "exports-loader?Tooltip!bootstrap/js/dist/tooltip",
            Util: "exports-loader?Util!bootstrap/js/dist/util",
        })
    ]
};

 

File package.json:

 

{
  "name": "site",
  "description": "",
  "repository": {
    "type": "git",
    "url": ""
  },
  "author": "No use for a name",
  "version": "1.0.0",
  "license": "MIT",
  "scripts": {
    "build": "webpack --config webpack.config.js",
    "watch": "webpack --config webpack.config.js --watch"
  },
  "dependencies": {
    "autoprefixer": "^9.1.5",
    "bootstrap": "^4.1.3",
    "css-loader": "^1.0.0",
    "jquery": "^3.3.1",
    "node-sass": "^4.9.3",
    "popper.js": "^1.14.4",
    "postcss-loader": "^3.0.0",
    "prismjs": "^1.15.0",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.23.0",
    "webpack": "^4.19.0",
    "webpack-cli": "^3.1.0"
  }
}

 

File index.js:

 

import 'bootstrap';

 

File layout.scss:

 

@import 'variables.scss';
@import "~bootstrap/scss/bootstrap";

 

Including in TYPO3:

To finally use that in TYPO3 I'm including the generated scripts.js file as JavaScript file via TypoScript:

 

page.includeJSFooter {
   site_scripts = EXT:site/Resources/Public/JavaScript/scripts.js
}

 

 

Comments

No comments

Write comment

* These fields are required