vue.config.js 4.47 KB
const path = require('path')
const CompressionPlugin = require('compression-webpack-plugin')
const SpritesmithPlugin = require('webpack-spritesmith') // 雪碧图


const environment = process.env.VUE_APP_NODE_ENV // 定义变量 环境
const app_version = process.env.VUE_APP_VERSION // 定义变量 版本号

// cdn
const cdn = {
  // 开发环境
  dev: {
    css: [],
    js: []
  },
  // 生产环境
  build: {
    css: [],
    js: [
      // `https://lib.baomitu.com/vue/2.6.11/vue.min.js`,
      // `https://lib.baomitu.com/vue-router/3.1.3/vue-router.min.js`,
      // `https://lib.baomitu.com/vuex/3.1.2/vuex.min.js`
    ]
  }
}

// 雪碧图的自定义模板
const templateFunction = function (data) {
  var shared = '.icon-sprite { display: inline-block; background-image: url(I); background-size: Dpx Hpx; }'
    .replace('I', data.sprites[0].image)
    .replace('D', data.sprites[0].total_width)
    .replace('H', data.sprites[0].total_height)

  var perSprite = data.sprites.map(function (sprite) {
    return '.icon-N { width: Wpx; height: Hpx; background-position: Xpx Ypx; }'
      .replace('N', sprite.name.replace(/_/g, '-'))
      .replace('W', sprite.width)
      .replace('H', sprite.height)
      .replace('X', sprite.offset_x)
      .replace('Y', sprite.offset_y)
  }).join('\n')

  return shared + '\n' + perSprite
}

module.exports = {
  outputDir: "dist",
  assetsDir: "assets",
  publicPath: "./",
  devServer: {
    host: '0.0.0.0',
    port: "8080",
    https: false,
    hotOnly: false,
    overlay: {
      warning: false,
      errors: false
    },
    proxy: {
      '/api': {
        // 测试环境
        target: process.env.VUE_APP_API, // http://192.168.0.114:8000/
        changeOrigin: true, // 是否跨域
        pathRewrite: {
          '^/api': '' // 需要rewrite重写的,  //
        }
      }
    },
  },
  pages: {
    index: {
      entry: "./src/main.js",
      template: path.join(__dirname, "public/index.html"),
      filename: 'index.html',
      title: 'NAFMII-WEB',
      chunks: ['chunk-vendors', 'chunk-common', 'index'],
      // cdn: environment === 'development' ? cdn.dev : cdn.build
    }
  },
  productionSourceMap: false,
  css: {
    loaderOptions: {
      less: {
        javascriptEnabled: true
      },
    }
  },
  configureWebpack: config => {
    if (environment === "devproduction" || environment === 'test' || environment === "uat" || environment === "pro") {
      //开发环境开启压缩
      config.plugins.push(new CompressionPlugin({ // Gzip压缩配置
        test: /\.js$|\.html$|\.scss/, // 匹配文件名
        threshold: 0,
        deleteOriginalAssets: false // 是否删除原文件
      }))
    }

    return {
      externals: {
        //moment: "moment",
        //axios: "axios"
      },
      resolve: {
        modules: ["node_modules", "spritesmith-generated"],
        alias: {
          // 别名
          '@': path.resolve('src'),
        }
      },
      plugins: [
        new SpritesmithPlugin({
          src: {
            cwd: path.resolve(__dirname, './src/assets/icon'),
            glob: '*.png'
          },
          target: { // 输出雪碧图文件及样式文件,这个是打包后,自动生成的雪碧图和样式
            image: path.resolve(__dirname, './src/assets/images/sprite.png'),
            css: [
              [path.resolve(__dirname, './src/assets/css/sprite.scss'), {
                // 引用自己的模板
                format: 'function_based_template'
              }]
            ]
          },
          customTemplates: { // 自定义模板入口
            function_based_template: templateFunction
          },
          apiOptions: { // 样式文件中调用雪碧图地址写法
            cssImageRef: '../images/sprite.png'
          },
          spritesmithOptions: { // 让合成的每个图片有一定的距离
            padding: 20
          }
        })
      ]
    }
  },
  chainWebpack: config => {
    const hashName = `assets/js/[name].[hash:8].${environment}.${app_version}.js`
    config.output.filename(hashName).end()
    config.output.chunkFilename(hashName).end()

    // const filename = config.output.store.get('filename')
    // const chunkFilename = config.output.store.get('chunkFilename')
    // const newfilename = filename.replace('[name]', '[name].[hash:8]')
    // const newchunkFilename = chunkFilename.replace('[name]', '[name].[hash:8]')

    // config.output.filename((newfilename)).end()
    // config.output.chunkFilename(newchunkFilename).end()
  },
  lintOnSave: false
}