0

I am trying to setup bootstrap using web pack, but it's not giving the auto prefixed version of CSS when I run the build.

In custom.scss I have added my customizations and then imported bootstrap from my node modules.

This is my webpack.config.js

module.exports = {
entry: {
    scss: ['./src/scss/custom.scss']
},
module: {
    rules: [    
    {
    test: /\.scss$/,
    loader: ExtractTextPlugin.extract({
        fallback: 'style-loader',
        use: [{
            loader: 'css-loader',
            options: {
                minimize: false,
                autoprefixer: true,
                sourceMap: true,
                importLoaders: 1
            }
        }, 'postcss-loader', 'sass-loader']
    })
   }
  ]
 },
 plugins: [
    new ExtractTextPlugin({
            filename: './assets/css/style.css',
            disable: false,
            allChunks: true
        }),
  ]
}

this is my postcsss.config.js

module.exports = {
  plugins: function () {
        return [
            require('precss'),
            require('autoprefixer')
        ];
    }
}

When I build, all the bootstrap css is getting added to style.css but it's not giving the auto prefixed css file. Vendor prefix is a must because it's BS4 so flex is there.

Thanks.

Ganesh Kumar G
  • 185
  • 2
  • 16

1 Answers1

1

This is my configuration for bootstrap v4

webpack.config.js (for production)

test: /\.scss$/,
        exclude: /node_modules/,
        use: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: 'css-loader!resolve-url-loader!postcss-loader!sass-loader',
        }),

webpack.config.js (for development)

test: /\.scss$/,
        exclude: /node_modules/,
        use: [
            {
                loader: 'style-loader',
            },
            {
                loader: 'css-loader',
                options: {
                    sourceMap: true,
                },
            },
            {
                loader: 'resolve-url-loader',
            },
            {
                loader: 'postcss-loader',
                options: {
                    sourceMap: true,
                },
            },
            {
                loader: 'sass-loader',
                options: {
                    sourceMap: true,
                },
            },
        ],

postcss.config.js

module.exports = {
  plugins: [
    require('postcss-cssnext')({
      browserslist: [
        '> 1%',
        'last 2 versions',
      ],
    }),
  ],
};

And in my main.scss I import bootstrap like this

@import '~bootstrap/scss/bootstrap';

The tilde (~) sign tells webpack to import from node_modules

Also you will need to install some packages if you are planning to use above config

  • postcss-cssnext
  • resolve-url-loader

I use post-cssnext insted of autoprefixer.

Gautam Naik
  • 8,990
  • 3
  • 27
  • 42