import * as path from 'path' import * as fs from 'fs' import { parseEnv, parseArgv } from '../../utils/parse' const pluginRE = /^(@vue\/|vue-|@[\w-]+(\.)?[\w-]+\/vue-)cli-plugin-/ const isVuePlugin = (id: string) => pluginRE.test(id) const { VUE_DEV_LINT_ON_SAVE } = parseEnv() export interface IPackage { dependencies: { [packageName: string]: string } devDependencies: { [packageName: string]: string } } const DEFAULT_CONFIG_PACKAGE_PATH = path.resolve(__dirname, '../../../package.json') export const injectVuePluginOptions = () => { const services = (process as any)['VUE_CLI_SERVICE'] const pkg = services.pkg const { command } = parseArgv() // 加载vue-default-config携带的 package.json const def = JSON.parse(fs.readFileSync(DEFAULT_CONFIG_PACKAGE_PATH, { encoding: 'utf-8' })) // 过滤 vue-plugin const { dependencies, devDependencies } = def const dep = { ...devDependencies, ...dependencies } for (const pluginKey of Object.keys(dep).filter(isVuePlugin)) { // 添加到 pkg 中 const pluginVersion = dep[pluginKey] if (pkg.dependencies[pluginKey] || pkg.devDependencies[pluginKey]) continue if (command === 'serve' && pluginKey === '@vue/cli-plugin-eslint' && VUE_DEV_LINT_ON_SAVE !== 1) { continue } pkg.devDependencies[pluginKey] = pluginVersion } services.plugins = services.resolvePlugins() }