3

When you use WebPack, you typically have to set an entry point to a .js file. Is this a requirement? I have two scenarios:

  1. I want to convert a .scss to a .css file.
  2. I want to minify images using imagemin.

I don't have or need any JavaScript. How can this be achieved using WebPack? Is that even the right tool? I've done this using Gulp in the past. Does WebPack only make sense when you require images and CSS from your JavaScript files?

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311

2 Answers2

0

Webpack really isn't the right tool if you're just doing CSS stuff. It can be done though I believe. You'll need the 'css-loader', 'style-loader', 'sass-loader' and 'node-sass' packages from npm. Check the documentation for the loaders to see how to add them into your webpack.config.js

My suggestion would be to create a JavaScript file called index.js and set that as your entry point. Then require the stylesheets in that file.

require('stylesheet.css')
require('styles.sass')
//etc

You can then look at configuring extract-text-webpack-plugin which will compile to a .css file for you.

const ExtractTextPlugin = require("extract-text-webpack-plugin");

const extractSass = new ExtractTextPlugin({
    filename: "[name].css"
});

module.exports = {
    entry: "./index.js",
    output: "./index.min.js",
    module: {
        rules: [{
            test: /\.scss$/,
            use: extractSass.extract({
                use: [{
                    loader: "css-loader"
                }, {
                    loader: "sass-loader"
                }]
            },{
            test: /\.sass$/,
            use: extractSass.extract({
                use: [{
                    loader: "css-loader"
                }, {
                    loader: "sass-loader"
                }]
            })
        }]
    },
    plugins: [
        extractSass
    ]
};
0

For .scss to sass use sass-loader and css-loader. and for minifying images use image-webpack-loader.

Whole configuration for webpack.conig.js is show below

module.exports = {
    entry: './app.js',
    output: {
        filename: 'bundle.js'
    },
    module: {
        rules: [
        {
            test: /\.s[ac]ss$/,
            use: ExtractTextPlugin.extract({
                use: ['css-loader', 'sass-loader'],

                fallback: 'style-loader'
            })
        }, {
            loader: 'image-webpack-loader',
            query: {
                mozjpeg: {
                    progressive: true,
                },
                gifsicle: {
                    interlaced: false,
                },
                optipng: {
                    optimizationLevel: 4,
                },
                pngquant: {
                    quality: '75-90',
                    speed: 3,
                },
            }
        }
        ]
    }
}
owais
  • 4,752
  • 5
  • 31
  • 41