cli单页应用改成多页应用配置详解

从接触vue开始用的是vue-cli直接搭建单页应用,参考配合着vue-router开发起来简直爽到吊炸天,但是由于项目越来越复杂了,单页用起来可能有点力不从心,能不能弄成多页面呢,查了相关资料得到的结论是完全可以的,能多页面多入口,并且可以使用组件,还引入jQuery,这简直完美了,这个demo是从我已经改造完成的项目中摘出来的,现在演示下怎么把基于vue2的vue-cli单页模板改造成多页面,并且多入口的项目。

技术栈

vue: 2.0.1

vue-resource:1.0.3

vue-router:2.0.0

webpack:1.13.2

gulp:3.9.1

ES6

运行

git clone https://github.com/dawnyu/vue-cli-multipage.git npm install npm run build npm run dev

改造后的目录

cli单页应用改成多页应用配置详解

可以多目录生成目标文件

公共的js和样式图标放到assets文件夹即可

修改点

build/utils.js

var path = require('path') var config = require('../config') var glob = require('glob') // 将样式提取到单独的css文件中,而不是打包到js文件或使用style标签插入在head标签中 var ExtractTextPlugin = require('extract-text-webpack-plugin') exports.assetsPath = function(_path) { var assetsSubDirectory = process.env.NODE_ENV === 'production' ? config.build.assetsSubDirectory : config.dev.assetsSubDirectory return path.posix.join(assetsSubDirectory, _path) } exports.cssLoaders = function(options) { options = options || {} // generate loader string to be used with extract text plugin function generateLoaders(loaders) { var sourceLoader = loaders.map(function(loader) { var extraParamChar if (/\?/.test(loader)) { loader = loader.replace(/\?/, '-loader?') extraParamChar = '&' } else { loader = loader + '-loader' extraParamChar = '?' } return loader + (options.sourceMap ? extraParamChar + 'sourceMap' : '') }).join('!') if (options.extract) { return ExtractTextPlugin.extract('vue-style-loader', sourceLoader) } else { return ['vue-style-loader', sourceLoader].join('!') } } // return { css: generateLoaders(['css']), postcss: generateLoaders(['css']), less: generateLoaders(['css', 'less']), sass: generateLoaders(['css', 'sass?indentedSyntax']), scss: generateLoaders(['css', 'sass']), stylus: generateLoaders(['css', 'stylus']), styl: generateLoaders(['css', 'stylus']) } } // Generate loaders for standalone style files (outside of .vue) exports.styleLoaders = function(options) { var output = [] var loaders = exports.cssLoaders(options) for (var extension in loaders) { var loader = loaders[extension] output.push({ test: new RegExp('\\.' + extension + '$'), loader: loader }) } return output } //增加获取多入口的方法 注意 这个参数是个数组 exports.getEntry = function(globPaths) { var entries = {}, basename, tmp, pathname; for (globPath of globPaths) { glob.sync(globPath).forEach(function(entry) { basename = path.basename(entry, path.extname(entry)); tmp = entry.split('https://www.jb51.net/').splice(-3); pathname = tmp.splice(0, 1) + 'https://www.jb51.net/' + basename; // 正确输出js和html的路径 entries[pathname] = entry; }); } console.log(entries); return entries; }

webpack.base.conf.js

var path = require('path') var config = require('../config') var webpack = require('webpack') var merge = require('webpack-merge') var utils = require('./utils') var projectRoot = path.resolve(__dirname, '../') ///——driname当前目录 var chunks = Object.keys(utils.getEntry(['./src/module/**/*.js', './src/m/**/*.js'])); // 将样式提取到单独的css文件中,而不是打包到js文件或使用style标签插入在head标签中 var ExtractTextPlugin = require('extract-text-webpack-plugin'); module.exports = { entry: utils.getEntry(['./src/module/**/*.js', './src/m/**/*.js']),//传入需要打包的入口,我这里是pc端和手机端入口打到一个包里 output: { path: config.build.assetsRoot, publicPath: process.env.NODE_ENV === 'production' ? config.build.assetsPublicPath : config.dev.assetsPublicPath, //根名称可配置 filename: '[name].js' }, resolve: { extensions: ['', '.js', '.vue'], fallback: [path.join(__dirname, '../node_modules')], alias: { 'src': path.resolve(__dirname, '../src'), 'assets': path.resolve(__dirname, '../src/assets'), 'components': path.resolve(__dirname, '../src/components'), 'jquery': 'jquery' } }, resolveLoader: { fallback: [path.join(__dirname, '../node_modules')] }, module: { loaders: [{ test: /\.vue$/, loader: 'vue-loader' }, { test: /\.js$/, loader: 'babel', include: projectRoot, exclude: /node_modules/ }, { test: /\.json$/, loader: 'json' }, { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, loader: 'url', query: { limit: 30000, name: utils.assetsPath('img/[name].[hash:7].[ext]') } }, { test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, loader: 'url', query: { limit: 10000, name: utils.assetsPath('fonts/[name].[hash:7].[ext]') } } ] }, eslint: { formatter: require('eslint-friendly-formatter') }, vue: { loaders: utils.cssLoaders(), postcss: [ require('autoprefixer')({ browsers: ['last 2 versions'] }) ] }, plugins: [ // new webpack.optimize.CommonsChunkPlugin('static/build.js'), // 提取公共模块 new webpack.optimize.CommonsChunkPlugin({ name: 'vendors', // 公共模块的名称 chunks: chunks, // chunks是需要提取的模块 minChunks: chunks.length }), // 配置提取出的样式文件 new ExtractTextPlugin('css/[name].css'), //引入jqury new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery" }) ], }

webpack.dev.conf.js

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyjsdd.html