vue-cli单页应用改成多页应用配置详解
前言从接触vue开始用的是vue-cli直接搭建单页应用,参考配合着vue-router开发起来简直爽到吊炸天,但是由于项目越来越复杂了,单页用起来可能有点力不从心,能不能弄成多页面呢,查了相关资料得到的结论是完全可以的,能多页面多入口,并且可以使用组件,还引入jQuery,这简直完美了,这个demo是从我已经改造完成的项目中摘出来的,现在演示下怎么把基于vue2的vue-cli单页模板改造成多页面,并且多入口的项目。 技术栈
运行改造后的目录 可以多目录生成目标文件 公共的js和样式图标放到assets文件夹即可 修改点 build/utils.js 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) { if (options.extract) { // http://vuejs.github.io/vue-loader/configurations/extract-css.html // Generate loaders for standalone style files (outside of .vue) webpack.base.conf.js webpack.dev.conf.js module.exports = merge(baseWebpackConfig,{
module: { loaders: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap }) },// eval-source-map is faster for development devtool: '#eval-source-map',plugins: [ new webpack.DefinePlugin({ 'process.env': config.dev.env }),// https://github.com/glenjamin/webpack-hot-middleware#installation--usage new webpack.optimize.OccurenceOrderPlugin(),new webpack.HotModuleReplacementPlugin(),new webpack.NoErrorsPlugin(),// https://github.com/ampedandwired/html-webpack-plugin // new HtmlWebpackPlugin({ // filename: 'index.html',// template: 'index.html',// inject: true // }) ] }) var pages = utils.getEntry(['./src/module/*/.html','./src/m/*/.html']); for (var pathname in pages) { // 配置生成的html文件,定义路径等 }; if (pathname in module.exports.entry) { module.exports.plugins.push(new HtmlWebpackPlugin(conf)); webpack.prod.conf.js module.exports = merge(baseWebpackConfig,{
module: { loaders: utils.styleLoaders({ sourceMap: config.build.productionSourceMap,extract: true }) },devtool: config.build.productionSourceMap ? '#source-map' : false,output: { path: config.build.assetsRoot,filename: utils.assetsPath('js/[name].[chunkhash].js'),chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') },vue: { loaders: utils.cssLoaders({ sourceMap: config.build.productionSourceMap,extract: true }) },plugins: [ // http://vuejs.github.io/vue-loader/workflow/production.html new webpack.DefinePlugin({ 'process.env': env }),new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false,drop_debugger: true,drop_console: true } }),new webpack.optimize.OccurenceOrderPlugin(),// extract css into its own file new ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css')),// generate dist index.html with correct asset hash for caching. // you can customize output by editing /index.html // see https://github.com/ampedandwired/html-webpack-plugin // new HtmlWebpackPlugin({ // filename: process.env.NODE_ENV === 'testing' ? // 'index.html' : config.build.index,// favicon: './src/assets/images/tjd.ico',// 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' // }),// split vendor js into its own file new webpack.optimize.CommonsChunkPlugin({ name: 'vendor',minChunks: function(module,count) { // any required modules inside node_modules are extracted to vendor return ( module.resource && /.js$/.test(module.resource) && module.resource.indexOf( path.join(__dirname,'../node_modules') ) === 0 ) } }),// extract webpack runtime and module manifest to its own file in order to // prevent vendor hash from being updated whenever app bundle is updated new webpack.optimize.CommonsChunkPlugin({ name: 'manifest',chunks: ['vendor'] }) ] }) if (config.build.productionGzip) { webpackConfig.plugins.push( var pages = utils.getEntry(['./src/module/*/.html','./src/m/*/.html']); for (var pathname in pages) { // 配置生成的html文件,定义路径等 }; module.exports.plugins.push(new HtmlWebpackPlugin(conf)); git地址: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |