vue和webpack大家项目的教程很多,可以选择自定义搭建,也可以选择直接用脚手架vue-cli。这里我就不一一细说了,直接甩已有的教程:https://segmentfault.com/a/1190000008602934
而我这里的webpack配置,是我搭建项目一直用的配置:
我的简单项目结构如下:
其中build中的各文件如下:
build.js
require('./check-versions')()// 检查 Node 和 npm 版本
process.env.NODE_ENV = 'production'//指定生产环境
var ora = require('ora')// 一个很好看的 loading 插件
var rm = require('rimraf')//提供node版本的UNIX的rm -rf命令
var path = require('path')//使用Node自带的文件路径插件
var chalk = require('chalk')//控制台高亮显示的插件
var webpack = require('webpack')//使用 webpack
var config = require('../config')//使用 config/index.js
var webpackConfig = require('./webpack.prod.conf')// 加载 webpack.prod.conf
// 使用 ora 打印出 loading + log
var spinner = ora('building for production...')
spinner.start()
//https://www.npmjs.com/package/rimraf
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
if (err) throw err//如果回调函数出现错误就抛出异常
// 开始 webpack 的编译
webpack(webpackConfig, function (err, stats) {//编译回调函数
spinner.stop()
if (err) throw err//编译失败就抛出异常
process.stdout.write(stats.toString({//标准输出流
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
}) + '\n\n')
console.log(chalk.cyan(' Build complete.\n'))
console.log(chalk.yellow(
' Tip: built files are meant to be served over an HTTP server.\n' +
' Opening index.html over file:// won\'t work.\n'
))
})
})
check-versions.js
var chalk = require('chalk')// 用于在控制台输出高亮字体的插件
var semver = require('semver')// 语义化版本检查插件
var packageConfig = require('../package.json')// 引入package.json
var shell = require('shelljs')//引入shelljs
// 开辟子进程执行指令cmd并返回结果
function exec (cmd) {
return require('child_process').execSync(cmd).toString().trim()
}
// node和npm版本需求
var versionRequirements = [
{
name: 'node',
currentVersion: semver.clean(process.version),
versionRequirement: packageConfig.engines.node
},
]
if (shell.which('npm')) {
versionRequirements.push({
name: 'npm',
currentVersion: exec('npm --version'),
versionRequirement: packageConfig.engines.npm
})
}
module.exports = function () {
var warnings = []
// 依次判断版本是否符合要求
for (var i = 0; i < versionRequirements.length; i++) {
var mod = versionRequirements[i]
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
warnings.push(mod.name + ': ' +
chalk.red(mod.currentVersion) + ' should be ' +
chalk.green(mod.versionRequirement)
)
}
}
if (warnings.length) {
console.log('')
// 如果有警告则将其输出到控制台
console.log(chalk.yellow('To use this template, you must update following to modules:'))
console.log()
for (var i = 0; i < warnings.length; i++) {
var warning = warnings[i]
console.log(' ' + warning)
}
console.log()
process.exit(1)
}
}
dev.client.js
/* eslint-disable */
require('eventsource-polyfill')
var hotClient = require('webpack-hot-middleware/client?noInfo=true&reload=true')
hotClient.subscribe(function (event) {
if (event.action === 'reload') {
window.location.reload()
}
})
dev.server.js
require('./check-versions')()
var config = require('../config')
if(!process.env.NODE_ENV) {
process.env.NODE_ENV = JSON.parse(config.dev.env.NODE_ENV)
}
var opn = require('opn')
var path = require('path')
var express = require('express')
var webpack = require('webpack')
var proxyMiddleware = require('http-proxy-middleware')
var webpackConfig = require('./webpack.dev.conf')
// default port where dev server listens for incoming traffic
var port = process.env.PORT || config.dev.port
// automatically open browser, if not set will be false
var autoOpenBrowser = !!config.dev.autoOpenBrowser
// Define HTTP proxies to your custom API backend
// https://github.com/chimurai/http-proxy-middleware
var proxyTable = config.dev.proxyTable
var app = express()
var compiler = webpack(webpackConfig)
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(bodyParser.json())
var devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
quiet: true
})
var hotMiddleware = require('webpack-hot-middleware')(compiler, {
log: false,
heartbeat: 2000
})
// force page reload when html-webpack-plugin template changes
compiler.plugin('compilation', function(compilation) {
compilation.plugin('html-webpack-plugin-after-emit', function(data, cb) {
hotMiddleware.publish({
action: 'reload'
})
cb()
})
})
// proxy api requests
var proxy = require('express-http-proxy');
Object.keys(proxyTable).forEach(function(context) {
var options = proxyTable[context]
// if (typeof options === 'string') {
// options = { target: options }
// }
// app.use(proxyMiddleware(options.filter || context, options))
var apiProxy = proxy(typeof options === 'string' ? options : options.target, {});
app.use(context, apiProxy);
})
// handle fallback for HTML5 history API
app.use(require('connect-history-api-fallback')())
// serve webpack bundle output
app.use(devMiddleware)
// enable hot-reload and state-preserving
// compilation error display
app.use(hotMiddleware)
// serve pure static assets
var staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
app.use(staticPath, express.static('./static'))
var uri = 'http://localhost:' + port
var _resolve
var readyPromise = new Promise(resolve => {
_resolve = resolve
})
console.log('> Starting dev server...')
devMiddleware.waitUntilValid(() => {
console.log('> Listening at ' + uri + '\n')
// when env is testing, don't need open it
if(autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
opn(uri)
}
_resolve()
})
var server = app.listen(port)
module.exports = {
ready: readyPromise,
close: () => {
server.close()
}
}
utils.js
var path = require('path')
var config = require('../config')
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 || {}
var cssLoader = {
loader: 'css-loader',
options: {
minimize: process.env.NODE_ENV === 'production',
sourceMap: options.sourceMap
}
}
// generate loader string to be used with extract text plugin
function generateLoaders (loader, loaderOptions) {
var loaders = [cssLoader]
if (loader) {
loaders.push({
loader: loader + '-loader',
options: Object.assign({}, loaderOptions, {
sourceMap: options.sourceMap
})
})
}
// Extract CSS when that option is specified
// (which is the case during production build)
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader'
})
} else {
return ['vue-style-loader'].concat(loaders)
}
}
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
return {
css: generateLoaders(),
postcss: generateLoaders(),
less: generateLoaders('less'),
sass: generateLoaders('sass', { indentedSyntax: true }),
scss: generateLoaders('sass'),
stylus: generateLoaders('stylus'),
styl: generateLoaders('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 + '$'),
use: loader
})
}
return output
}
vue-loader.conf.js
var utils = require('./utils')
var config = require('../config')
var isProduction = process.env.NODE_ENV === 'production'
module.exports = {
loaders: utils.cssLoaders({
sourceMap: isProduction
? config.build.productionSourceMap
: config.dev.cssSourceMap,
extract: isProduction
}),
transformToRequire: {
video: 'src',
source: 'src',
img: 'src',
image: 'xlink:href'
}
}
webpack.base.conf.js
var path = require('path') // 使用 NodeJS 自带的文件路径插件
var utils = require('./utils') //封装了一些方法的工具
var config = require('../config') //使用 config/index.js
var vueLoaderConfig = require('./vue-loader.conf') //使用vue-loader.conf
// 拼接我们的工作区路径为一个绝对路径
function resolve (dir) {
return path.join(__dirname, '..', dir)
}
module.exports = {
entry: {
// 编译文件入口
app: './src/main.js'
},
output: {
//使用chonfig/index.js中build的assetsRoot作为输出根路径
path: config.build.assetsRoot,
filename: '[name].js', //编译输入的文件名
publicPath: process.env.NODE_ENV === 'production' // 正式发布环境下编译输出的发布路径
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
// 自动补全的扩展名,能够使用户在引入模块时不带扩展
extensions: ['.js', '.vue', '.json'],
// 默认路径代理,例如 import Vue from 'vue$',会自动到 'vue/dist/vue.esm.js'中寻找
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src')
}
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
}
}
webpack.dev.conf.js
var utils = require('./utils') //封装了一些方法的工具
var webpack = require('webpack') //使用 webpack
var config = require('../config') //使用 config/index.js
var merge = require('webpack-merge') //使用 webpack 配置合并插件
var baseWebpackConfig = require('./webpack.base.conf') // 加载 webpack.base.conf
var HtmlWebpackPlugin = require('html-webpack-plugin') // 使用 html-webpack-plugin 插件,这个插件可以帮我们自动生成 html 并且注入到 .html 文件中
//https://www.npmjs.com/package/friendly-errors-webpack-plugin,可以识别某些类别的Webpack错误并进行清理,聚合和优先排序
var FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
// add hot-reload related code to entry chunks
//将 Hol-reload 相对路径添加到 webpack.base.conf 的 对应 entry 前
Object.keys(baseWebpackConfig.entry).forEach(function (name) {
baseWebpackConfig.entry[name] = ['./build/dev-client'].concat(baseWebpackConfig.entry[name])
})
// 将我们 webpack.dev.conf.js 的配置和 webpack.base.conf.js 的配置合并
module.exports = merge(baseWebpackConfig, {
module:{
// 使用 styleLoaders
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
},
// 使用 #cheap-module-eval-source-map 模式作为开发辅助调试工具
// 具体配置请参考https://doc.webpack-china.org/configuration/devtool/
devtool: '#cheap-module-eval-source-map',
plugins: [
// definePlugin 接收字符串插入到代码当中, 需要的话可以写上 JS 的字符串
new webpack.DefinePlugin({
'process.env': config.dev.env
}),
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
// HotModule 插件在页面进行变更的时候只会重回对应的页面模块,不会重绘整个 html 文件
new webpack.HotModuleReplacementPlugin(),
//https://doc.webpack-china.org/plugins/no-emit-on-errors-plugin/
//在编译出现错误时,使用 NoEmitOnErrorsPlugin 来跳过输出阶段。这样可以确保输出资源不会包含错误。
new webpack.NoEmitOnErrorsPlugin(),
// https://github.com/ampedandwired/html-webpack-plugin
// 将 index.html 作为入口,注入 html 代码后生成 index.html文件
//https://doc.webpack-china.org/plugins/html-webpack-plugin/ webpack插件列表(中文)
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true
}),
//看上面
new FriendlyErrorsPlugin()
]
})
webpack.prod.conf.js
var path = require('path')
var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge') // 在大型项目中,可能 webpack.config.js 会变得越来越臃肿,这个时候可以利用做 webpack-merge 插件。将配置定义在一个目录下面的不同文件中,然后通过 webpack-merge 来合并成最终的配置。
var baseWebpackConfig = require('./webpack.base.conf')
//可以将单个文件或整个目录复制到构建目录中
var CopyWebpackPlugin = require('copy-webpack-plugin')
// 一个可以插入 html 并且创建新的 .html 文件的插件
var HtmlWebpackPlugin = require('html-webpack-plugin')
// 一个 webpack 扩展,可以提取一些代码并且将它们和文件分离开
// 如果我们想将 webpack 打包成一个文件 css js 分离开,那我们需要这个插件
var ExtractTextPlugin = require('extract-text-webpack-plugin')
//一个个优化/最小化css资源的插件
var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
var env = config.build.env
//合并 webpack.base.conf.js中的配置,里面具体的配置参考webpack.base.conf.js里面的注释
var webpackConfig = merge(baseWebpackConfig, {
module: {
rules: 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'), //编译输出带hash的文件名,可以指定hash长度(chunkhash:6)
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') // 没有指定输出名的文件输出的文件名
},
plugins: [
// http://vuejs.github.io/vue-loader/en/workflow/production.html
// definePlugin 接收字符串插入到代码当中, 所以你需要的话可以写上 JS 的字符串
new webpack.DefinePlugin({
'process.env': env
}),
// 压缩 js (同样可以压缩 css)
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
sourceMap: true
}),
// extract css into its own file
//将 css 文件分离出来
new ExtractTextPlugin({
filename: utils.assetsPath('css/[name].[contenthash].css')
}),
// Compress extracted CSS. We are using this plugin so that possible
// duplicated CSS from different components can be deduped.
//压缩css代码
new OptimizeCSSPlugin({
cssProcessorOptions: {
safe: true
}
}),
// 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
// 输入输出的 .html 文件
new HtmlWebpackPlugin({
filename: process.env.NODE_ENV === 'testing'
? 'index.html'
: config.build.index,
template: 'index.html',
inject: true, // 是否注入 html
minify: { // 压缩的方式
removeComments: true, //移除带html的注释
collapseWhitespace: true, //移除空格
removeAttributeQuotes: true //移除属性的引号
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
//https://doc.webpack-china.org/plugins/commons-chunk-plugin/
chunksSortMode: 'dependency' //资源按照依赖关系去插入
}),
// split vendor js into its own file//将引用的库文件拆出来打包到一个[name].js文件中
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module, count) {
// any required modules inside node_modules are extracted to vendor
//任何一个从node_modules中引用的模块都会被打包进来
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
//https://doc.webpack-china.org/concepts/manifest/
//把webpack的runtime和manifest这些webpack管理所有模块交互的代码打包到[name].js文件中,防止build之后vendor的hash值被更新
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
chunks: ['vendor']
}),
// copy custom static assets
//复制自定义的静态资源文件到dist/static文件夹中
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
}
])
]
})
// 开启 gzip 的情况下使用下方的配置
if (config.build.productionGzip) {
// Gzip依赖 compression-webpack-plugin 插件
var CompressionWebpackPlugin = require('compression-webpack-plugin')
// 向webpackconfig.plugins中加入下方的插件
webpackConfig.plugins.push(
// 使用 compression-webpack-plugin 插件进行压缩,https://doc.webpack-china.org/plugins/compression-webpack-plugin/
new CompressionWebpackPlugin({
asset: '[path].gz[query]',//目标资源名称
algorithm: 'gzip',//压缩方式
test: new RegExp(
'\\.(' +
config.build.productionGzipExtensions.join('|') +
')$'
),//所有匹配该正则的资源都会被处理。默认值是全部资源。
threshold: 10240,//只有大小大于该值的资源会被处理。单位是 bytes。默认值是 0。
minRatio: 0.8//只有压缩率小于这个值的资源才会被处理。默认值是 0.8。
})
)
}
//配置项目分析工具加载下方插件
if (config.build.bundleAnalyzerReport) {
var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}
module.exports = webpackConfig
config中的文件如下:
dev.env.js
var merge = require('webpack-merge')
var prodEnv = require('./prod.env')
module.exports = merge(prodEnv, {
NODE_ENV: '"development"'
})
index.js
var path = require('path') //使用Node自带的文件路径插件
module.exports = {
// 生产环境配置
build: {
// 使用 config/prod.env.js 中定义的编译环境
env: require('./prod.env'),
index: path.resolve(__dirname, '../dist/index.html'), // 编译注入的 index.html 文件,必须是本地的绝对路径
assetsRoot: path.resolve(__dirname, '../dist'), // 编译输出的静态资源根路径
assetsSubDirectory: 'static', // 编译输出的二级目录
assetsPublicPath: '/', // 编译发布上线路径的根目录,可配置为资源服务器域名或 CDN 域名
productionSourceMap: true, //生成用于生产构建的源映射
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false, // 是否开启 gzip
productionGzipExtensions: ['js', 'css'], // 需要使用 gzip 压缩的文件扩展名
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report //一个实用工具,用于分析项目的依赖关系https://www.npmjs.com/package/webpack-bundle-analyzer
},
// 开发环境
dev: {
env:require('./dev.env'), // 使用 config/dev.env.js 中定义的编译环境
port: 8080, // 运行测试页面的端口
autoOpenBrowser: true, //是否自动打开浏览器
assetsSubDirectory: 'static', // 编译输出的二级目录
assetsPublicPath: '/', // 编译发布上线路径的根目录,可配置为资源服务器域名或 CDN 域名
proxyTable: {
//https://github.com/chimurai/http-proxy-middleware,配置方式
}, // 需要 proxyTable 代理的接口(可跨域)http://vuejs-templates.github.io/webpack/proxy.html
// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false // 是否开启 cssSourceMap
}
}
prod.env.js
module.exports = {
NODE_ENV: '"production"'
}
相关配置已经介绍完了,下面看主要的代码:我想实现的想过很简单,就是运行后出现下面的页面:
我们先看组件库中的几个组件:
about.vue
<template>
<div>about</div>
</template>
<script>
</script>
<style>
</style>
header.vue
<template>
<div>
<h1>共同的header</h1>
<img src="../assets/imgs/vue.png">
</div>
</template>
<script>
</script>
<style>
</style>
记得截个图片放到imgs中去。
home.vue
<template>
<div>
<ol>
<li v-for="todo in todos">
{{ todo.text }}
</li>
</ol>
<button @click="eClick()">事件</button>
</div>
</template>
<script>
export default {
name: 'indexP',
data () {
return {
todos: [
{ text: 'Learn JavaScript' },
{ text: 'Learn Vue' },
{ text: 'Build something awesome' }
]
}
},
methods:{
eClick(){
console.log(9999);
}
}
}
</script>
router.js
import Vue from 'vue'
import Router from 'vue-router'
import indexPage from './components/header.vue'
import homePage from './components/home.vue'
import aboutPage from './components/about.vue'
Vue.use(Router)
export default new Router({
routes:[
{
path:'/',
component:homePage
},{
path:'/about',
component:aboutPage
}
]
})
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
</div>
<script src="./dist/build.js"></script>
</body>
</html>
App.vue
<template>
<div id="app">
<header-tab></header-tab>
<h2>{{msg}}</h2>
<div class="nav-box">
<p class="nav-list">
<router-link class="nav-item" to="/">首页</router-link>
<router-link class="nav-item" to="/about">关于</router-link>
</p>
</div>
<div>
<router-view></router-view>
</div>
</div>
</template>
<script>
import HeaderTab from './components/header.vue'
let data = () => {
return {
msg: 'Welcome to Your Vue.js App'
}
}
export default {
name: 'app',
data:data,
components:{
HeaderTab
}
}
</script>
<style>
h2{
color:#f00;
}
#app {
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
h1, h2 {
font-weight: normal;
}
ul {
list-style-type: none;
padding: 0;
}
li {
text-align: left;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router.js'
import './assets/styles/base.css'
Vue.config.debug = true
new Vue({
router,
el:'#app',
render:h=>h(App)
})
base.css
h1{
color: #999;
}
好了,写完之后,从控制台进入到你所建的文件下面,npm run dev就可以了。