webpack对html文件的处理
互联网
18-5-28
所以解决办法是:
使用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 | {?} | 不放入你某些资源模块 |
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插件: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 | {?} | 不放入你某些资源模块 |
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文件的处理的详细内容,更多内容请关注技术你好其它相关文章!
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场