zoukankan      html  css  js  c++  java
  • Webpack 学习记录-02

    想在HTMl页面中生成内联的JS代码:

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title><%= htmlWebpackPlugin.options.title %></title>
    	<script type="text/javascript">
    		<%= compilation.assets[htmlWebpackPlugin.files.chunks.main.entry.substr(htmlWebpackPlugin.files.publicPath.length)].source() %>
    	</script>
    </head>
    <body>
    <% for (var chk in htmlWebpackPlugin.files.chunks) {%>
    <% if(chk!=="main")  {%>
    <script type="text/javascript" src="<%= htmlWebpackPlugin.files.chunks[chk].entry %>"></script>
    <% } %>
    <% } %>
    
    </body>
    </html>
    

     webpack.config.js

    var htmlWebpackPlugin=require("html-webpack-plugin");
    
    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:__dirname+"/dist",
    		filename:"js/[name].js"
    	},
    	plugins:[
    		new htmlWebpackPlugin({
    			template:'index.html',
    			inject:'body',
    			filename:"a.html",
    			title:"this is a.html",
    			chunks:['main','a']
    		}),
    		new htmlWebpackPlugin({
    			template:"index.html",
    			inject:'body',
    			filename:"b.html",
    			title:"this is b.html",
    			chunks:['main','a']
    
    		}),
    		new htmlWebpackPlugin({
    			template:"index.html",
    			inject:'body',
    			filename:"c.html",
    			title:"this  is c.html",
    			chunks:['main','c']		})
    
    	]
    		
    	
    }
    

     开始拼写错了<%= compilation.assets[htmlWebpackPlugin.files.chunks.main.entry.substr(htmlWebpackPlugin.files.publicPath.length)].source() %>
        </script>  写错为:<%= compilation.asserts[htmlWebpackPlugin.files.chunks.main.entry.substr(htmlWebpackPlugin.files.publicPath.length)].source() %>
        </script>

    一直报错:

  • 相关阅读:
    P4781 【模板】拉格朗日插值
    P1306 斐波那契公约数
    P1154 奶牛分厩
    P1028 数的计算
    P1445 [Violet]樱花
    2020 Multi-University Training Contest 4
    Codeforces Round #658 (Div. 2) D
    2020牛客暑期多校训练营(第八场) K
    Codeforces Round #659 (Div. 2)
    #10106. 「一本通 3.7 例 2」单词游戏
  • 原文地址:https://www.cnblogs.com/happyfish321/p/6918545.html
Copyright © 2011-2022 走看看