webpack打包多页应用,为什么不同的入口(html)文件会引用不相关的js文件?

发布于 2022-09-11 16:03:57 字数 3802 浏览 30 评论 0

想联系一下webpack的使用方式,所以抛弃了任何手脚架工具,自己创建了一个多页面应用,然后手写了webpack相关配置,但是通过npm build打包的时候发现虽然是多个html文件,但是所有的html里面引入的js都是相同的(都引入了所有的js(打包后的)文件),啥问题呢?

源码地址:https://gitee.com/qingyun1029/webpack-for-multiple-page-demo

使用方式:

  1. 克隆项目到本地
    git clone git@gitee.com:qingyun1029/webpack-for-multiple-page-demo.git
  2. 安装依赖模块
    npm install
  3. 打包代码
    webpack.base.conf.js

打包后的代码结构如下:

图片描述

about.html(格式化后代码片段)
图片描述

home.html(格式化后代码片段)
图片描述

可以看到这两个html页面都引入了另一个和自己没关系的js文件,怎么回事呢?

项目相关配置,如使用源码,请 忽略 下面内容!!!!

————————————————————————————————————————————————

'use strict'
const webpack = require('webpack')
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    mode: 'production',
    entry: {
        // home: path.resolve(__dirname, 'src/pages/home/index.js'),
        // about: path.resolve(__dirname, 'src/pages/about/index.js'),

        home: './src/pages/home/index.js',
        about: './src/pages/about/index.js',
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].[chunkhash].js',
    },
    resolve: {
        extensions: ['.js', '.json'],
        // alias: {
        //   'vue$': 'vue/dist/vue.esm.js',
        //   '@': resolve('src'),
        // }
    },
    module: {
        // rules: [
        //     {
        //         test: /\.js$/,
        //         loader: 'babel-loader',
        //         include: [resolve('src')]
        //     }
        // ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'home.html',
            template: 'src/pages/home/html/index.html',
            inject: true,
            // favicon: resolveApp('favicon.ico'),
            minify: {
              removeComments: true,
              collapseWhitespace: true,
            //   removeAttributeQuotes: true,
              // more options:
              // https://github.com/kangax/html-minifier#options-quick-reference
            },
            // necessary to consistently work with multiple chunks via CommonsChunkPlugin
            // chunksSortMode: 'dependency'
        }),
        new HtmlWebpackPlugin({
            filename: 'about.html',
            template: 'src/pages/about/html/index.html',
            inject: true,
            minify: {
              removeComments: true,
              collapseWhitespace: true,
            },
        }),
    ],
}

build.js



const ora = require('ora')
const webpack = require('webpack')
const webpackConfig = require('./webpack.base.conf.js')
const chalk = require('chalk')
const spinner = ora('building for production...')
spinner.start()

webpack(webpackConfig, function (err, stats) {
    spinner.stop()
    if (err) throw err
    process.stdout.write(stats.toString({
        colors: true,
        modules: false,
        children: false,
        chunks: false,
        chunkModules: false
    }) + '\n\n')

    if (stats.hasErrors()) {
        console.log(chalk.red('  Build failed with errors1111111.\n'))
        process.exit(1)
    }

    console.log(chalk.cyan('  Build complete222222222.\n'))
    console.log(chalk.yellow(
        '  Tip: built files are meant to be served over an HTTP server.\n' +
        '  Opening index.html over file:// won\'t work.\n'
    ))
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南笙 2022-09-18 16:03:58
        new HtmlWebpackPlugin({
            chunks:['home'],
            filename: 'home.html',
            template: 'src/pages/home/html/index.html',
            inject: true,
            // favicon: resolveApp('favicon.ico'),
            minify: {
              removeComments: true,
              collapseWhitespace: true,
            //   removeAttributeQuotes: true,
              // more options:
              // https://github.com/kangax/html-minifier#options-quick-reference
            },
            // necessary to consistently work with multiple chunks via CommonsChunkPlugin
            // chunksSortMode: 'dependency'
        }),

HtmlWebpackPlugin 加上chunks

找个人就嫁了吧 2022-09-18 16:03:58
home: './src/pages/home/index.js',
about: './src/pages/about/index.js'

我感觉是这里的问题,改成数组试试

£冰雨忧蓝° 2022-09-18 16:03:58
  • webpack不知道入口文件和html文件存在依赖关系
  • 每个入口文件都被称为一个chunk
  • html-webpack-plugin文档的描述来看,默认会将所有chunk都加入到html中,但是允许只加入某些chunk,通过配置chunks选项即可

clipboard.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文