javascript - HtmlWebpackPlugin生成html问题,怎么对应js文件,而不是全引入

 KLD受害者 发布于 2022-11-11 08:18

动态生成html,我有两个html模板,index.html跟list.html,我想分别对应index.js跟list.js
但是生成index.html和list.html的时候把两个js都引入了
请问怎么才能分开对应引用,还是我的思路不对?

配置文件entry 2个js,HtmlWebpackPlugin new了两次生成2个html

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
    devServer:{
        contentBase:'./dist/view'
    },
    entry:{
        index:'./src/js/index.js',
        list:'./src/js/list.js'
    },
    output:{
        path:path.join(__dirname,'dist'),
        publicPath: "/Capella/dist/",
        filename:'js/[name].js'
    },
    module:{
        loaders:[
            {test:/\.css$/,loader:'style!css'},
            {test:/\.js$/,exclude:'/node_modules/',loader:'babel-loader'}
        ]
    },
    plugins:[
        new webpack.ProvidePlugin({
            _:"lodash",
            $:"jQuery"
        }),
        new HtmlWebpackPlugin({
            filename:'./view/index.html',
            template:'./src/view/index.html',
            inject:'true',
            hasg:'true',
            minify:{
                removeComments:false,    
                collapseWhitespace:false  
            }
        }),
        new HtmlWebpackPlugin({
            filename:'./view/list.html',
            template:'./src/view/list.html',
            inject:'true',
            hasg:'true',
            minify:{
                removeComments:false,    
                collapseWhitespace:false    
            }
        }),
        // new webpack.optimize.UglifyJsPlugin({
        //     compass:{
        //         warnings:false
        //     },
        //     except: ['$super', '$', 'exports', 'require'] 
        // })
    ]
}

生成后的list.html index.html的script引入跟list一样






bunld


    
  • aaa
  • bbb
  • cccc
2 个回答
  • 关键在于

            new HtmlWebpackPlugin({
                filename:'./view/index.html',
                template:'./src/view/index.html',
                chunks: ['index'],
                inject:'true',
                hasg:'true',
                minify:{
                    removeComments:false,    
                    collapseWhitespace:false  
                }
            }),
            new HtmlWebpackPlugin({
                filename:'./view/list.html',
                template:'./src/view/list.html',
                chunks: ['list'],
                inject:'true',
                hasg:'true',
                minify:{
                    removeComments:false,    
                    collapseWhitespace:false    
                }
            })
    2022-11-12 01:55 回答
  • HtmlWebpackPlugin分别加
    chunks:[index.js]
    chunks:[list.js]

    2022-11-12 01:55 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有