新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解webpack+ES6+Sass搭建多頁面應用-創(chuàng)新互聯(lián)
webpack同之前的gulp相比,gulp屬于非模塊化打包工具,webpack屬于模塊化打包工具,兩者的優(yōu)劣這里不做過多分析(可自行百度)。
目的:是為了分享一下使用過程中,用到的插件、loader、遇到的各種問題以及解決辦法,最后會附上最終代碼。
在這個項目中,用 webpack打包css、scss、js、圖片文件、jquery、第三方插件、字體圖標,編譯es6、壓縮html、壓縮js、壓縮css ,基本涵蓋了常用的東西。
代碼結構如下
下面單獨說幾個難點
1、打包多頁面,使用到的插件是html-wepack-plugin , html-loader ,也就是你有多少個頁面,就有多少個入口,也就寫多少個模版文件。當然,頁面過多時,可以考慮遍歷處理。
//引入路徑插件 const path = require('path'); //引入導出html插件 const HtmlWebpackPlugin = require('html-webpack-plugin'); const config = { //入口 entry:{ home:'./pages/lawSearchHomepage.js', list:'./pages/lawSearchList.js' }, //出口 output:{ filename: '[name].bundle.js', path: path.resolve(__dirname,'build') }, //插件 plugins: [ //html單獨導出插件——首頁 new HtmlWebpackPlugin({ filename:'lawSearchHomepage.html',//輸出后的文件名稱 template:'./pages/lawSearchHomepage.html',//模版頁面路徑 favicon:'./pages/images/favicon.ico',//頁簽圖標 chunks:['home'],//需要引入的js文件名稱 inject: true, hash:true,//哈希值 minify: {//壓縮 removeComments: true, collapseWhitespace: true } }), //html單獨導出插件——列表頁 new HtmlWebpackPlugin({ filename:'lawSearchList.html', template:'./pages/lawSearchList.html', favicon:'./pages/images/favicon.ico', chunks:['list'], inject: true, hash:true, minify: { removeComments: true, collapseWhitespace: true } }) ] } module.exports = config;
分享題目:詳解webpack+ES6+Sass搭建多頁面應用-創(chuàng)新互聯(lián)
鏈接地址:http://www.ef60e0e.cn/article/depodd.html