欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue 多入口文件搭建 vue多页面搭建的实例讲解

程序员文章站 2022-06-21 08:02:21
红色为更改后的不同之处 vue 多入口文件搭建 在webpack.base.conf 中修改 var path = require('path')...

红色为更改后的不同之处

vue 多入口文件搭建 vue多页面搭建的实例讲解

vue 多入口文件搭建

在webpack.base.conf

中修改

var
path = require('path')
var
config = require('../config')
var
utils = require('./utils')
var
projectroot = 
path.resolve(__dirname,'../')
var glob = require('glob');
var entries = getentry('./src/module/*.js'); // 获得入口js文件
module.exports = {
entry: entries,
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')
}
},
resolveloader: {
fallback: [path.join(__dirname,'../node_modules')]
},
module: {
loaders: [
{
test: /\.vue$/,
loader:'vue'
},
{
test: /\.js$/,
loader:'babel',
include:projectroot,
exclude: /node_modules/
},
{
test: /\.json$/,
loader:'json'
},
{
test: /\.html$/,
loader:'vue-html'
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader:'url',
query: {
limit:10000,
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]')
}
}
]
},
vue: {
loaders:utils.cssloaders()
}
}
function getentry(globpath) {
var entries = {},
basename, tmp, pathname;
glob.sync(globpath).foreach(function (entry) {
basename = path.basename(entry, path.extname(entry));
console.log(1,basename);
tmp = entry.split('/').splice(-3);
console.log(2,tmp);
pathname = basename; // 正确输出js和html的路径
console.log(3,pathname);
entries[pathname] = entry;
console.log(4,entry);
});
console.log("base-entrys:");
console.log(5,entries);
return entries;
}

vue 多入口文件搭建 vue多页面搭建的实例讲解

这样一来的话,就在中细分,最后输出html都在dist下。

这里的字符串操作也是和路径的情况相匹配的,如果有需要进行其他方式的设定,注意在这里修改路径的识别。

vue多页面搭建

vue 多入口文件搭建 vue多页面搭建的实例讲解

原本的webpack.dev.conf中有一个插件的设置内容

对这部分内容进行修改

vue 多入口文件搭建 vue多页面搭建的实例讲解

var
config = require('../config')
var
webpack = require('webpack')
var
merge = require('webpack-merge')
var
utils = require('./utils')
var
basewebpackconfig = 
require('./webpack.base.conf')
var
htmlwebpackplugin = 
require('html-webpack-plugin')
var path = require('path');
var glob = require('glob');
// add hot-reload related code to entry chunks
object.keys(basewebpackconfig.entry).foreach(function
 (name) {
basewebpackconfig.entry[name] = ['./build/dev-client'].concat(basewebpackconfig.entry[name])
})
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
]
})
function getentry(globpath) {
var entries = {},
basename, tmp, pathname;
glob.sync(globpath).foreach(function(entry) {
basename = path.basename(entry, path.extname(entry));
tmp = entry.split('/').splice(-3);
pathname = basename; // 正确输出js和html的路径
entries[pathname] = entry;
});
console.log("dev-entrys:");
console.log(entries);
return entries;
}
var pages = getentry('./pages/*.html');
console.log("dev pages----------------------");
for (var pathname in pages) {
console.log("filename:" + pathname + '.html');
console.log("template:" + pages[pathname]);
// 配置生成的html文件,定义路径等
var conf = {
filename: pathname + '.html',
template: pages[pathname], // 模板路径
minify: { //传递 html-minifier 选项给 minify 输出
removecomments: true
},
inject: 'body', // js插入位置
chunks: [pathname, "vendor", "manifest"] // 每个html引用的js模块,也可以在这里加上vendor等公用模块
};
// 需要生成几个html文件,就配置几个htmlwebpackplugin对象
module.exports.plugins.push(new htmlwebpackplugin(conf));
}
----------------------------------------------
webpack.prod.conf配置
和webpack.dev.conf.js中做类似的处理,
先注释掉原来的htmlwebpackplugin,然后在下面添加函数,
通过迭代插入多个htmlwebpackplugin。
var
path =require('path')
var
config =require('../config')
var
utils =require('./utils')
var
webpack =require('webpack')
var
merge =require('webpack-merge')
var
basewebpackconfig =require('./webpack.base.conf')
var
extracttextplugin =require('extract-text-webpack-plugin')
var
htmlwebpackplugin =require('html-webpack-plugin')
var
env =process.env.node_env ==='testing'
?
require('../config/test.env')
:
config.build.env
var
glob =require('glob');
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
}
}),
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,
// 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'
// }),
// 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)
 {
var
compressionwebpackplugin =require('compression-webpack-plugin')
webpackconfig.plugins.push(
new
compressionwebpackplugin({
asset:
'[path].gz[query]',
algorithm:
'gzip',
test:
newregexp(
'\\.(' +
config.build.productiongzipextensions.join('|')
 +
')$'
),
threshold:
10240,
minratio:
0.8
})
)
}
function getentry(globpath) {
var entries = {},
basename, tmp,pathname;
glob.sync(globpath).foreach(function (entry) {
basename = path.basename(entry,path.extname(entry));
tmp = entry.split('/').splice(-3);
pathname = tmp.splice(0,1) + '/' + basename; // 正确输出js和html的路径
entries[pathname] =entry;
});
console.log("prod-entrys:");
console.log(entries);
return entries;
}
var pages =getentry('./pages/*.html');
console.log("prod pages-----");
for (varpathname inpages) {
 
 console.log("filename:"+pathname +'.html');
console.log("template:"+pages[pathname]);
// 配置生成的html文件,定义路径等
var conf = {
filename: pathname +'.html',
template: pages[pathname],// 模板路径
minify:{ //
removecomments:true,
collapsewhitespace: false
},
inject: true,// js插入位置
chunks: [pathname,"vendor", "manifest"]// 每个html引用的js模块,也可以在这里加上vendor等公用模块
};
// 需要生成几个html文件,就配置几个htmlwebpackplugin对象
module.exports.plugins.push(newhtmlwebpackplugin(conf));
}

以上这篇vue 多入口文件搭建 vue多页面搭建的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。