Best Practice Sharing for Webpack Configuration

This article mainly introduces best practices for WebPack configuration. The practice sharing this article has the following advantages:

  • Using HappyPack to increase the package speed.
  • Use MD5 HASH to generate file versions, and version control
  • supports multiple ports in the system
  • template, which can be used in HTMLPlugin outputs some configurations. Sex information
  • Support for DEVSERVER, MOCK

1. WebPack Best Practice

1. Hot loading

2. Syntax check

3.JS package

4. Template packaging

Second, solution

1.WebPack.config.json

VAR path = Require (‘PATH’); VAR FS = Require (‘fs’); var exec = Require (‘child_process’). EXEC; var happypack = request (‘happypack’); var merge = required (‘WebPack-Merge’); varweBPack = 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’); // remove the page file mapping function getHtmlPluginArr () {var data = JSON.PARSE (FS.READFILESYNC (‘UTRYCONF.JSON’, ‘UTF-8’)); var Pagelist = Data.PageList; var resultobj = {“pluginarr”: [], “entryobj”: {}}; for (var index = 0; index

2. Configuration of template file:
  < pageList.length; index++) {
 var element = pageList[index];
 var entry = element.entry;
 //通过对app.json中src的路径截取获得分发路径
 var filename = (function () {
 var filenameStr = entry.split("./app/page/")[1];
 return filenameStr.substr(0, filenameStr.lastIndexOf("."));
 })();
 var title = element.title;
 var extra = element.extra;
 resultObj.entryObj[filename] = entry;
 //利用路径一部分来进行HtmlwebpackPlugin的chunks
 resultObj.pluginArr.push(
 new HtmlwebpackPlugin({
  chunks: [filename], //当前页面js
  title: title,
  extra: extra,//包含页面额外的配置信息
  template: "app/" + "template.ejs",
  filename: 'views/'+filename + '.ejs',
  chunksSortMode: "dependency"//按chunks的顺序对js进行引入
 })
 );
 //HappyPack, loader多进程去处理文件
 resultObj.pluginArr.push(
 new HappyPack({ id: 'html' }),
 new HappyPack({ id: 'css' }),
 new HappyPack({ id: 'js' }),
 new HappyPack({ id: 'tpl' })
 );
 }
 return resultObj;
}
var appJsonObj = getHtmlPluginArr();
/**通用配置 */
var commonConfig = {
 entry: appJsonObj.entryObj,
 module: {
 loaders: [
 { test: /\.html$/, loader: "html?minimize=false", happy: {id: "html"} },
 { test: /\.json$/, loader: "json" },
 { test: /\.scss|\.css$/, loaders: ["style", "css", "sass"], happy: {id: "css"} },
 { test: /\.(?:jpg|gif|png)$/, loader: 'url?limit=10240&name=images/[name]-[hash:10].[ext]' },
 { test: /\.handlebars/, loader: "handlebars", query: { helperDirs: [APP_PATH + "/helper"] }, happy: {id: "tpl"} },
 { test: /\.js$|\.jsx$/, exclude: /(node_modules|bower_components)/, loader: 'babel', query: { presets: ['es2015'] }, happy: {id: "js"} },
 ]
 },
 output: {
 path: BUILD_PATH,
 filename: "js/[name].js"
 },
 externals: {
 "jquery": "jQuery"
 },
 //配置短路径引用
 resolve: {
 extensions: ['', '.js', '.json', '.scss','.vue'],
 alias: {
 vue : 'vue/dist/vue.js'
 }
 },
 plugins: appJsonObj.pluginArr,
 cache: true
}
module.exports = merge(commonConfig, {
 output: {
 publicPath: '/',
 path: BUILD_PATH,
 filename: "js/[name]-[chunkhash:10].js"
 },
 plugins: [
 new webpack.optimize.UglifyJsPlugin({ minimize: true }),
 new WebpackMd5Hash()
 ]
}); 

   ">         
3.WebPack.dev.config.json configuration
<% for(var i = 0;i <script src=" VAR PATH = Require ('PATH'); VAR FS = Require ('fs'); var merge = required (' 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'); // Remove the page file mapping 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 Summary The above is the full content of the best configuration of WebPack, and it is desirable that the content of this article is for everyone's Learning or work can bring a certain help, if you have any questions, you can leave a message, thank you for your support of Tumi Cloud.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment