From 5e53c0b88cea83cd4f4f3420c2b4ac31e9cf55b5 Mon Sep 17 00:00:00 2001 From: YJZQV <2821740329@qq.com> Date: Mon, 5 Jul 2021 10:37:57 +0800 Subject: [PATCH] feat: add patches fix: user ignore the file suffix and uses the alias(@) to refer a file --- src/cli/cli.ts | 5 +++++ src/constants/constants.ts | 1 + src/generate/genePackageJson.ts | 6 ++++++ src/generate/genePatches.ts | 14 ++++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 src/generate/genePatches.ts diff --git a/src/cli/cli.ts b/src/cli/cli.ts index 01d4765..db05e13 100644 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -4,6 +4,7 @@ import chalk from 'chalk' import { geneIndexHtml } from '../generate/geneIndexHtml' import { genePackageJson } from '../generate/genePackageJson' import { geneViteConfig } from '../generate/geneViteConfig' +import { genePatches } from '../generate/genePatches' import { Command } from 'commander' import { Config } from '../config/config' import { astParseRoot } from '../ast-parse/astParse' @@ -50,6 +51,10 @@ export async function start (config : Config): Promise { // generate index.html must be after generate vite.config.js geneIndexHtml(rootDir, config) + // generate patches + const patchesDir = path.resolve(rootDir, 'patches') + genePatches(patchesDir) + console.log(chalk.green('************************ Done ! ************************')) const pkgManager = fs.existsSync(path.resolve(rootDir, 'yarn.lock')) ? 'yarn' diff --git a/src/constants/constants.ts b/src/constants/constants.ts index 09e89c3..4c7373a 100644 --- a/src/constants/constants.ts +++ b/src/constants/constants.ts @@ -8,3 +8,4 @@ export const VITE_PLUGIN_ENV_COMPATIBLE = '^1.0.0' export const SASS_VERSION = '^1.34.0' export const VITE_PLUGIN_COMMONJS_VERSION = '^1.0.0-beta3' export const POSTCSS_VERSION = '^8.3.5' +export const PATCH_PACKAGE_VERSION = '^6.4.7' diff --git a/src/generate/genePackageJson.ts b/src/generate/genePackageJson.ts index 4986b83..0616eb4 100644 --- a/src/generate/genePackageJson.ts +++ b/src/generate/genePackageJson.ts @@ -43,9 +43,15 @@ export function genePackageJson (packageJsonPath: string): void { packageJson.dependencies.postcss = constants.POSTCSS_VERSION } + // patch-package support + packageJson.devDependencies['patch-package'] = constants.PATCH_PACKAGE_VERSION + // add vite dev script packageJson.scripts['serve-vite'] = 'vite' packageJson.scripts['build-vite'] = 'vite build' + // add postinatall + packageJson.scripts.postinstall = 'patch-package' + writeSync(packageJsonPath, JSON.stringify(packageJson, null, 2)) } diff --git a/src/generate/genePatches.ts b/src/generate/genePatches.ts new file mode 100644 index 0000000..2d7284e --- /dev/null +++ b/src/generate/genePatches.ts @@ -0,0 +1,14 @@ +import fs from 'fs' +import path from 'path' +import { readSync, writeSync } from '../utils/file' + +export function genePatches (patchesDir: string): void { + // read all patches name + const patchesInfo = fs.readdirSync('patches'); + // inject patch + patchesInfo.forEach(patch => { + const filePath = path.resolve(patchesDir, patch); + const patchContent = readSync(path.resolve('patches', patch)); + writeSync(filePath, patchContent); + }); +}