webpack configuration (step 4: html articles (template articles))

Project files about webpack: https://gitee.com/codeFarmerPen/webpack

First look at the project structure:

Create a new ejs directory under the app, and the suffix of the html file will naturally become ejs;

The ejs file in the ejs directory is the main file,

The generate directory contains the spliced ​​files,

The public directory contains public files.

Configuration prerequisites:

Introduce ejs-loader

$ cnpm install ejs ejs-loader

Add in loaders:

            ,
            { 
            	test: /\.ejs$/, 
            	loader: 'ejs-loader?variable=data' 
            }

webpack.config.js:

Because the imported file becomes an ejs file, webpack.config.js should also be changed accordingly;

The path and suffix name of the template have been changed, and the content has been added to the loaders

const path = require('path');
let htmlwebpackplugin = require('html-webpack-plugin');//引入html-webpack-plugin插件 
let get_html = function(name,chunk){//封装
    return {
        template: './app/ejs/generate/'+ name + '.ejs',//这里改变了路径和后缀名!!!
        filename:  name+ '.html',
        chunks  : ['main',chunk||null],//这里引入公共文件main.js,另外一个文件按需引入,当然也可以把这个的值设为数组,传入function的第二个值用数组就行
        chunksSortMode: 'manual',//将chunks按引入的顺序排序
        inject  : true,//所有JavaScript资源插入到body元素的底部
        hash    : true,
		xhtml	: true
    }
};

let export_html= {
    entry:  {
    	main:__dirname+"/app/js/main.js",//入口文件
    	main1:__dirname+"/app/js/main1.js",//入口文件
    },
    output: {
        path: __dirname+"/_build/",
        filename: "js/[name].js",//产出文件,name根据entry的入口文件键名定
    },
    module: {
        loaders: [
			{
			    test: /(\.jsx|\.js)$/,
			    loader: 'babel-loader',
			    query: {
			      presets: ['es2015']
			    }
			}
            ,
            { 
            	test: /\.ejs$/, 
            	loader: 'ejs-loader?variable=data' 
            }
        ]
    }
    ,
 	plugins: [
 		//new一个模板出来测试一下
  		new htmlwebpackplugin(get_html("home","main1"))
 	]
    
};
module.exports=export_html;	

 

Contents of files in the directory:

app/ejs/public/head.ejs

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <link rel="shortcut icon" type="image/x-icon" href="" />
    <title>hello!</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no">
	<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>

app/ejs/public/foot.ejs

	<footer id="footer" :choose="choose">
    	<section><div></div></section>
    	<section><div></div></section>
    	<section><div><span v-model="addnumber" :value="addnumber"></span></div></section>
    	<section><div></div></section>
    </footer>
    </body>
</html>

app/ejs/home.ejs

<body>
    <p>hello,nice to meet u!</p>

app/ejs/generate/home.ejs   

<%= require("../public/head.ejs")() %>
<%= require("../home.ejs")() %>
<%= require("../public/foot.ejs")() %>

This file introduces the above three files, and then this will be put into the entry file of htmlwebpackplugin

webpack to see what is generated,

_build/home.html

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <link rel="shortcut icon" type="image/x-icon" href="" />
    <title>hello!</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no">
	<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
//备注:上是head.ejs文件的内容
<body>
    <p>hello,nice to meet u!</p>
//备注:上面是home.ejs的内容
//备注:下面是foot.ejs的内容
	<footer id="footer" :choose="choose">
    	<section><div></div></section>
    	<section><div></div></section>
    	<section><div><span v-model="addnumber" :value="addnumber"></span></div></section>
    	<section><div></div></section>
    </footer>
    <script type="text/javascript" src="js/main.js?a2847a8b5f38621da2fb"></script><script type="text/javascript" src="js/main1.js?a2847a8b5f38621da2fb"></script></body>
</html>

success!

Below are all my links about webpack config

webpack configuration (first step: configuration prerequisites): https://my.oschina.net/u/3797834/blog/1648528

Webpack configuration (Step 2: Getting Started): https://my.oschina.net/u/3797834/blog/1648578

webpack configuration (the third step: ES6): https://my.oschina.net/u/3797834/blog/1649033

webpack configuration (step 4: html article (basic article)): https://my.oschina.net/u/3797834/blog/1649157

webpack configuration (step 4: html article (advanced article)): https://my.oschina.net/u/3797834/blog/1649196

webpack configuration (step 4: html article (template article)): https://my.oschina.net/u/3797834/blog/1649246

webpack configuration (step 5: less/css articles (basic articles)): https://my.oschina.net/u/3797834/blog/1649270

webpack configuration (step 5: less/css articles (advanced articles)): https://my.oschina.net/u/3797834/blog/1649288

webpack configuration (step 5: less/css article (url picture article)): https://my.oschina.net/u/3797834/blog/1649302

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325809927&siteId=291194637