基于 webpack2 實(shí)現(xiàn)的多入口項(xiàng)目腳手架詳解
簡(jiǎn)介
基于 webpack2 實(shí)現(xiàn)的多入口項(xiàng)目腳手架,主要使用 extract-text-webpack-plugin 實(shí)現(xiàn) js 、css 公共代碼提取,html-webpack-plugin 實(shí)現(xiàn) html 多入口,less-loader 實(shí)現(xiàn) less 編譯,postcss-loader 配置 autoprefixer 實(shí)現(xiàn)自動(dòng)添加瀏覽器兼容前綴,html-withimg-loader 實(shí)現(xiàn) html 內(nèi)引入圖片版本號(hào)添加和模板功能,babel-loader 實(shí)現(xiàn) ES6 轉(zhuǎn)碼功能, happypack 多線程加速構(gòu)建速度。
目錄
├── dist # 構(gòu)建后的目錄 ├── config # 項(xiàng)目配置文件 │ ├── webpack.config.js # webpack 配置文件 │ └── postcss.config.js # postcss 配置文件 ├── src # 程序源文件 │ └── js # 入口 │ ├ └── index.js # 匹配 view/index.html │ ├ └── user │ ├ ├ ├── index.js # 匹配 view/user/index.html │ ├ ├ ├── list.js # 匹配 view/user/list.html │ ├ └── lib # JS 庫(kù)等,不參與路由匹配 │ ├ ├── jquery.js │ └── view │ ├ └── index.html # 對(duì)應(yīng) js/index.js │ ├ └── user │ ├ ├── index.html # 對(duì)應(yīng) js/user/index.js │ ├ ├── list.html # 對(duì)應(yīng) js/user/list.js │ └── css # css 文件目錄 │ ├ └── base.css │ ├ └── iconfont.css │ └── font # iconfont 文件目錄 │ ├ └── iconfont.ttf │ ├ └── iconfont.css │ └── img # 圖片文件目錄 │ ├ └── pic1.jpg │ ├ └── pic2.png │ └── template # html 模板目錄 │ └── head.html │ └── foot.html
配置
多入口
根據(jù) JS 目錄獲取多入口數(shù)組
const ROOT = process.cwd(); // 根目錄
let entryJs = getEntry('./src/js/**/*.js');
/**
* 根據(jù)目錄獲取入口
* @param {[type]} globPath [description]
* @return {[type]} [description]
*/
function getEntry (globPath) {
let entries = {};
Glob.sync(globPath).forEach(function (entry) {
let basename = path.basename(entry, path.extname(entry)),
pathname = path.dirname(entry);
// js/lib/*.js 不作為入口
if (!entry.match(/\/js\/lib\//)) {
entries[pathname.split('/').splice(3).join('/') + '/' + basename] = pathname + '/' + basename;
}
});
return entries;
}
// webpack 配置
const config = {
entry: entryJs,
output: {
filename: 'js/[name].js?[chunkhash:8]',
chunkFilename: 'js/[name].js?[chunkhash:8]',
path: path.resolve(ROOT, 'dist'),
publicPath: '/'
},
}
module
使用 babel 實(shí)現(xiàn) ES2015 轉(zhuǎn) ES5,less 轉(zhuǎn) css 并使用 postcss 實(shí)現(xiàn) autoprefixer 自動(dòng)添加瀏覽器兼容,url-loader 實(shí)現(xiàn) css 引用圖片、字體添加版本號(hào),html-withimg-loader 實(shí)現(xiàn) html 引用圖片添加版本號(hào)并實(shí)現(xiàn)模板功能。
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader?id=js',
options: {
presets: ['env']
}
}
},
{
test: /\.(less|css)$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader?id=styles',
use: [{
loader: 'css-loader?id=styles',
options: {
minimize: !IsDev
}
},
{
loader: 'less-loader?id=styles'
},
{
loader: 'postcss-loader?id=styles',
options: {
config: {
path: PostcssConfigPath
}
}
}
]
})
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 100,
publicPath: '',
name: '/img/[name].[ext]?[hash:8]'
}
}
]
},
{
test: /\.(eot|svg|ttf|woff)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 100,
publicPath: '',
name: '/font/[name].[ext]?[hash:8]'
}
}
]
},
// @see https://github.com/wzsxyz/html-withimg-loader
{
test: /\.(htm|html)$/i,
loader: 'html-withimg-loader?min=false'
}
]
},
// postcss.config.js
module.exports = {
plugins: [
require('autoprefixer')({
browsers: ['> 1%', 'last 5 versions', 'not ie <= 9'],
})
]
}
View 視圖
根據(jù)目錄對(duì)應(yīng)關(guān)系,獲取 js 對(duì)應(yīng)的 html 入口
let entryHtml = getEntryHtml('./src/view/**/*.html'),
configPlugins;
entryHtml.forEach(function (v) {
configPlugins.push(new HtmlWebpackPlugin(v));
});
// webpack 配置
resolve: {
alias: {
views: path.resolve(ROOT, './src/view')
}
},
/**
* 根據(jù)目錄獲取 Html 入口
* @param {[type]} globPath [description]
* @return {[type]} [description]
*/
function getEntryHtml (globPath) {
let entries = [];
Glob.sync(globPath).forEach(function (entry) {
let basename = path.basename(entry, path.extname(entry)),
pathname = path.dirname(entry),
// @see https://github.com/kangax/html-minifier#options-quick-reference
minifyConfig = IsDev ? '' : {
removeComments: true,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true
};
entries.push({
filename: entry.split('/').splice(2).join('/'),
template: entry,
chunks: ['common', pathname.split('/').splice(3).join('/') + '/' + basename],
minify: minifyConfig
});
});
return entries;
}
plugins
使用 happypack 多線程加快構(gòu)建速度,CommonsChunkPlugin 實(shí)現(xiàn)提取公用 js 為單獨(dú)文件,extract-text-webpack-plugin 實(shí)現(xiàn)提取公用 css 為單獨(dú)文件,
let configPlugins = [
new HappyPack({
id: 'js',
// @see https://github.com/amireh/happypack
threadPool: HappyThreadPool,
loaders: ['babel-loader']
}),
new HappyPack({
id: 'styles',
threadPool: HappyThreadPool,
loaders: ['style-loader', 'css-loader', 'less-loader', 'postcss-loader']
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'common'
}),
// @see https://github.com/webpack/webpack/tree/master/examples/multiple-entry-points-commons-chunk-css-bundle
new ExtractTextPlugin({
filename: 'css/[name].css?[contenthash:8]',
allChunks: true
})
];
entryHtml.forEach(function (v) {
configPlugins.push(new HtmlWebpackPlugin(v));
});
// webpack 配置
plugins: configPlugins,
開(kāi)發(fā)
webpack-dev-server 實(shí)現(xiàn)開(kāi)發(fā)環(huán)境自動(dòng)刷新等功能
// webpack 配置
devServer: {
contentBase: [
path.join(ROOT, 'src/')
],
hot: false,
host: '0.0.0.0',
port: 8080
}
開(kāi)發(fā)
npm start
http://localhost:8080/view
構(gòu)建
cross-env 實(shí)現(xiàn)區(qū)分開(kāi)發(fā)和生產(chǎn)環(huán)境構(gòu)建
命令 說(shuō)明
npm run dev 開(kāi)發(fā)環(huán)境構(gòu)建,不壓縮代碼
npm run build 生產(chǎn)環(huán)境構(gòu)建,壓縮代碼
倉(cāng)庫(kù):https://github.com/givebest/webpack2-multiple-entry
本地下載:http://xiazai.jb51.net/201706/yuanma/webpack2-multiple-entry(jb51.net).rar
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
JavaScript函數(shù)節(jié)流概念與用法實(shí)例詳解
這篇文章主要介紹了JavaScript函數(shù)節(jié)流概念與用法,結(jié)合實(shí)例形式詳細(xì)分析了JavaScript函數(shù)節(jié)流的概念、功能,并分析了函數(shù)節(jié)流的用法與使用技巧,需要的朋友可以參考下2016-06-06
JavaScript實(shí)現(xiàn)構(gòu)造json數(shù)組的方法分析
這篇文章主要介紹了JavaScript實(shí)現(xiàn)構(gòu)造json數(shù)組的方法,結(jié)合實(shí)例形式對(duì)比分析了javascript構(gòu)造json數(shù)組的實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-08-08
淺談async、defer以普通script加載的區(qū)別
本文主要介紹了淺談async、defer以普通script加載的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
javascript 隨機(jī)數(shù) 與高級(jí)應(yīng)用 附vbscript(asp) 隨機(jī)數(shù)總結(jié)
有時(shí)忘了程序的隨機(jī)數(shù)函數(shù)或javascript和vbscript的隨機(jī)數(shù)混亂了,特總結(jié)下兩者的隨機(jī)數(shù)函數(shù),以備以后使用方便。2007-10-10
javascript獲取元素文本內(nèi)容的通用函數(shù)
獲取元素文本內(nèi)容的通用函數(shù),思路很好值得參考。2009-12-12
JavaScript中英文字符長(zhǎng)度統(tǒng)計(jì)方法示例【按照中文占2個(gè)字符】
這篇文章主要介紹了JavaScript中英文字符長(zhǎng)度統(tǒng)計(jì)方法,涉及javascript針對(duì)中英文字符的匹配與運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
利用JavaScript制作一個(gè)搞怪的兔子動(dòng)畫(huà)效果
又是一年新春之際,祝福大家兔年快樂(lè)!給大家介紹一個(gè)有趣的動(dòng)效(兼容?IE),頁(yè)面右下角有一只搞怪的兔子,鼠標(biāo)在頁(yè)面中懸停時(shí),兔子會(huì)跟著做出不同的動(dòng)作和表情,感興趣的小伙伴可以了解一下2023-01-01

