import * as path from 'path' import ChainableWebpackConfig from 'webpack-chain' import SentryWebpackPlugin from '@ysfe/sentry-webpack-plugin' import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer' import { IgnorePlugin } from 'webpack' import { injectPerformanceConfig } from './performance/warning' import { injectRsolveAlias } from './alias' import { injectStlyusOptimization } from './stylus' import { injectSvgModule } from './svg' import { parseArgv, parseEnv } from '../utils/parse' import { readSimpleProerties } from '../utils/read-proerties' import { smp } from './performance/smp' import { spriteChunks } from './chunks' import PolyfillWebpackPlugin from '@ysfe/polyfill-webpack-plugin' import moment from 'moment' // # webpack 打包性能分析插件 // # webpack 依赖分析 /** 创建 webpack 配置 */ export const createWebpackConfigure = (config: ChainableWebpackConfig): void => { // @ 环境变量 const { VUE_ENABLE_SENTRY } = parseEnv() // @ 命令行参数 const { command, analyzer, disableSentry, tag } = parseArgv().alias('disable-sentry', 'disableSentry') // > 关闭prefetch config.plugins.delete('prefetch').end() // > 关闭 preload, 这个坚决不开 config.plugins.delete('preload').end() // > 注入自定义环境变量 config.plugin('define').tap((arg: Array) => { // 注入当前运行命令 arg[0]['process.env'].command = `'${command}'` return arg }) // > 关闭 mini-css-extract-css 失败提示 // config.plugins.get('extract-css').get('args')[0].ignoreOrder = true // > 注入自定义资源(import)路径 injectRsolveAlias(config) // > 注入 svg icon 配置 injectSvgModule(config) // > 注入样式优化选项. (全局变量注入、postcss plugin、less plugin) injectStlyusOptimization(config) // > chunk spriteChunks(config) // > 添加 polyfill 插件 config.plugin('polyfill').use(PolyfillWebpackPlugin) // > moment 去除语言包 config.plugin('ignore').use(IgnorePlugin, [ { checkResource: (resource, context) => { return context.includes('moment/locale') } } ]) // > performance monitor. max: entry - 512kb, other - 1mb injectPerformanceConfig(config) // # analyse 性能分析所需的webpack配置导出. // 分析插件地址: https://webpack.github.io/analyse/ config.profile(analyzer as any as boolean) // ? 启用性能评测 if (analyzer) { // # 捆绑统计 config.plugin('bundle-analyzer').use(BundleAnalyzerPlugin) // # 插件耗时分析 | loader 耗时分析 smp(config) } else { // ? 根据senry配置及环境变量启用sentry if (VUE_ENABLE_SENTRY === 1 && !disableSentry) { const sentryclirc: { [key: string]: any } = readSimpleProerties(path.join(process.cwd(), '.sentryclirc')) if (sentryclirc.dsn) { let release: string const defaultRelease: string = `${path.basename(process.cwd())}_${moment().format('MMDD_HHmmss')}` if (typeof tag === 'string' && tag.length > 0) { release = tag === '${tag}' ? defaultRelease : tag } else { release = defaultRelease } console.log('\nsentry release:', release) config.plugin('sentry').use(SentryWebpackPlugin, [{ release }]) } else { console.warn(`\n[warn] 缺少sentry必要配置, sentry sourcemap 上报未执行`) } } } }