Multi-page webpack projects are extracted and packaged with MiniCssExtractPlugin, and css files are repeatedly packaged.

the situation is as follows
currently I want to do a pc website, so I first roll out a simple scaffolding.
is also my first contact with a developer under webpack. I have stepped on a lot of holes to do this, and now I have died here
. (donors may ask, why not just start html+script? Because the old man wants to pretend to use the front-end engineering)

use webpack4.6.0 + html-webpack-plugin3.2.0 (Note: not vue), something else.

there are 2 pages, and index.html,main.html, loads their respective js respectively. The configuration is as follows

.
const path = require("path")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const MiniCssExtractPlugin = require("mini-css-extract-plugin") 

//
module.exports = {
    entry : {
        "index" : __dirname + "/src/index.js",
        "main" : __dirname + "/src/main.js",
        "jqueryv183min" :__dirname + "/src/jqueryv183min.js"
    },
    output:{
        path: __dirname + "/dist",
        filename:"js/[name].js",
        publicPath:"/",
    },
    devServer:{...},
    module:{
        rules:[
            {
                test: /.js$/,
                loader: "babel-loader?presets=es2015",
            },
            {
                test: /\.(htm|html)$/i,
                loader: "html-withimg-loader"
            },
            {
                test:/\.(jpg|png|gif|svg)$/,
                //1024base64
                loader: "url-loader",
                options: {
                    limit: 1024,
                      outputPath:"images/"
                }
            },
            {
                test: /\.css$/,
                loader:[MiniCssExtractPlugin.loader,"css-loader"]
            }
        ]
    },
    plugins:[
    new HtmlWebpackPlugin({
        template: __dirname + "/src/index.html",
        filename:"index.html",
        chunks:["jqueryv183min","index"],
        chunksSortMode: "manual"
    }),
    new HtmlWebpackPlugin({
        template: __dirname + "/src/main.html",
        filename:"main.html",
        chunks:["main"],
        chunksSortMode: "manual"
    }),
    new MiniCssExtractPlugin({
        filename: "css/[name].css",
        chunkFilename: "[id].css"
    })
    ]
}

in both index.js and main.js, there are two references to css
base.css is of course the basic rest css,
in order to save trouble, the effect css, in all pages is placed in main.css, so each page should be referenced (or will be separated during formal development)

import idxcss from "./css/base.css"
import maincss from "./css/main.css"

after build, html and js are packaged normally. In the css directory, two css files appear: index.css,main.css,

is, of course, the

extracted by MiniCssExtractPlugin from js.

the packaged index.html introduces the same main.html as index.css, and main.css

Open it and see that the contents of the two files are exactly the same, both containing the contents of base.css and main.css

the problem is obvious. Base.css has been repeatedly packaged, so the expected effect of the old man is:
base.css and main.css are packaged to generate separate files. Of course, two link tags in the two html introduce these two css files

.

or if it is the same as the packaged js, the required blocks can be packaged and automatically introduced
so that each html page automatically introduces a separate css file, while introducing a common bsse.css file instead of repeatedly packing base.css in it as above

Baidu for some time, fruitless
the old man first starts with the configuration of the plug-in MiniCssExtractPlugin, but really can"t find useful information
No one has ever encountered this problem? The problem may be special?
is it possible that the old man is used to the previous direct development method, so there is a problem with the structural configuration?


thanks for inviting ~ can actually be directly extracted and compressed into a style file.

new MiniCssExtractPlugin({
    filename: "css/style.css",
    chunkFilename: "[id].css"
})

base.css has been repeatedly packaged. Because you wrote entry entry index , main , and the referenced style is the same. It must be two copies of the same style.
what you need is to extract public css, compression css``. Plug-ins such as
purify-css , optimize-css-assets-webpack-plugin , etc.
refer to the following article.
get started with webpack4 and upgrade? Let's take a look at this ~
in webpack+vue, how can the same part of css be extracted into a common common.css?


now that the problem has been solved, in order to help future generations, we hereby write down the plan (for the scaffolding structure of the old man only)
such as the structure of the old man is
index.js:
import idxcss from'. / css/base.css'
import maincss from'. / css/index.css'
main.js:
import idxcss from'. / css/base.css'
import maincss from'. / css/index.css'

2 entry files are duplicated with 2 css

when I read the article that webpack4 cannot use extract-text-webpack-plugin, use MiniCssExtractPlugin
at the very beginning, but MiniCssExtractPlugin cannot extract all the css from js and pack them into one css, so the console will prompt you to repeat the output

.

so I saw in other places that webpack4 can actually use extract-text-webpack-plugin
to install with @ next
such as cnpm I extract-text-webpack-plugin@next-- save-dev

.

the specific usage is Baidu. There is also
in the address posted by the upstairs donor. This plug-in can package all css into one file, and will not repeat the package

.

there is a problem with the configuration.

Menu