本文主要介绍了关于webpack配置的最佳实践,本文分享的实践具有以下的优势:
一、webpack最佳实践中的需求
1.热加载
2.语法校验
3.js打包
4.模板打包
二、解决方案
1.webpack.config.json
var path = require('path'); var fs = require('fs'); var exec = require('child_process').exec; var HappyPack = require('happypack'); var merge = require('webpack-merge'); var webpack = require('webpack'); var WebpackMd5Hash = require('webpack-md5-hash'); var HtmlwebpackPlugin = require('html-webpack-plugin'); var ROOT_PATH = path.resolve(__dirname); var APP_PATH = path.resolve(ROOT_PATH, 'app'); var BUILD_PATH = path.resolve(ROOT_PATH, __dirname+'/devserver/public'); //取出页面文件映射 function getHtmlPluginArr() { var data = JSON.parse(fs.readFileSync('entryconf.json', 'utf-8')); var pageList = data.pageList; var resultObj = { "pluginArr": [], "entryObj": {} }; for (var index = 0; index
2.模板文件的配置:
<% if (htmlWebpackPlugin.options.extra&&htmlWebpackPlugin.options.extra.js) {%> <% for(var i = 0;i <% } %> <% } %>
3.webpack.dev.config.json配置
var path = require('path'); var fs = require('fs'); var merge = require('webpack-merge'); var webpack = require('webpack'); var HtmlwebpackPlugin = require('html-webpack-plugin'); var OpenBrowserPlugin = require('open-browser-webpack-plugin'); var ROOT_PATH = path.resolve(__dirname); var APP_PATH = path.resolve(ROOT_PATH, 'app'); var BUILD_PATH = path.resolve(ROOT_PATH, 'build'); var MODULE_PATH = path.resolve(ROOT_PATH, 'node_modules'); //取出页面文件映射 function getHtmlPluginArr() { var data = JSON.parse(fs.readFileSync('app/entries.json', 'utf-8')); var pageList = data.pageList; var resultObj = { "pluginArr": [], "entryObj": {} }; for (var index = 0; index
总结
以上就是关于webpack最佳配置的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。