详解vue2.0脚手架的webpack 配置文件分析(3)

// see for documentation. // 不再重复介绍了 ... var path = require('path') module.exports = { // production 环境 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', // 编译发布上线路径的根目录,可配置为资源服务器域名或 CDN 域名 assetsPublicPath: 'https://www.jb51.net/', // 是否开启 cssSourceMap 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 // 是否开启 gzip productionGzip: false, // 需要使用 gzip 压缩的文件扩展名 productionGzipExtensions: ['js', 'css'] }, // dev 环境 dev: { // 使用 config/dev.env.js 中定义的编译环境 env: require('./dev.env'), // 运行测试页面的端口 port: 8080, // 编译输出的二级目录 assetsSubDirectory: 'static', // 编译发布上线路径的根目录,可配置为资源服务器域名或 CDN 域名 assetsPublicPath: 'https://www.jb51.net/', // 需要 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 cssSourceMap: false } }

至此,我们的 npm run dev 命令就讲解完毕,下面让我们来看一看执行 npm run build 命令时发生了什么 ~

build.js

// https://github.com/shelljs/shelljs // 检查 Node 和 npm 版本 require('./check-versions')() // 使用了 shelljs 插件,可以让我们在 node 环境的 js 中使用 shell require('shelljs/global') env.NODE_ENV = 'production' // 不再赘述 var path = require('path') // 加载 config.js var config = require('../config') // 一个很好看的 loading 插件 var ora = require('ora') // 加载 webpack var webpack = require('webpack') // 加载 webpack.prod.conf var webpackConfig = require('./webpack.prod.conf') // 输出提示信息 ~ 提示用户请在 http 服务下查看本页面,否则为空白页 console.log( ' Tip:\n' + ' Built files are meant to be served over an HTTP server.\n' + ' Opening index.html over file:// won\'t work.\n' ) // 使用 ora 打印出 loading + log var spinner = ora('building for production...') // 开始 loading 动画 spinner.start() // 拼接编译输出文件路径 var assetsPath = path.join(config.build.assetsRoot, config.build.assetsSubDirectory) // 删除这个文件夹 (递归删除) rm('-rf', assetsPath) // 创建此文件夹 mkdir('-p', assetsPath) // 复制 static 文件夹到我们的编译输出目录 cp('-R', 'static/*', assetsPath) // 开始 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') })

webpack.prod.conf.js

// 不再赘述 var path = require('path') // 加载 confi.index.js var config = require('../config') // 使用一些小工具 var utils = require('./utils') // 加载 webpack var webpack = require('webpack') // 加载 webpack 配置合并工具 var merge = require('webpack-merge') // 加载 webpack.base.conf.js var baseWebpackConfig = require('./webpack.base.conf') // 一个 webpack 扩展,可以提取一些代码并且将它们和文件分离开 // 如果我们想将 webpack 打包成一个文件 css js 分离开,那我们需要这个插件 var ExtractTextPlugin = require('extract-text-webpack-plugin') // 一个可以插入 html 并且创建新的 .html 文件的插件 var HtmlWebpackPlugin = require('html-webpack-plugin') var env = config.build.env // 合并 webpack.base.conf.js var webpackConfig = merge(baseWebpackConfig, { module: { // 使用的 loader loaders: utils.styleLoaders({ sourceMap: config.build.productionSourceMap, extract: true }) }, // 是否使用 #source-map 开发工具,更多信息可以查看 DDFE 往期文章 devtool: config.build.productionSourceMap ? '#source-map' : false, output: { // 编译输出目录 path: config.build.assetsRoot, // 编译输出文件名 // 我们可以在 hash 后加 :6 决定使用几位 hash 值 filename: utils.assetsPath('js/[name].[chunkhash].js'), // 没有指定输出名的文件输出的文件名 chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') }, vue: { // 编译 .vue 文件时使用的 loader loaders: utils.cssLoaders({ sourceMap: config.build.productionSourceMap, extract: true }) }, plugins: [ // 使用的插件 // // definePlugin 接收字符串插入到代码当中, 所以你需要的话可以写上 JS 的字符串 new webpack.DefinePlugin({ 'process.env': env }), // 压缩 js (同样可以压缩 css) new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }), new webpack.optimize.OccurrenceOrderPlugin(), // extract css into its own file // 将 css 文件分离出来 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 // 输入输出的 .html 文件 new HtmlWebpackPlugin({ filename: config.build.index, template: 'index.html', // 是否注入 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'] }) ] }) // 开启 gzip 的情况下使用下方的配置 if (config.build.productionGzip) { // 加载 compression-webpack-plugin 插件 var CompressionWebpackPlugin = require('compression-webpack-plugin') // 向webpackconfig.plugins中加入下方的插件 var reProductionGzipExtensions = '\\.(' + config.build.productionGzipExtensions.join('|') + '$)' webpackConfig.plugins.push( // 使用 compression-webpack-plugin 插件进行压缩 new CompressionWebpackPlugin({ asset: '[path].gz[query]', algorithm: 'gzip', test: new RegExp(reProductionGzipExtensions), // 注:此处因有代码格式化的bug,与源码有差异 threshold: 10240, minRatio: 0.8 }) ) } module.exports = webpackConfig

总结

vue2.0脚手架的webpack 配置文件分析借此回顾下,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wysywj.html