2

I would like to make my bundled .css file being generated by Webpack more configurable, so I can output different 'versions' - based on the same .css file - to make the life of developers working on my project in the future easier.

I would like to have the following steps:

  1. Concat of all SCSS into CSS (bundle.css)
  2. Minimize output of step 1 (bundle.min.css)
  3. Embed all images from step 2 (bundle.b64.min.css)
  4. Embed all fonts from step 3 (bundle.bs64.fonts.min.css)

In the end - after my build process -, I would have 4 distinct files in my dist folder. Would that me possible?

The way I'm currently doing it, I run a different script for each step - deletes dist folder, goes through project, produces the output. I would like to have a single script that does all of it at once without having to go through my project 4 times.

I kind of found a solution for it here:

Webpack Extract-Text-Plugin Output Multiple CSS Files (Both Minified and Not Minified)

But, for my specific case, I would have to return 4 different configurations in a array instead of a single object.

yagosansz
  • 47
  • 2
  • 8
  • Do you want the assets (images etc.) to be bundled into a single bundle css file? (I don't know if that's possible). I use file-loader to load the files. – aviya.developer Jul 23 '19 at 15:50
  • also: `:warning: Since webpack v4 the extract-text-webpack-plugin should not be used for css. Use mini-css-extract-plugin instead.` from the docs so don't use that. – aviya.developer Jul 23 '19 at 15:50
  • The things you are mentioning are covered quite thoroughly in the `webpackjs` documentation, both in the guides as well as plugins references. for steps 1 & 2 you need plugin `mini-css-extract-plugin` for steps 3 & 4 you need to handle assests with `file-loader`. these two plugins in the `webpackjs` documentation. – aviya.developer Jul 23 '19 at 15:53
  • To your first question: Yes, I'd like that to happen (is that even possible?). I'm using the file-loader as well for my current code. Second: Thanks for letting me know! (I'm using *mini-css-extract-plugin*). I get what you're saying. I can generate the steps by running different scripts. But I want to be able to go through all 4 steps in one shot. **Can you comment on that?** – yagosansz Jul 23 '19 at 15:54
  • First: I don't know if it's possible to compile the assets into a bundle css file (maybe it is but i haven't heard of it). Seconds: Are you using a `config.webpack.js` file? – aviya.developer Jul 23 '19 at 16:00
  • the whole point of a script like 'npm run build:prod` is that this single lines runs all the procedure for you so if you're using more than that there's something wrong in how you are building it. Can you share your `packack.json` and `webpack.config.js` files? – aviya.developer Jul 23 '19 at 16:02
  • That's what I'm asking in my post @aviya.developer. I have different scripts because I haven't found a way to do it in a single one, but the solution where I posted a link to in my original post, which for me doesn't seem ideal. – yagosansz Jul 23 '19 at 16:07

1 Answers1

1

Ok so based on our comment conversation i'm gonna give you a workflow of steps 1-4, but with regular assets handling, not a bundling of assets (which i haven't heard of but maybe someone else can elaborate there).

So the steps:

  1. bundle all scss files into 1 bundle.css
  2. make sure this bundle is minified
  3. add assets management to build for images
  4. add assets management to build for fonts

The important things:

This workflow is basically a built by configuration. configuring the npm scripts with the package.json file, and configuring webpack with config.webpack.js. This will allow you to simply run 1 command to build your project: npm run build. note: For simplicity's sake i am going to ignore production/development/etc environments and focus on a single environment.

package.json:

This is used to set up the command that will actually run when you input npm run build in the terminal (from the project dir of course).

since we are avoiding different environments for now and as you are not using Typescript this is a very simple configuraton:

"scripts": {
    "build": "webpack",
  },

that's all you have to add. It sound's stupid now but when the project will get more complex you are going to like those scripts so better start off making them already.

webpack.config.js: The major lifting will be made in this configuration file. This basically tells webpack what to do when you run it (which is what npm run build is doing).

first off let's install some plugins:

  • npm install --save-dev file-loader
  • npm install --save-dev html-webpack-plugin
  • npm install --save-dev mini-css-extract-plugin
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  mode: 'production',
  devtool: 'source-map'
  entry: './client/src/app.jsx',
  output: {
    path: path.join(__dirname, 'client/dist/public'),
    filename: 'bundle.[hash].js'
  },
  module: {
    rules: [
      {
        test: /\.s?css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              hmr: false
            }
          },
          'css-loader',
          'sass-loader'
        ]
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: ['file-loader']
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [
          'file-loader'
        ]
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.json', '.jsx']
  },
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './client/src/index_template.html'
    }),
    new MiniCssExtractPlugin({
      filename: 'style.[hash].css',
      chunkFilename: '[id].[hash].css'
    }),
  ]
};

Notice i've added the htmlWebpackPlugin because it makes it easier to reference the correct hashed bundles automatically. Also I've assumed the app is a react app but you can just change the entry point to where your app loads from.

This is quite hard to do of the fly without testing things out, but i hope this gives you enough reference as to what you should change and do to get going with it.

Again i strognly recommend the webpack.js guides and documentation, they are very thorough and once you start getting the hang of it things start working smoothly.

aviya.developer
  • 3,343
  • 2
  • 15
  • 41
  • I appreciate your help, although it still doesn't help me with the questions I've asked because that's not what I asked about in my post...I've read the documentation and scavenged the forum for a solution, and only have found one solution (which I've posted above), that is not practical, because I would need an array with 4 distinct configurations. Initially I thought it wasn't possible because I don't know if Webpack is supposed to work that way...but it doesn't hurt to ask, right? – yagosansz Jul 23 '19 at 17:21
  • @yagosansz could you perhaps share some code, especially the two configuration files? Maybe i'll understand your issue better. You mention 4 scripts but haven't shared them. It would help. – aviya.developer Jul 23 '19 at 20:17