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

对于vue中config/index.js:配置的详解

这篇文章主要介绍了关于对于vue中config/index.js:配置的详解,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
当我们需要和后台分离部署的时候,必须配置config/index.js:
用vue-cli 自动构建的目录里面  (环境变量及其基本变量的配置)
var path = require('path') module.exports = { build: { index: path.resolve(__dirname, 'dist/index.html'), assetsroot: path.resolve(__dirname, 'dist'), assetssubdirectory: 'static', assetspublicpath: '/', productionsourcemap: true }, dev: { port: 8080, proxytable: {} }}
在'build'部分,我们有以下选项:
build.index必须是本地文件系统上的绝对路径。
index.html (带着插入的资源路径) 会被生成。
如果你在后台框架中使用此模板,你可以编辑index.html路径指定到你的后台程序生成的文件。例如rails程序,可以是app/views/layouts/application.html.erb,或者laravel程序,可以是resources/views/index.blade.php。
build.assetsroot必须是本地文件系统上的绝对路径。
应该指向包含应用程序的所有静态资产的根目录。public/ 对应rails/laravel。
build.assetssubdirectory被webpack编译处理过的资源文件都会在这个build.assetsroot目录下,所以它不可以混有其它可能在build.assetsroot里面有的文件。例如,假如build.assetsroot参数是/path/to/dist,build.assetssubdirectory 参数是 static, 那么所以webpack资源会被编译到path/to/dist/static目录。
每次编译前,这个目录会被清空,所以这个只能放编译出来的资源文件。
static/目录的文件会直接被在构建过程中,直接拷贝到这个目录。这意味着是如果你改变这个规则,所有你依赖于static/中文件的绝对地址,都需要改变。
build.assetspublicpath【资源的根目录】这个是通过http服务器运行的url路径。在大多数情况下,这个是根目录(/)。如果你的后台框架对静态资源url前缀要求,你仅需要改变这个参数。在内部,这个是被webpack当做output.publicpath来处理的。
后台有要求的话一般要加上./ 或者根据具体目录添加,不然引用不到静态资源
build.productionsourcemap在构建生产环境版本时是否开启source map。
dev.port开发服务器监听的特定端口
dev.proxytable定义开发服务器的代理规则。
项目中配置的config/index.js,有dev和production两种环境的配置 以下介绍的是production环境下的webpack配置的理解
var path = require('path')module.exports = { build: { // production 环境 env: require('./prod.env'), // 使用 config/prod.env.js 中定义的编译环境 index: path.resolve(__dirname, '../dist/index.html'), // 编译输入的 index.html 文件 assetsroot: path.resolve(__dirname, '../dist'), // 编译输出的静态资源路径 assetssubdirectory: 'static', // 编译输出的二级目录 assetspublicpath: '/', // 编译发布的根目录,可配置为资源服务器域名或 cdn 域名 productionsourcemap: true, // 是否开启 csssourcemap // 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 压缩的文件扩展名 }, dev: { // dev 环境 env: require('./dev.env'), // 使用 config/dev.env.js 中定义的编译环境 port: 8080, // 运行测试页面的端口 assetssubdirectory: 'static', // 编译输出的二级目录 assetspublicpath: '/', // 编译发布的根目录,可配置为资源服务器域名或 cdn 域名 proxytable: {}, // 需要 proxytable 代理的接口(可跨域) // 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 }}
下面是vue中的build/webpack.base.conf.js
//引入依赖模块var path = require('path')var config = require('../config') // 获取配置var utils = require('./utils')var projectroot = path.resolve(__dirname, '../') var env = process.env.node_env// check env & config/index.js to decide weither to enable css sourcemaps for the// various preprocessor loaders added to vue-loader at the end of this filevar csssourcemapdev = (env === 'development' && config.dev.csssourcemap)/* 是否在 dev 环境下开启 csssourcemap ,在 config/index.js 中可配置 */var csssourcemapprod = (env === 'production' && config.build.productionsourcemap)/* 是否在 production 环境下开启 csssourcemap ,在 config/index.js 中可配置 */var usecsssourcemap = csssourcemapdev || csssourcemapprod /* 最终是否使用 csssourcemap */ module.exports = { entry: { // 配置webpack编译入口 app: './src/main.js' }, output: { // 配置webpack输出路径和命名规则 path: config.build.assetsroot, // webpack输出的目标文件夹路径(例如:/dist) publicpath: process.env.node_env === 'production' ? config.build.assetspublicpath : config.dev.assetspublicpath, // webpack编译输出的发布路径(判断是正式环境或者开发环境等) filename: '[name].js' // webpack输出bundle文件命名格式,基于文件的md5生成hash名称的script来防止缓存 }, resolve: { extensions: ['', '.js', '.vue', '.scss'], //自动解析确定的拓展名,使导入模块时不带拓展名 fallback: [path.join(__dirname, '../node_modules')], alias: { // 创建import或require的别名,一些常用的,路径长的都可以用别名 'vue$': 'vue/dist/vue', 'src': path.resolve(__dirname, '../src'), 'assets': path.resolve(__dirname, '../src/assets'), 'components': path.resolve(__dirname, '../src/components'), 'scss_vars': path.resolve(__dirname, '../src/styles/vars.scss') } }, resolveloader: { fallback: [path.join(__dirname, '../node_modules')] }, module: { loaders: [ { test: /\.vue$/, // vue文件后缀 loader: 'vue' //使用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: 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: { // .vue 文件配置 loader 及工具 (autoprefixer) loaders: utils.cssloaders({ sourcemap: usecsssourcemap }), //// 调用cssloaders方法返回各类型的样式对象(css: loader) postcss: [ require('autoprefixer')({ browsers: ['last 2 versions'] }) ] }}
webpack.prod.conf.js 生产环境下的配置文件
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')// 用于从webpack生成的bundle中提取文本到特定文件中的插件// 可以抽取出css,js文件将其与webpack输出的bundle分离var extracttextplugin = require('extract-text-webpack-plugin') //如果我们想用webpack打包成一个文件,css js分离开,需要这个插件var htmlwebpackplugin = require('html-webpack-plugin')// 一个用于生成html文件并自动注入依赖文件(link/script)的webpack插件var env = config.build.env// 合并基础的webpack配置var webpackconfig = merge(basewebpackconfig, { // 配置样式文件的处理规则,使用styleloaders module: { loaders: utils.styleloaders({ sourcemap: config.build.productionsourcemap, extract: true }) }, devtool: config.build.productionsourcemap ? '#source-map' : false, // 开启source-map,生产环境下推荐使用cheap-source-map或source-map,后者得到的.map文件体积比较大,但是能够完全还原以前的js代码 output: { path: config.build.assetsroot,// 编译输出目录 filename: utils.assetspath('js/[name].[chunkhash].js'), // 编译输出文件名格式 chunkfilename: utils.assetspath('js/[id].[chunkhash].js') // 没有指定输出名的文件输出的文件名格式 }, vue: { // vue里的css也要单独提取出来 loaders: utils.cssloaders({ // css加载器,调用了utils文件中的cssloaders方法,用来返回针对各类型的样式文件的处理方式, sourcemap: config.build.productionsourcemap, extract: true }) }, // 重新配置插件项 plugins: [ // http://vuejs.github.io/vue-loader/en/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')), // 抽离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 // filename 生成网页的html名字,可以使用/来控制文件文件的目录结构,最 // 终生成的路径是基于webpac配置的output.path的 new htmlwebpackplugin({ // 生成html文件的名字,路径和生产环境下的不同,要与修改后的publickpath相结合,否则开启服务器后页面空白 filename: config.build.index, // 源文件,路径相对于本文件所在的位置 template: 'index.html', inject: true,// 要把
vue 中build/build.js页面
// https://github.com/shelljs/shelljsrequire('./check-versions')() // 检查 node 和 npm 版本require('shelljs/global') // 使用了 shelljs 插件,可以让我们在 node 环境的 js 中使用 shellenv.node_env = 'production'var path = require('path') var config = require('../config') // 加载 config.jsvar ora = require('ora') // 一个很好看的 loading 插件var webpack = require('webpack') // 加载 webpackvar webpackconfig = require('./webpack.prod.conf') // 加载 webpack.prod.confconsole.log( // 输出提示信息 ~ 提示用户请在 http 服务下查看本页面,否则为空白页 ' tip:\n' + ' built files are meant to be served over an http server.\n' + ' opening index.html over file:// won\'t work.\n')var spinner = ora('building for production...') // 使用 ora 打印出 loading + logspinner.start() // 开始 loading 动画/* 拼接编译输出文件路径 */var assetspath = path.join(config.build.assetsroot, config.build.assetssubdirectory)rm('-rf', assetspath) /* 删除这个文件夹 (递归删除) */mkdir('-p', assetspath) /* 创建此文件夹 */ cp('-r', 'static/*', assetspath) /* 复制 static 文件夹到我们的编译输出目录 */webpack(webpackconfig, function (err, stats) { // 开始 webpack 的编译 // 编译成功的回调函数 spinner.stop() if (err) throw err process.stdout.write(stats.tostring({ colors: true, modules: false, children: false, chunks: false, chunkmodules: false }) + '\n')})
项目入口,由package.json 文件可以看出
"scripts": { "dev": "node build/dev-server.js", "build": "node build/build.js", "watch": "node build/build-watch.js" },
当我们执行 npm run dev / npm run build  / npm run watch时运行的是 node build/dev-server.js 或 node build/build.js 或node build/build-watch.js
node build/build-watch.js 是我配置的载production环境的配置基础上在webpack的配置模块加上 watch:true  便可实现代码的实时编译
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
微信小程序中如何解决wx.request对于json 含\u2028的处理异常
实例详解vue.js内置组件之keep-alive组件的使用
以上就是对于vue中config/index.js:配置的详解的详细内容。
其它类似信息

推荐信息