|
| 1 | +import path from 'path' |
| 2 | +import { build, mergeConfig, type InlineConfig } from 'vite' |
| 3 | +import baseVitePreprocessor from './vitePreprocessor' |
| 4 | +import { debug, getConfig } from './common' |
| 5 | + |
| 6 | +let wasPrebuilt = true |
| 7 | + |
| 8 | +/** |
| 9 | + * Pre-process all files at the beginning of the test run, before they are ran through the |
| 10 | + * preprocessor for each spec. |
| 11 | + * |
| 12 | + * Can greatly improve overall test time, since this pre-build takes into account the shared |
| 13 | + * assets/imports of all spec files being ran (as opposed to the regular preprocessor alone, which |
| 14 | + * performs on each spec file individually). |
| 15 | + * |
| 16 | + * Does not work in watch mode, since watch mode preprocesses the current watched spec rather than the |
| 17 | + * "whole" of them. (TODO: look into a way to do this) |
| 18 | + * |
| 19 | + * @param {InlineConfig | string} config - Vite config object, or path to user |
| 20 | + * Vite config file for backwards compatibility |
| 21 | + * |
| 22 | + * @example |
| 23 | + * import { getVitePrebuilder } from 'cypress-vite' |
| 24 | + * ... |
| 25 | + * const { vitePrebuild, vitePreprocessor } = getVitePrebuilder(path.resolve(__dirname, './vite.config.ts')) |
| 26 | + * ... |
| 27 | + * setupNodeEvents(on, config) { |
| 28 | + * on('before:run', (details) => vitePrebuild(details, config)) |
| 29 | + * on('file:preprocessor', vitePreprocessor) |
| 30 | + * }, |
| 31 | + */ |
| 32 | +export function getVitePrebuilder(userConfig?: InlineConfig | string) { |
| 33 | + const viteConfig: InlineConfig = getConfig(userConfig) |
| 34 | + |
| 35 | + const OUT_DIR = 'node_modules/.cypress-vite-prebuild' |
| 36 | + |
| 37 | + /** |
| 38 | + * Prebuild with all spec files as entries. |
| 39 | + * Does not pre-build if only 1 spec ran, or if `watchForFileChanges=true`. |
| 40 | + * |
| 41 | + * @param details |
| 42 | + * Cypress details |
| 43 | + * @param config |
| 44 | + * Cypress config |
| 45 | + */ |
| 46 | + async function maybeVitePrebuild( |
| 47 | + details: Cypress.BeforeRunDetails, |
| 48 | + config: Cypress.PluginConfigOptions, |
| 49 | + ) { |
| 50 | + // TODO: there may be a way to get it to work for watch mode... Could we watch the file while keeping imported assets? |
| 51 | + if ( |
| 52 | + config.watchForFileChanges || |
| 53 | + !details.specs || |
| 54 | + details.specs.length < 2 |
| 55 | + ) { |
| 56 | + // We don't gain anything from pre-preprocessing if there isn't more than 1 spec being ran. |
| 57 | + wasPrebuilt = false |
| 58 | + debug('Not pre-building with Vite.') |
| 59 | + return |
| 60 | + } |
| 61 | + const files: string[] = details.specs.map((spec) => spec.absolute) |
| 62 | + if (config.supportFile) { |
| 63 | + files.push(config.supportFile) |
| 64 | + } |
| 65 | + |
| 66 | + debug(`Pre-building ${files.length} files with Vite.`) |
| 67 | + |
| 68 | + await build( |
| 69 | + mergeConfig(viteConfig, { |
| 70 | + build: { |
| 71 | + outDir: OUT_DIR, |
| 72 | + emptyOutDir: true, |
| 73 | + minify: false, |
| 74 | + rollupOptions: { |
| 75 | + input: files, |
| 76 | + output: { entryFileNames: '[name].ts', format: 'es' }, |
| 77 | + treeshake: true, |
| 78 | + }, |
| 79 | + }, |
| 80 | + esbuild: { treeShaking: true }, |
| 81 | + } satisfies InlineConfig), |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + function customVitePreprocessor(file: Cypress.FileObject) { |
| 86 | + if (wasPrebuilt && !file.shouldWatch) { |
| 87 | + file.filePath = path.join(OUT_DIR, path.basename(file.filePath)) |
| 88 | + } |
| 89 | + |
| 90 | + // TODO: make so initial preprocess works, don't have to re-preprocess here |
| 91 | + return baseVitePreprocessor(viteConfig)(file) |
| 92 | + } |
| 93 | + |
| 94 | + return { |
| 95 | + vitePrebuild: maybeVitePrebuild, |
| 96 | + vitePreprocessor: customVitePreprocessor, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export default getVitePrebuilder |
0 commit comments