为什么去处理html文件
我们所有的方法都打包到了dist的文件夹下面,而我们的html是在自己定义的文件夹下面,如果自己手动再去一个一个src引入这些dist文件夹下的js,那么也有些太不靠谱了
所以解决办法是:
使用webpack插件:htmlwebpackplugin第一步:下载
npm install --save-dev extract-text-webpack-plugin
第二步:webpack.config.js配置
其中htmlwebpackplugin的配置项有:
name类型description
title {string} 用于生成的html文档的标题
filename {string} 要生成html的文件。可以指定目录
template {string} 依据的模板文件
inject {boolean|string} 将js资源注入到页面哪个部位,值有:true \ ‘head’ \ ‘body’ \ false,当传递true或’body’所有javascript资源将被放置在正文元素的底部。’head’将脚本放置在head元素中
favicon {string} 将给定的图标路径添加到输出html
hash {boolean} 如果true将webpack所有包含的脚本和css文件附加一个独特的编译哈希。这对缓存清除非常有用
chunks {?} 放入你需要引入的资源模块
excludechunks {?} 不放入你某些资源模块
预期目标: 我的项目是一个多入口文件的项目,希望每一个入口页面引入对应的js模块和css
比如login页面引入login的js和css、index引入对应js和css
webpack.config.js配置如下:
const path = require('path');const webpack = require('webpack')const extracttextplugin = require("extract-text-webpack-plugin");const htmlwebpackplugin = require('html-webpack-plugin');const configs = { entry:{ 'commom':['./src/page/common/index.js'], 'index':['./src/page/index/index.js'], 'login':['./src/page/login/index.js'] }, output:{ path:path.resolve(__dirname, 'dist'), filename:'js/[name].js' }, module:{ rules:[ { test:/\.css$/, use:extracttextplugin.extract({ fallback: "style-loader", use: "css-loader" }) } ] }, plugins:[ //独立通用模块 new webpack.optimize.commonschunkplugin({ name : 'common', filename : 'js/base.js' }), //独立打包css new extracttextplugin('css/[name].css'), //对html模板进行处理,生成对应的html,引入需要的资源模块 new htmlwebpackplugin({ template:'./src/view/index.html',//模板文件 filename:'view/login/index.html',//目标文件 chunks:['commom','login'],//对应加载的资源 inject:true,//资源加入到底部 hash:true//加入版本号 }) ] } module.exports= configs
然后打包结果如下
其中生成的目标文件:
为什么去处理html文件
我们所有的方法都打包到了dist的文件夹下面,而我们的html是在自己定义的文件夹下面,如果自己手动再去一个一个src引入这些dist文件夹下的js,那么也有些太不靠谱了
所以解决办法是:
使用webpack插件:htmlwebpackplugin第一步:下载
npm install --save-dev extract-text-webpack-plugin
第二步:webpack.config.js配置
其中htmlwebpackplugin的配置项有:
name类型description
title {string} 用于生成的html文档的标题
filename {string} 要生成html的文件。可以指定目录
template {string} 依据的模板文件
inject {boolean|string} 将js资源注入到页面哪个部位,值有:true \ ‘head’ \ ‘body’ \ false,当传递true或’body’所有javascript资源将被放置在正文元素的底部。’head’将脚本放置在head元素中
favicon {string} 将给定的图标路径添加到输出html
hash {boolean} 如果true将webpack所有包含的脚本和css文件附加一个独特的编译哈希。这对缓存清除非常有用
chunks {?} 放入你需要引入的资源模块
excludechunks {?} 不放入你某些资源模块
预期目标: 我的项目是一个多入口文件的项目,希望每一个入口页面引入对应的js模块和css
比如login页面引入login的js和css、index引入对应js和css
webpack.config.js配置如下:
const path = require('path');const webpack = require('webpack')const extracttextplugin = require("extract-text-webpack-plugin");const htmlwebpackplugin = require('html-webpack-plugin');const configs = { entry:{ 'commom':['./src/page/common/index.js'], 'index':['./src/page/index/index.js'], 'login':['./src/page/login/index.js'] }, output:{ path:path.resolve(__dirname, 'dist'), filename:'js/[name].js' }, module:{ rules:[ { test:/\.css$/, use:extracttextplugin.extract({ fallback: "style-loader", use: "css-loader" }) } ] }, plugins:[ //独立通用模块 new webpack.optimize.commonschunkplugin({ name : 'common', filename : 'js/base.js' }), //独立打包css new extracttextplugin('css/[name].css'), //对html模板进行处理,生成对应的html,引入需要的资源模块 new htmlwebpackplugin({ template:'./src/view/index.html',//模板文件 filename:'view/login/index.html',//目标文件 chunks:['commom','login'],//对应加载的资源 inject:true,//资源加入到底部 hash:true//加入版本号 }) ] } module.exports= configs
然后打包结果如下
其中生成的目标文件:
相关推荐:
在webpack中使用echarts详解
node.js、jade生成静态html文件实例
webpack的插件详解
以上就是webpack对html文件的处理的详细内容。
