詳解如何使用 vue-cli 開發(fā)多頁應用
本文介紹了如何使用 vue-cli 開發(fā)多頁應用,分享給大家,具體如下:
修改的webpack配置文件
全局配置
修改 webpack.base.conf.js
打開 ~\build\webpack.base.conf.js ,找到entry,添加多入口
entry: { app: './src/main.js', app2: './src/main2.js', app3: './src/main3.js', },
運行、編譯的時候每一個入口都會對應一個Chunk
run dev 開發(fā)環(huán)境
修改 webpack.dev.conf.js
打開 ~\build\webpack.dev.conf.js ,在plugins下找到new HtmlWebpackPlugin,在其后面添加對應的多頁,并為每個頁面添加Chunk配置
chunks: ['app']中的app對應的是webpack.base.conf.js中entry設置的入口文件
plugins:[ // https://github.com/ampedandwired/html-webpack-plugin // 多頁:index.html → app.js new HtmlWebpackPlugin({ filename: 'index.html',//生成的html template: 'index.html',//來源html inject: true,//是否開啟注入 chunks: ['app']//需要引入的Chunk,不配置就會引入所有頁面的資源 }), // 多頁:index2.html → app2.js new HtmlWebpackPlugin({ filename: 'index2.html',//生成的html template: 'index2.html',//來源html inject: true,//是否開啟注入 chunks: ['app2']//需要引入的Chunk,不配置就會引入所有頁面的資源 }), // 多頁:index3.html → app3.js new HtmlWebpackPlugin({ filename: 'index3.html',//生成的html template: 'index3.html',//來源html inject: true,//是否開啟注入 chunks: ['app3']//需要引入的Chunk,不配置就會引入所有頁面的資源 }) ]
run build 編譯
修改 config/index.js
打開~\config\index.js,找到build下的index: path.resolve(__dirname, '../dist/index.html'),在其后添加多頁
build: { index: path.resolve(__dirname, '../dist/index.html'), index2: path.resolve(__dirname, '../dist/index2.html'), index3: path.resolve(__dirname, '../dist/index3.html'), },
修改 webpack.prod.conf.js
打開~\build\webpack.prod.conf.js,在plugins下找到new HtmlWebpackPlugin,在其后面添加對應的多頁,并為每個頁面添加Chunk配置
HtmlWebpackPlugin 中的 filename 引用的是 config/index.js 中對應的 build
plugins: [ // 多頁:index.html → app.js new HtmlWebpackPlugin({ filename: config.build.index, template: 'index.html', inject: true, minify: { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true // more options: // https://github.com/kangax/html-minifier#options-quick-reference }, // necessary to consistently work with multiple chunks via CommonsChunkPlugin chunksSortMode: 'dependency', chunks: ['manifest','vendor','app']//需要引入的Chunk,不配置就會引入所有頁面的資源 }), // 多頁:index2.html → app2.js new HtmlWebpackPlugin({ filename: config.build.index2, template: 'index2.html', inject: true, minify: { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true }, chunksSortMode: 'dependency', chunks: ['manifest','vendor','app2']//需要引入的Chunk,不配置就會引入所有頁面的資源 }), // 多頁:index3.html → app3.js new HtmlWebpackPlugin({ filename: config.build.index3, template: 'index3.html', inject: true, minify: { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true }, chunksSortMode: 'dependency', chunks: ['manifest','vendor','app3']//需要引入的Chunk,不配置就會引入所有頁面的資源 }), ]
如果頁面比較多,可以考慮使用循環(huán)將 HtmlWebpackPlugin 添加到 plugins
// utils.js exports.getEntry = function(globPath, pathDir) { var files = glob.sync(globPath); var entries = {}, entry, dirname, basename, pathname, extname; for (var i = 0; i < files.length; i++) { entry = files[i]; dirname = path.dirname(entry); extname = path.extname(entry); basename = path.basename(entry, extname); pathname = path.join(dirname, basename); pathname = pathDir ? pathname.replace(new RegExp('^' + pathDir), '') : pathname; entries[pathname] = ['./' + entry]; } return entries; }
// webpack.base.conf.js var pages = Object.keys(utils.getEntry('../src/views/**/*.html', '../src/views/')); pages.forEach(function (pathname) { // https://github.com/ampedandwired/html-webpack-plugin var conf = { filename: '../views/' + pathname + '.html', //生成的html存放路徑,相對于path template: '../src/views/' + pathname + '.html', //html模板路徑 inject: false, //js插入的位置,true/'head'/'body'/false /* * 壓縮這塊,調(diào)用了html-minify,會導致壓縮時候的很多html語法檢查問題, * 如在html標簽屬性上使用{{...}}表達式,所以很多情況下并不需要在此配置壓縮項, * 另外,UglifyJsPlugin會在壓縮代碼的時候連同html一起壓縮。 * 為避免壓縮html,需要在html-loader上配置'html?-minimize',見loaders中html-loader的配置。 */ // minify: { //壓縮HTML文件 // removeComments: true, //移除HTML中的注釋 // collapseWhitespace: false //刪除空白符與換行符 // } }; if (pathname in config.entry) { conf.favicon = 'src/images/favicon.ico'; conf.inject = 'body'; conf.chunks = ['vendors', pathname]; conf.hash = true; } config.plugins.push(new HtmlWebpackPlugin(conf)); });
同樣入口 entry 也可以使用
// webpack.base.conf.js entry: { app: utils.getEntry('../src/scripts/**/*.js', '../src/scripts/') },
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue3.x的版本中build后dist文件中出現(xiàn)legacy的js文件問題
這篇文章主要介紹了Vue3.x的版本中build后dist文件中出現(xiàn)legacy的js文件問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07解決vue-quill-editor上傳內(nèi)容由于圖片是base64的導致字符太長的問題
vue-quill-editor默認插入圖片是直接將圖片轉(zhuǎn)為base64再放入內(nèi)容中,如果圖片較多,篇幅太長,就會比較煩惱,接下來通過本文給大家介紹vue-quill-editor上傳內(nèi)容由于圖片是base64的導致字符太長的問題及解決方法,需要的朋友可以參考下2018-08-08vue中前端如何實現(xiàn)pdf預覽功能(含vue-pdf插件用法)
這篇文章主要給大家介紹了vue中前端如何實現(xiàn)pdf預覽功能的相關資料,文中包含vue-pdf插件用法,在前端開發(fā)中,很多時候我們需要進行pdf文件的預覽操作,需要的朋友可以參考下2023-07-07Vue高性能列表GridList組件及實現(xiàn)思路詳解
這篇文章主要為大家介紹了Vue高性能列表GridList組件及實現(xiàn)思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11element-ui tree結(jié)構(gòu)實現(xiàn)增刪改自定義功能代碼
這篇文章主要介紹了element-ui tree結(jié)構(gòu)實現(xiàn)增刪改自定義功能代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08vue實現(xiàn)的上拉加載更多數(shù)據(jù)/分頁功能示例
這篇文章主要介紹了vue實現(xiàn)的上拉加載更多數(shù)據(jù)/分頁功能,涉及基于vue的事件響應、數(shù)據(jù)交互等相關操作技巧,需要的朋友可以參考下2019-05-05element多選表格中使用Switch開關的實現(xiàn)
當在做后臺管理系統(tǒng)的時候,會用到用戶的狀態(tài)管理這個功能,本文主要介紹了element多選表格中使用Switch開關的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07