webpack2使用ch5-多页面设置 内部js和引入js

1 当前目录

2 webpack.config.js 配置

const webpack = require('webpack'),
      htmlWebpackPlugin = require('html-webpack-plugin'),
      path = require('path');

module.exports = {
    entry: {
      main: './src/script/main.js',
      a: './src/script/a.js',
      b: './src/script/b.js',
      c: './src/script/c.js'
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: 'js/[name]-[chunkhash].js',
        publicPath: 'http://cdn.com/'
    },
    plugins: [
        new htmlWebpackPlugin({
            filename: 'a.html',
            template: 'index.html',
            inject: 'body',
            title: 'this is a',
            excludeChunks: ['b', 'c']  //不包括的chunks
        }),
        new htmlWebpackPlugin({
            filename: 'b.html',
            template: 'index.html',
            inject: 'body',
            title: 'this is b',
            excludeChunks: ['a', 'c']
        }),
        new htmlWebpackPlugin({
            filename: 'c.html',
            template: 'index.html',
            inject: 'body',
            title: 'this is c',
            excludeChunks: ['a', 'b']
        })
    ]
};

4 根目录模板 index.html

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><%= htmlWebpackPlugin.options.title %></title>
    <!--将mian.js写在页面里面
        去掉不带publicPath的内容如 http://cdn.cn/
        compilation.assets[].source()
     -->
    <script type="text/javascript">
        <%= compilation.assets[htmlWebpackPlugin.files.chunks.main.entry.substr(htmlWebpackPlugin.files.publicPath.length)].source() %>
    </script>
</head>
<body>

    <!--如果chunk有main.js就不引入 因为上面引入了??????-->
    <% for(var key in htmlWebpackPlugin.files.chunks){ %>
       <% if(key !== 'main'){ %>
            <script type="text/javascript" src="<%= htmlWebpackPlugin.files.chunks.entry %>"></script>
       <% } %>
    <% } %>
</body>
</html>

 

5 打包

 

6 查看变化

6.1 a.html

 

6.2 b.html

6.3 c.html

 

转载于:https://www.cnblogs.com/easyweb/p/6673968.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值