打包文件

vue 结合webpack

Webpack

In your entry file:

这里是webpack.base.conf.js webpack的设置

  1. const createLintingRule = () => ({
  2. test: /\.(js|vue)$/,
  3. loader: 'eslint-loader',
  4. enforce: 'pre',
  5. include: [resolve('src'), resolve('test')],
  6. options: {
  7. formatter: require('eslint-friendly-formatter'),
  8. emitWarning: !config.dev.showEslintErrorsInOverlay
  9. }
  10. })
  11. //这个是代码规范检查
  12. module.exports = {
  13. context: path.resolve(__dirname, '../'),
  14. entry: {
  15. app: ['babel-polyfill', './src/main.js']
  16. },
  17. output: {
  18. path: config.build.assetsRoot,
  19. filename: '[name].js',
  20. publicPath: process.env.NODE_ENV === 'production'
  21. ? config.build.assetsPublicPath
  22. : config.dev.assetsPublicPath
  23. },
  24. resolve: {
  25. extensions: ['.js', '.vue', '.json'],
  26. alias: {
  27. 'vue$': 'vue/dist/vue.esm.js',
  28. '@': resolve('src'),
  29. }
  30. },
  31. module: {
  32. rules: [
  33. ...(config.dev.useEslint ? [createLintingRule()] : []),
  34. {
  35. test: /\.vue$/,//解析vue 文件
  36. loader: 'vue-loader',
  37. options: vueLoaderConfig
  38. },
  39. {
  40. test: /\.js$/,//解析js 文件
  41. loader: 'babel-loader',
  42. include: [resolve('src'), resolve('test')]
  43. },
  44. {
  45. test: /\.svg$/,//解析svg文件并生成到对应的目录
  46. loader: 'svg-sprite-loader',
  47. include: [resolve('src/icons')]
  48. },
  49. {
  50. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  51. loader: 'url-loader',
  52. exclude: [resolve('src/icons')],
  53. options: {
  54. limit: 10000,
  55. name: utils.assetsPath('img/[name].[hash:7].[ext]')
  56. }
  57. },
  58. {
  59. test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
  60. loader: 'url-loader',//这些类型文件加载并hash化
  61. options: {
  62. limit: 10000,
  63. name: utils.assetsPath('media/[name].[hash:7].[ext]')
  64. }
  65. },
  66. {
  67. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  68. loader: 'url-loader',
  69. options: {
  70. limit: 10000,
  71. name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
  72. }
  73. }
  74. ]
  75. }

gulp打包

  1. // 创建版本号(年月日时分) 为后来的打包自动生成一个序列做准备
  2. (function () {
  3. var d = new Date();
  4. var yy = d.getFullYear().toString().slice(2);
  5. var MM = d.getMonth() + 1 >= 10 ? (d.getMonth() + 1) : '0' + (d.getMonth() + 1);
  6. var DD = d.getDate() >= 10 ? d.getDate() : '0' + d.getDate();
  7. var h = d.getHours() >= 10 ? d.getHours() : '0' + d.getHours();
  8. var mm = d.getMinutes() >= 10 ? d.getMinutes() : '0' + d.getMinutes();
  9. version = yy + MM + DD + h + mm;
  10. versionPath = distPath + '/' + version;
  11. })();
  12. // 编译
  13. gulp.task('build', $.shell.task([ 'node build/build.js' ]));
  14. // 创建版本号目录
  15. gulp.task('create:versionCatalog', ['build'], function () {
  16. return gulp.src(`${distPath}/static/**/*`)
  17. .pipe(gulp.dest(`${versionPath}/static/`))
  18. });
  19. // 替换${versionPath}/static/js/manifest.js window.SITE_CONFIG.cdnUrl占位变量
  20. gulp.task('replace:cdnUrl', ['create:versionCatalog'], function () {
  21. return gulp.src(`${versionPath}/static/js/manifest.js`)
  22. .pipe($.replace(new RegExp(`"${require('./config').build.assetsPublicPath}"`, 'g'), 'window.SITE_CONFIG.cdnUrl + "/"'))
  23. .pipe(gulp.dest(`${versionPath}/static/js/`))
  24. });
  25. // 替换${versionPath}/static/config/index-${env}.js window.SITE_CONFIG['version']配置变量
  26. gulp.task('replace:version', ['create:versionCatalog'], function () {
  27. return gulp.src(`${versionPath}/static/config/index-${env}.js`)
  28. .pipe($.replace(/window.SITE_CONFIG\['version'\] = '.*'/g, `window.SITE_CONFIG['version'] = '${version}'`))
  29. .pipe(gulp.dest(`${versionPath}/static/config/`))
  30. });
  31. // 合并${versionPath}/static/config/[index-${env}, init].js 至 ${distPath}/config/index.js
  32. gulp.task('concat:config', ['replace:version'], function () {
  33. return gulp.src([`${versionPath}/static/config/index-${env}.js`, `${versionPath}/static/config/init.js`])
  34. .pipe($.concat('index.js'))
  35. .pipe(gulp.dest(`${distPath}/config/`))
  36. });
  37. gulp.task('default',function () {
  38. // 获取环境配置
  39. del([distPath]);
  40. env = process.env.npm_config_qa ? 'qa' : process.env.npm_config_uat ? 'uat' : 'prod'
  41. // 开始打包编译
  42. gulp.start(['build', 'create:versionCatalog', 'replace:cdnUrl', 'replace:version', 'concat:config'], function () {
  43. // 清除, 编译 / 处理项目中产生的文件
  44. del([`${distPath}/static`, `${versionPath}/static/config`])
  45. })
  46. });

以后慢慢解释