Vue、webpack中默认的config.js、index.js 配置详情

vue

在vue.js 框架搭建好后,其vue-cli 自动构建的目录里面相关环境变量及其基本变量配置,如下代码所示:

module.exports = {

build: {

index: path.resolve(__dirname, '../dist/index.html'),

assetsRoot: path.resolve(__dirname, '../dist'),

assetsSubDirectory: 'static',

assetsPublicPath: '/',

productionSourceMap: true,

devtool: '#source-map',

productionGzip: false,

productionGzipExtensions: ['js', 'css'],

bundleAnalyzerReport: process.env.npm_config_report

},

}

build 各参数含义:

       index:必须是本地文件系统上的绝对路径,index.html (带着插入的资源路径) 会被生成.

       assetsRoot:指向包含应用程序的所有静态资产的根目录。

       assetsSubDirectory:存放编译出来的资源文件,static/ 目录下的文件会在构建过程中直接拷贝到这个目录下。

       assetsPublicPath:资源的根目录,这个是通过http服务器运行的url路径。在大多数情况下,这个是根目录(/)。如果你的后台框架对静态资源url前缀有要求,你仅需要改变这个参数。在内部,这个是被webpack当做output.publicPath来处理的。

                                  后台有要求的话一般要加上./ 或者根据具体目录添加,不然引用不到静态资源

       productionSourceMap:构建生产环境版本是是否开启source map.

       productionGzip:是否开启gzip.

       productionGzipExtensions:需要使用Gzip 压缩的文件扩展名.

       bundleAnalyzerReport:检测包的情况,在config/index.js中一个相关插件叫做bundleAnalyzerReport,当打包输入npm run build --report,就可以在打包的同时查看每个打包生成的js,查看里面相关库的构成,以便我们进行相关的删减,即库太大,是否可用自己实                                           现,有的库是否有必要,可否进行删除.

     

      项目中配置的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 file

var 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.js 框架搭建好后,其vue-cli 自动构建的目录里面相关环境变量及其基本变量配置,如下代码所示:

module.exports = {

build: {

index: path.resolve(__dirname, '../dist/index.html'),

assetsRoot: path.resolve(__dirname, '../dist'),

assetsSubDirectory: 'static',

assetsPublicPath: '/',

productionSourceMap: true,

devtool: '#source-map',

productionGzip: false,

productionGzipExtensions: ['js', 'css'],

bundleAnalyzerReport: process.env.npm_config_report

},

}

build 各参数含义:

       index:必须是本地文件系统上的绝对路径,index.html (带着插入的资源路径) 会被生成.

       assetsRoot:指向包含应用程序的所有静态资产的根目录。

       assetsSubDirectory:存放编译出来的资源文件,static/ 目录下的文件会在构建过程中直接拷贝到这个目录下。

       assetsPublicPath:资源的根目录,这个是通过http服务器运行的url路径。在大多数情况下,这个是根目录(/)。如果你的后台框架对静态资源url前缀有要求,你仅需要改变这个参数。在内部,这个是被webpack当做output.publicPath来处理的。

                                  后台有要求的话一般要加上./ 或者根据具体目录添加,不然引用不到静态资源

       productionSourceMap:构建生产环境版本是是否开启source map.

       productionGzip:是否开启gzip.

       productionGzipExtensions:需要使用Gzip 压缩的文件扩展名.

       bundleAnalyzerReport:检测包的情况,在config/index.js中一个相关插件叫做bundleAnalyzerReport,当打包输入npm run build --report,就可以在打包的同时查看每个打包生成的js,查看里面相关库的构成,以便我们进行相关的删减,即库太大,是否可用自己实                                           现,有的库是否有必要,可否进行删除.

     

      项目中配置的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 file

var 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、webpack中默认的config.js、index.js 配置详情 的全部内容, 来源链接: utcz.com/z/380754.html

回到顶部