简体   繁体   English

Webpack-捆绑多个/不同版本的.css文件

[英]Webpack - Bundle multiple/different versions of .css files

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. 我想使由Webpack生成的捆绑的.css文件更具可配置性,因此我可以基于相同的.css文件输出不同的“版本”,以使开发人员将来从事我的项目的工作变得更加轻松。

I would like to have the following steps: 我要执行以下步骤:

  1. Concat of all SCSS into CSS ( bundle.css ) 将所有SCSS合并为CSS( bundle.css
  2. Minimize output of step 1 ( bundle.min.css ) 最小化步骤1的输出( bundle.min.css
  3. Embed all images from step 2 ( bundle.b64.min.css ) 嵌入第2步中的所有图像( bundle.b64.min.css
  4. Embed all fonts from step 3 ( bundle.bs64.fonts.min.css ) 嵌入第3步中的所有字体( bundle.bs64.fonts.min.css

In the end - after my build process -, I would have 4 distinct files in my dist folder. 最后,在构建过程之后,我的dist文件夹中将有4个不同的文件。 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. 我目前的操作方式是,为每个步骤运行一个不同的脚本-删除dist文件夹,遍历项目,生成输出。 I would like to have a single script that does all of it at once without having to go through my project 4 times. 我希望有一个脚本可以一次完成所有操作,而不必经历我的项目4次。

I kind of found a solution for it here: 我在这里找到了解决方案:

Webpack Extract-Text-Plugin Output Multiple CSS Files (Both Minified and Not Minified) Webpack提取文本插件输出多个CSS文件(已缩小和未缩小)

But, for my specific case, I would have to return 4 different configurations in a array instead of a single object. 但是,对于我的特定情况,我将必须在数组中而不是单个对象中返回4种不同的配置。

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). 好的,根据我们的评论对话,我将为您提供第1-4步的工作流程,但要进行常规资产处理,而不是资产捆绑(我没有听说过,但也许有人可以在此处进行阐述)。

So the steps: 因此,步骤如下:

  1. bundle all scss files into 1 bundle.css 将所有的scss文件捆绑到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 . 使用package.json文件配置npm脚本,并使用config.webpack.js配置webpack。 This will allow you to simply run 1 command to build your project: npm run build . 这将使您只需运行1条命令即可构建项目: 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 : 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). 这用于设置当您在终端中输入npm run build时实际上将运行的命令(当然是从项目dir开始)。

since we are avoiding different environments for now and as you are not using Typescript this is a very simple configuraton: 因为我们现在正在避免使用不同的环境,并且由于您没有使用Typescript,所以这是一个非常简单的配置:

"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. webpack.config.js :主要的提升将在此配置文件中进行。 This basically tells webpack what to do when you run it (which is what npm run build is doing). 这基本上告诉webpack运行它时该做什么(这是npm run build所做的事情)。

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. 注意,我已经添加了htmlWebpackPlugin,因为它使自动引用正确的散列包变得更加容易。 Also I've assumed the app is a react app but you can just change the entry point to where your app loads from. 另外,我假设该应用程序是React应用程序,但是您可以将入口点更改为应用程序的加载位置。

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. 我再次强烈推荐webpack.js 指南和文档 ,它们非常详尽,一旦您掌握了它,一切就会开始顺利进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM