Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat and minify CSS files with Webpack without require them

I've got and old part of an application that contains some CSS files that are concatenated and minified with gulp script.

And I've got a new application that bundled with Webpack.

Is it possible to assemble the old CSS with Webpack without any additional require calls? Just get all CSS from old_css/**/*.css, concat, minify and write to assets/old.css?

like image 262
konclave Avatar asked Mar 25 '16 18:03

konclave


People also ask

How do I Minify CSS files in webpack?

To minify the resulting CSS, you'll use the optimize-css-assets-webpack-plugin: In Glitch console, run npm install --save-dev optimize-css-assets-webpack-plugin . Run refresh , so the changes are synchronized with the Glitch editor.

Does webpack minify by default?

Webpack v4+ will minify your code by default in production mode .

Does webpack compile CSS?

Loaders are the node-based utilities built for webpack to help webpack to compile and/or transform a given type of resource that can be bundled as a javascript module. css-loader is the npm module that would help webpack to collect CSS from all the css files referenced in your application and put it into a string.

How do I use webpack to minify JavaScript files?

Modifying JavaScript minification process In webpack, minification process is controlled through two configuration fields: optimization. minimize flag to toggle it and optimization. minimizer array to configure the process.


1 Answers

You can achieve this by "requiring" the CSS files through a separate entry. You'll end up with something like this:

{
    entry: {
        styles: glob('old_css/**/*.css'), // array of css files
        ...
    },
    output: {
        filename: '[name].[chunkhash].js',
        ...
    },
    module: {
        loaders: [
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract("style-loader", "css-loader")
            },
            ...
        ]
    },
    plugins: [
        new ExtractTextPlugin('[name].[chunkhash].css'),
        ...
    ],
    ...
}

You'll end up with a JavaScript file named after your style entry in addition to the CSS file. You can ignore that, though.

like image 105
Juho Vepsäläinen Avatar answered Oct 17 '22 09:10

Juho Vepsäläinen