From 2c19b57bac2c0dafe4772708f2322d69970798c2 Mon Sep 17 00:00:00 2001 From: Damian Stasik <920747+damianstasik@users.noreply.github.com> Date: Mon, 1 Jan 2024 15:02:05 +0100 Subject: [PATCH] refactor: reuse existing node utils --- packages/vite/src/node/build.ts | 11 ++++------- packages/vite/src/node/optimizer/index.ts | 5 +++-- packages/vite/src/node/plugins/splitVendorChunk.ts | 4 ++-- packages/vite/src/node/utils.ts | 2 +- packages/vite/src/node/watch.ts | 3 ++- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 9525809419114d..aa71f7b4bc1312 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -27,6 +27,7 @@ import { buildReporterPlugin } from './plugins/reporter' import { buildEsbuildPlugin } from './plugins/esbuild' import { type TerserOptions, terserPlugin } from './plugins/terser' import { + arraify, asyncFlatten, copyDir, emptyDir, @@ -427,13 +428,9 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{ completeSystemWrapPlugin(), ...(usePluginCommonjs ? [commonjsPlugin(options.commonjsOptions)] : []), dataURIPlugin(), - ...(( - await asyncFlatten( - Array.isArray(rollupOptionsPlugins) - ? rollupOptionsPlugins - : [rollupOptionsPlugins], - ) - ).filter(Boolean) as Plugin[]), + ...((await asyncFlatten(arraify(rollupOptionsPlugins))).filter( + Boolean, + ) as Plugin[]), ...(config.isWorker ? [webWorkerPostPlugin()] : []), ], post: [ diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 5a98810777a314..680adc880cfd91 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -23,6 +23,7 @@ import { normalizePath, removeLeadingSlash, tryStatSync, + unique, } from '../utils' import { transformWithEsbuild } from '../plugins/esbuild' import { ESBUILD_MODULES_TARGET } from '../constants' @@ -1226,10 +1227,10 @@ function getConfigHash(config: ResolvedConfig, ssr: boolean): string { plugins: config.plugins.map((p) => p.name), optimizeDeps: { include: optimizeDeps?.include - ? Array.from(new Set(optimizeDeps.include)).sort() + ? unique(optimizeDeps.include).sort() : undefined, exclude: optimizeDeps?.exclude - ? Array.from(new Set(optimizeDeps.exclude)).sort() + ? unique(optimizeDeps.exclude).sort() : undefined, esbuildOptions: { ...optimizeDeps?.esbuildOptions, diff --git a/packages/vite/src/node/plugins/splitVendorChunk.ts b/packages/vite/src/node/plugins/splitVendorChunk.ts index 034b8c2ee5231e..b2ecbcb6f684dc 100644 --- a/packages/vite/src/node/plugins/splitVendorChunk.ts +++ b/packages/vite/src/node/plugins/splitVendorChunk.ts @@ -4,7 +4,7 @@ import type { ManualChunkMeta, OutputOptions, } from 'rollup' -import { isInNodeModules } from '../utils' +import { arraify, isInNodeModules } from '../utils' import type { UserConfig } from '../../node' import type { Plugin } from '../plugin' @@ -103,7 +103,7 @@ export function splitVendorChunkPlugin(): Plugin { config(config) { let outputs = config?.build?.rollupOptions?.output if (outputs) { - outputs = Array.isArray(outputs) ? outputs : [outputs] + outputs = arraify(outputs) for (const output of outputs) { const viteManualChunks = createSplitVendorChunk(output, config) if (viteManualChunks) { diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 01eb7165c9f5b0..1da760262873ae 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1070,7 +1070,7 @@ export const requireResolveFromRootWithFallback = ( } export function emptyCssComments(raw: string): string { - return raw.replace(multilineCommentsRE, (s) => ' '.repeat(s.length)) + return raw.replace(multilineCommentsRE, (s) => blankReplacer(s)) } function backwardCompatibleWorkerPlugins(plugins: any) { diff --git a/packages/vite/src/node/watch.ts b/packages/vite/src/node/watch.ts index 3b40a71c84d3fc..c5dc879ac56a48 100644 --- a/packages/vite/src/node/watch.ts +++ b/packages/vite/src/node/watch.ts @@ -1,6 +1,7 @@ import { EventEmitter } from 'node:events' import glob from 'fast-glob' import type { FSWatcher, WatchOptions } from 'dep-types/chokidar' +import { arraify } from './utils' import type { ResolvedConfig } from '.' export function resolveChokidarOptions( @@ -15,7 +16,7 @@ export function resolveChokidarOptions( '**/node_modules/**', '**/test-results/**', // Playwright glob.escapePath(config.cacheDir) + '/**', - ...(Array.isArray(ignored) ? ignored : [ignored]), + ...arraify(ignored), ], ignoreInitial: true, ignorePermissionErrors: true,