Skip to content

Commit

Permalink
refactor: reuse existing node utils
Browse files Browse the repository at this point in the history
  • Loading branch information
damianstasik committed Jan 1, 2024
1 parent 2a540ee commit 2c19b57
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
11 changes: 4 additions & 7 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: [
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
normalizePath,
removeLeadingSlash,
tryStatSync,
unique,
} from '../utils'
import { transformWithEsbuild } from '../plugins/esbuild'
import { ESBUILD_MODULES_TARGET } from '../constants'
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/splitVendorChunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/watch.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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,
Expand Down

0 comments on commit 2c19b57

Please sign in to comment.