• webpack4从0开始构建前端单页项目(11)提取样式到单独的css文件中


    ExtractTextWebpackPlugin、MiniCssExtractPlugin 这个两个插件能够把样式提取为单独的.css 文件。不过更建议使用MiniCssExtractPlugin这个插件提取css文件。

    安装 mini-css-extract-plugin

        cnpm i mini-css-extract-plugin -D
    

    项目结构

        project
        |   .babelrc            # babel-loader配置文件
        |   .editorconfig       # 配置格式化插件
        |   package.json        # 项目需要的依赖
        |   webpack.config.js   # webpack配置文件
        |
        +---public
        |       index.html      # 用于打包生成 .html 文件的模板
        |
        ---src
                main.js         # webpack的入口文件
                style.sass       # sass文件
    

    依赖的模块(package.json)

        "devDependencies": {
            "@babel/core": "^7.11.6",
            "@babel/preset-env": "^7.11.5",
            "babel-loader": "^8.1.0",
            "css-loader": "^4.3.0",
            "dart-sass": "^1.25.0",
            "html-webpack-plugin": "^4.5.0",
            "mini-css-extract-plugin": "^0.11.3",
            "sass": "^1.26.11",
            "sass-loader": "^10.0.2",
            "style-loader": "^1.2.1",
            "webpack": "^4.44.2",
            "webpack-cli": "^3.3.12",
            "webpack-dev-server": "^3.11.0"
        }
    

    webpack 配置文件

    // webpack.config.js
        const webpack = require("webpack");
        const path = require("path");
        const htmlWebpackPlugin = require("html-webpack-plugin");
        const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    
        module.exports = {
            entry: "./src/main.js",
            mode: "development",
            output: {
                path: path.join(__dirname, "dist"),
                filename: "app.[hash:16].js",
            },
            plugins: [
                new MiniCssExtractPlugin({  // 使用MiniCssExtractPlugin
                    // 类似于 webpackOptions.output 中的选项
                    // 所有选项都是可选的
                    filename: "[name].css",
                    chunkFilename: "[id].css",
                    ignoreOrder: false, // 忽略有关顺序冲突的警告
                }),
                new htmlWebpackPlugin({
                    filename: "index.html",
                    template: "./public/index.html",
                    minify: {
                        collapseWhitespace: false,
                    },
                    inject: true,
                }),
            ],
            devServer: {
                contentBase: path.join(__dirname, "dist"),
                compress: true,
                port: 9000, // 配置端口
                hot: true,
            },
    
            module: {
                rules: [
                    {
                        test: /.js$/,
                        exclude: /(node_modules|bower_components)/,
                        use: "babel-loader",
                    },
                    {
                        test: /.s[ac]ss$/,
                        use: [
                            MiniCssExtractPlugin.loader,    // 使用MiniCssExtractPlugin
                            "css-loader",
                            { loader: "sass-loader" },
                        ],
                    },
                ],
            },
        };    
    

    在webpack4中使用ExtractTextWebpackPlugin则必须如下安装

        cnpm i extract-text-webpack-plugin@next -D
    

    ExtractTextWebpackPlugin 参考: https://webpack.docschina.org/plugins/extract-text-webpack-plugin/

    mini-css-extract-plugin 参考: https://webpack.docschina.org/plugins/mini-css-extract-plugin/

    开发工具
  • 相关阅读:
    HDU 4325 Flowers(树状数组)
    HDU 1166 敌兵布阵(树状数组)
    linux网络编程之一-----多播(组播)编程
    对 /dev/shm 认识
    使用GDB调试STL容器
    Android中图片优化之webp使用
    Android后台进程与前台线程间的区别使用
    Android如何从外部跳进App
    熟悉Android开发不得不知道的技巧
    Java代码规范文档
  • 原文地址:https://www.cnblogs.com/cisbest/p/13765448.html
Copyright © 2020-2023  润新知