Skip to content
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const maybeMap = <I, O>(
value: I | I[],
modifier: (item: I) => O,
): O | O[] => (Array.isArray(value) ? value.map(modifier) : modifier(value))

export const omit = <T extends object, K extends keyof T>(
obj: T,
...keys: K[]
): Omit<T, K> => {
const ret = { ...obj }
keys.forEach((key) => delete ret[key])
return ret
}
28 changes: 14 additions & 14 deletions src/vitePrebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,21 @@ export function getVitePrebuilder(userConfig?: InlineConfig | string) {

debug(`Pre-building ${files.length} files with Vite.`)

await build(
mergeConfig(viteConfig, {
build: {
outDir: OUT_DIR,
emptyOutDir: true,
minify: false,
rollupOptions: {
input: files,
output: { entryFileNames: '[name].ts', format: 'es' },
treeshake: true,
},
const resolvedConfig: InlineConfig = mergeConfig(viteConfig, {
build: {
outDir: OUT_DIR,
emptyOutDir: true,
minify: false,
rollupOptions: {
input: files,
output: { entryFileNames: '[name].ts', format: 'es' },
treeshake: true,
},
esbuild: { treeShaking: true },
} satisfies InlineConfig),
)
},
esbuild: { treeShaking: true },
} satisfies InlineConfig)

await build(resolvedConfig)
}

function customVitePreprocessor(file: Cypress.FileObject) {
Expand Down
43 changes: 33 additions & 10 deletions src/vitePreprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from 'path'
import { build, type InlineConfig } from 'vite'
import { build, type BuildEnvironmentOptions, type InlineConfig } from 'vite'
import chokidar from 'chokidar'
import { debug, getConfig, type CypressPreprocessor } from './common'
import { maybeMap, omit } from './utils'

const watchers: Record<string, chokidar.FSWatcher> = {}

Expand Down Expand Up @@ -78,20 +79,42 @@ function vitePreprocessor(
formats: ['umd'],
name: filenameWithoutExtension,
},
rollupOptions: {
output: {
// override any manualChunks from the user config because they don't work with UMD
// eslint-disable-next-line @typescript-eslint/no-explicit-any
manualChunks: false as any,
},
},
},
}

await build({
const resolvedConfig: InlineConfig & {
build?: InlineConfig['build'] & {
// `rolldownOptions` is used instead of `rollupOptions` in Vite 8.
// Just copy the type from `rollupOptions`, so we don't have to
// maintain another type or install another package.
rolldownOptions?: BuildEnvironmentOptions['rollupOptions']
}
} = {
...config,
...defaultConfig,
})
}

// Remove any manualChunks or advancedChunks.
for (const key of ['rollupOptions', 'rolldownOptions'] as const) {
if (resolvedConfig.build?.[key]?.output) {
resolvedConfig.build[key].output = maybeMap(
resolvedConfig.build[key].output,
(o) =>
omit(
o as typeof o & {
manualChunks?: unknown
advancedChunks?: unknown
codeSplitting?: unknown
},
'manualChunks',
'advancedChunks',
'codeSplitting',
),
)
}
}

await build(resolvedConfig)

return outputPath
}
Expand Down
Loading