您好,欢迎访问一九零五行业门户网

vue-cli+bulid配置文件步骤详解

这次给大家带来vue-cli+bulid配置文件步骤详解,vue-cli+bulid配置文件的注意事项有哪些,下面就是实战案例,一起来看一下。
本文章适合初学者学习,如有错请提出。近期对vue比较感兴趣,所以准备用vue写一个blog。早期先对vue脚手架了解一下,对于新手官网建议先不用vue-cli,但我觉得如果没有任何的依据凭自己写一个项目(包括webpack的配置等)这无疑是浪费时间的而且都最后还是是而非的。所以我觉得完全可以用脚手架建一个webpack项目,然后我们可以具体对应它生成的文件学习(当然这只是我的学习方法,我认为这样比较好学,但不一定人人都是这样的)。
在学习的过程中发现网上许多的简介都已经过期(vue发展的过快了吧。。。。),所以我结合自己的项目和网上的资料备注一下,希望和其他的人一起讨论。这个适合的版本为:nodejs(6.10.2)、vue(2.5.2)、vue-router(3.0.1)和webpack(3.6.0)的。适合的环境为windows的,其他的系统我也不知道可不可以用。
一、vue-cli安装、webpack项目新建
1、默认电脑已经安装了node,不会的请百度然后先安装nodejs。
2、安装好nodejs之后,全局安装vue-cli:npm install -g vue-cli。
3、新建webpack项目:vue init webpack projectname(这是比较完整的,我们学习用这个比较好)、vue init webpack-simple projectname(简易版的)。
注意:projectname项目名不能用中文。
4、“vue init webpack-simple projectname”创建新项目的目录结构:
生成新项目时并没有安装依赖,需要进入新的项目安装依赖:cd projectname -> npm install。
新建项目时,会需要填一些东西,但如果你都不想填也无所谓,全部默认、全部yes都行:
(1)、project name:——项目名称
(2)、project description:——项目描述
(3)、author:——作者
(4)、vue build:——构建模式,一般默认选择第一种
(5)、install vue-router?:——是否安装引入vue-router,这里选是,vue-router是路由组件,后面构建项目会用到
(6)、use eslint to lint your code?:——eslint的格式验证非常严格,多一个空格少一个空格都会报错。个人觉得如果是平时练习的话可以选yes因为这个可以规范自己js代码的书写规范。但在实际开发项目中不建议使用,会影响开发效率。
(7)、setup unit tests with karma + mocha 以及setup e2e tests with nightwatch这两个是测试,可以不用安装。
“vue init webpack projectname”创建新项目的目录结构:
二、build目录下配置文件之check-versions.js
这个文件并不是十分重要,只要稍微了解就行了。
/**  * 验证版本  */ 'use strict' //chalk是一个颜色插件。可以通过 const chalk = require('chalk') //semver一个版本控制插件 const semver = require('semver') const packageconfig = require('../package.json') //shelljss是nodejs对与多进程的支持,是对于child_process封装 const shell = require('shelljs') function exec (cmd) {  return require('child_process').execsync(cmd).tostring().trim() } const versionrequirements = [  {//对应node的版本   name: 'node',   //当前环境版本,semver.clean把当前环境版本信息转化规定格式,也是' =v1.2.3 '->'1.2.3'   currentversion: semver.clean(process.version),   //要求版本,对应package.json的engines所配置的信息   versionrequirement: packageconfig.engines.node  } ] //npm环境中 if (shell.which('npm')) {  versionrequirements.push({   name: 'npm',   //执行方法得到版本号   currentversion: exec('npm --version'),   versionrequirement: packageconfig.engines.npm  }) } module.exports = function () {  const warnings = []  for (let i = 0; i < versionrequirements.length; i++) { const mod = versionrequirements[i] //如果版本号不符合package.json文件中指定的版本号,就执行下面的代码 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 (let i = 0; i < warnings.length; i++) { const warning = warnings[i] console.log(' ' + warning) } console.log() process.exit(1) } }
三、build目录下配置文件之utils.js
这个文件主要用于处理有关于css方面的,主要对后面vue-loader.conf.js文件有关系,对webpack配置loaders方面也有影响。
/** * webpack开发环境:主要用来处理css-loader和vue-style-loader */ 'use strict' const path = require('path') const config = require('../config') //引入extract-text-webpack-plugin插件,用来将css提取到单独的css文件中 const extracttextplugin = require('extract-text-webpack-plugin') const packageconfig = require('../package.json') exports.assetspath = function (_path) { //process.env.node_env在bulid.js中定义 //如果为生产环境assetssubdirectory为“static”,否则也为“static” //config.build.assetssubdirectory与config.dev.assetssubdirectory都在config/index中定义 const assetssubdirectory = process.env.node_env === 'production' ? config.build.assetssubdirectory : config.dev.assetssubdirectory //path.join和path.posix.join区别前者返回完整路径,后者返回完整路径的相对路径 //例:path.join是e:/shishans/blogsss/static,path.posix.join是static return path.posix.join(assetssubdirectory, _path) } exports.cssloaders = function (options) { options = options || {} //css-loader的基本配置 const cssloader = { loader: 'css-loader', options: { //option用于配置loder的 //是否开启cssmap,默认是false //一般我们会压缩js或者css以节省宽带,但在开发压缩就很难调试 //所以用sourcemap进行关联,给出对应的sourcemap文件 sourcemap: options.sourcemap } } const postcssloader = { loader: 'postcss-loader', options: { sourcemap: options.sourcemap } } // generate loader string to be used with extract text plugin function generateloaders (loader, loaderoptions) { //将上面的基础配置放到一个数据中 const loaders = options.usepostcss ? [cssloader, postcssloader] : [cssloader] //如果该函数传递了单独的loder就加入到loaders数组中例如:sass或者less之类的 if (loader) { loaders.push({ //加载对应的loader loader: loader + '-loader', //es6方法object.assign:主要用于合并对象的,浅拷贝 options: object.assign({}, loaderoptions, { sourcemap: options.sourcemap }) }) } // extract css when that option is specified // (which is the case during production build) // extract自定义属性,用extracttextplugin.extract控制是否把文件单独提取 // true:单独提取,false表示不提取 if (options.extract) { return extracttextplugin.extract({ use: loaders, fallback: 'vue-style-loader' }) } else { //[].concat()方法用于连接数组 return ['vue-style-loader'].concat(loaders) } } // https://vue-loader.vuejs.org/en/configurations/extract-css.html return { css: generateloaders(),//返回[cssloader, vue-style-loader] postcss: generateloaders(),//返回[cssloader, vue-style-loader] less: generateloaders('less'),//返回[cssloader, vue-style-loader, less] sass: generateloaders('sass', { indentedsyntax: true }), scss: generateloaders('sass'), stylus: generateloaders('stylus'), styl: generateloaders('stylus') } } // generate loaders for standalone style files (outside of .vue) // 这个方法主要处理import这种方式导入的文件类型的打包 exports.styleloaders = function (options) { const output = [] const loaders = exports.cssloaders(options) for (const extension in loaders) { const loader = loaders[extension] output.push({ test: new regexp('\\.' + extension + '$'), use: loader }) } return output } //用于返回脚手架错误的函数 exports.createnotifiercallback = () => {  //使用node-notifier来发送桌面消息,包括应用状态改变以及错误信息  const notifier = require('node-notifier')  return (severity, errors) => {   if (severity !== 'error') return   const error = errors[0]   const filename = error.file && error.file.split('!').pop()   notifier.notify({    title: packageconfig.name,    message: severity + ': ' + error.name,    subtitle: filename || '',    icon: path.join(dirname, 'logo.png')   })  } }
四、build目录下配置文件之webpack.base.conf.js
从这个文件开始,webpack配置文件正式开始,前面的相当于是这个文件参数般的存在。而实际上这个也不是正式会运行的配置文件。一个项目有2中情况:开发环境和生成环境。这2中环境一些方面的配置是不一样的,比如在生产环境我们会对js和css进行压缩以减少宽带。这个文件实际上是这2中环境通用的配置。下面的webpack.dev.conf.js文件(开发环境)、
webpack.prod.conf.js(生产环境),这2个文件才是实际环境运行使用的配置文件。
/**  * webpack开发环境和生成环境通用的配置  */ 'use strict' const path = require('path') const utils = require('./utils') const config = require('../config') const vueloaderconfig = require('./vue-loader.conf') //获取对应文件路径的函数 //因为该文件是在项目的二级文件build下,所以要加上../这样才能找到像src这样的目录 function resolve (dir) {  //join方法用于将多个字符串结合成一个路径字符串  //path在node中会经常用到可以仔细了解一下path的各种方法  //dirname:获取当前文件所在目录的完整绝对路径  return path.join(dirname, '..', dir) } //eslint用来检查我们写的js代码是否满足指定的规则 const createlintingrule = () => ({  test: /\.(js|vue)$/,  loader: 'eslint-loader',  enforce: 'pre',  include: [resolve('src'), resolve('test')],  options: {   formatter: require('eslint-friendly-formatter'),   emitwarning: !config.dev.showeslinterrorsinoverlay  } }) module.exports = {  context: path.resolve(dirname, '../'),  entry: {   //入口文件是src下的main.js   app: './src/main.js'  },  output: {   path: config.build.assetsroot,   filename: '[name].js',   publicpath: process.env.node_env === 'production'    ? config.build.assetspublicpath    : config.dev.assetspublicpath  },  resolve: {   //自动解析确定的扩展,在引入模块时不带扩展名   //例如:import somejs from @/some   extensions: ['.js', '.vue', '.json'],   alias: {    // 后面的$符号指精确匹配    // 也就是说只能使用 import vuejs from vue 这样的方式导入vue.esm.js文件    'vue$': 'vue/dist/vue.esm.js',    // resolve('src') 其实在这里就是项目根目录中的src目录    // 例如引用src目录下的some.js方法:import somejs from @/some.js    // 用@来代替../src    '@': resolve('src'),   }  },  module: {   rules: [    ...(config.dev.useeslint ? [createlintingrule()] : []),    {     test: /\.vue$/,     loader: 'vue-loader',     options: vueloaderconfig    },    {     test: /\.js$/,     loader: 'babel-loader',     include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]    },    {     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]')     }    }   ]  },  node: {   // prevent webpack from injecting useless setimmediate polyfill because vue   // source contains it (although only uses it if it's native).   setimmediate: false,   // prevent webpack from injecting mocks to node native modules   // that does not make sense for the client   dgram: 'empty',   fs: 'empty',   net: 'empty',   tls: 'empty',   child_process: 'empty'  } }
五、build目录下配置文件之webpack.dev.conf.js
webpack.prod.conf.js也差不多。这2者之间的差别以后再讨论。
/**  * 此文件用于开发环境下的webpack配置  * 就本项目执行npm run dev 和 npm run start都会用到这个文件的配置  * 具体可以参考javascript中scripts的配置  */ 'use strict' const utils = require('./utils') const webpack = require('webpack') const config = require('../config') const merge = require('webpack-merge') const path = require('path') const basewebpackconfig = require('./webpack.base.conf') const copywebpackplugin = require('copy-webpack-plugin') //生成html文件 const htmlwebpackplugin = require('html-webpack-plugin') //friendly-errors-webpack-plugin:把webpack的错误和日志搜集起来展现给用户 const friendlyerrorsplugin = require('friendly-errors-webpack-plugin') const portfinder = require('portfinder') const host = process.env.host const port = process.env.port && number(process.env.port) const devwebpackconfig = merge(basewebpackconfig, {  module: {   rules: utils.styleloaders({ sourcemap: config.dev.csssourcemap, usepostcss: true })  },  // cheap-module-eval-source-map is faster for development  // devtool是开发工具选项,用来指定如何生成sourcemap文件,cheap-module-eval-source-map此款soucemap文件性价比最高  // 生产环境:#source-map  // 开发环境:#cheap-module-eval-source-map 编译消耗小  devtool: config.dev.devtool,  // these devserver options should be customized in /config/index.js  devserver: {   clientloglevel: 'warning',   historyapifallback: {    rewrites: [     { from: /.*/, to: path.posix.join(config.dev.assetspublicpath, 'index.html') },    ],   },   hot: true,   contentbase: false, // since we use copywebpackplugin.   compress: true,   host: host || config.dev.host,   port: port || config.dev.port,   open: config.dev.autoopenbrowser,   overlay: config.dev.erroroverlay    ? { warnings: false, errors: true }    : false,   publicpath: config.dev.assetspublicpath,   proxy: config.dev.proxytable,   quiet: true, // necessary for friendlyerrorsplugin   watchoptions: {    poll: config.dev.poll,   }  },  plugins: [   // defineplugin内置webpack插件,专门用来定义全局变量的   // 下面定义一个全局变量 process.env 并且值是如下   new webpack.defineplugin({    'process.env': require('../config/dev.env')   }),   // 这个插件帮助你实现无刷新加载,关于内部实现原理   new webpack.hotmodulereplacementplugin(),   new webpack.namedmodulesplugin(), // hmr shows correct file names in console on update.   new webpack.noemitonerrorsplugin(),   // https://github.com/ampedandwired/html-webpack-plugin   new htmlwebpackplugin({    filename: 'index.html',    template: 'index.html',    inject: true   }),   // copy custom static assets   new copywebpackplugin([    {     from: path.resolve(dirname, '../static'),     to: config.dev.assetssubdirectory,     ignore: ['.*']    }   ])  ] }) module.exports = new promise((resolve, reject) => {  portfinder.baseport = process.env.port || config.dev.port  portfinder.getport((err, port) => {   if (err) {    reject(err)   } else {    // publish the new port, necessary for e2e tests    process.env.port = port    // add port to devserver config    devwebpackconfig.devserver.port = port    // add friendlyerrorsplugin    devwebpackconfig.plugins.push(new friendlyerrorsplugin({     compilationsuccessinfo: {      messages: [`your application is running here: http://${devwebpackconfig.devserver.host}:${port}`],     },     onerrors: config.dev.notifyonerrors     ? utils.createnotifiercallback()     : undefined    }))    resolve(devwebpackconfig)   }  }) })
六、config目录下之index.js
这个文件配置了一些全局属性,分别dev和build用于区别开发环境和生产环境不同的地方。
七、总结
在vue2.5.2中取消了build目录中的dev-server.js和dev-client.js文件,改用webpack.dev.conf.js代替,所以 配置本地访问在webpack.dev.conf.js里配置即可。具体如何配置以后运用到时候具体了解,本文章就不讲了。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
el获取上下文参数步骤详解
el表达式怎样判断非空
以上就是vue-cli+bulid配置文件步骤详解的详细内容。
其它类似信息

推荐信息