Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Webpack, is it possible to generate CSS only, excluding the output.js?

I'm using Webpack with the extract-text-webpack-plugin.

In my project, I have some build scripts. One of the build scripts is supposed to bundle and minify CSS only. As I'm using Webpack for the other scripts, I found it a good idea to use Webpack even when I only want to bundle and minify CSS.

It's working fine, except that I can't get rid of the output.js file. I don't want the resulting webpack output file. I just want the CSS for this particular script.

Is there a way to get rid of the resulting JS? If not, do you suggest any other tool specific for handling CSS? Thanks.

like image 498
André Pena Avatar asked Apr 21 '15 00:04

André Pena


People also ask

How does webpack load CSS?

To be able to use CSS in your webpack app, you need to set up a new loader. Out-of-the-box, webpack only understands Javascript and JSON. With a loader, you can translate another type of file to a format that webpack understands and can work with. There are many webpack loaders and each loader has a specific purpose.

How do I bundle CSS with webpack?

By default, Webpack will inline your CSS as Javascript tags that injects the CSS into the page. This sounds strange but it lets you do hot reloading in development. In production you extract them to a file using the ExtractTextPlugin, and require it with a normal link tag.

What does webpack generate?

Webpack will generate the files and put them in the /dist folder for you, but it doesn't keep track of which files are actually in use by your project. In general it's good practice to clean the /dist folder before each build, so that only used files will be generated. Let's take care of that with output.

How do you minify a CSS file 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.


1 Answers

There is an easy way, no extra tool is required.

There is an easy way and you don't need extra libraries except which you are already using: webpack with the extract-text-webpack-plugin.

In short:

Make the output js and css file have identical name, then the css file will override js file.

A real example (webpack 2.x):

import path from 'path' import ExtractTextPlugin from 'extract-text-webpack-plugin'  const config = {   target: 'web',   entry: {     'one': './src/one.css',     'two': './src/two.css'   },   output: {     path: path.join(__dirname, './dist/'),     filename: '[name].css' // output js file name is identical to css file name   },   module: {     rules: [       {         test: /\.css$/,         use: ExtractTextPlugin.extract({           fallback: 'style-loader',           use: 'css-loader'         })       }     ]   },   plugins: [     new ExtractTextPlugin('[name].css') // css file will override generated js file   ] } 
like image 122
Tyler Liu Avatar answered Sep 23 '22 05:09

Tyler Liu