-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
139 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// copied from https://github.com/mammadataei/cypress-vite | ||
|
||
import * as path from 'path'; | ||
import { build, InlineConfig } from 'vite'; | ||
import type { RollupOutput, RollupWatcher, WatcherOptions } from 'rollup'; | ||
|
||
type CypressPreprocessor = (file: any) => string | Promise<string>; | ||
|
||
/** | ||
* Cypress preprocessor for running e2e tests using vite. | ||
* | ||
* @param {string} userConfigPath | ||
* @example | ||
* setupNodeEvents(on) { | ||
* on( | ||
* 'file:preprocessor', | ||
* vitePreprocessor(path.resolve(__dirname, './vite.config.ts')), | ||
* ) | ||
* }, | ||
*/ | ||
function vitePreprocessor(userConfigPath?: string): CypressPreprocessor { | ||
return async (file) => { | ||
const { outputPath, filePath, shouldWatch } = file; | ||
|
||
const fileName = path.basename(outputPath); | ||
const filenameWithoutExtension = path.basename( | ||
outputPath, | ||
path.extname(outputPath) | ||
); | ||
|
||
const defaultConfig: InlineConfig = { | ||
logLevel: 'silent', | ||
define: { | ||
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV), | ||
}, | ||
build: { | ||
emptyOutDir: false, | ||
minify: false, | ||
outDir: path.dirname(outputPath), | ||
sourcemap: true, | ||
write: true, | ||
watch: getWatcherConfig(shouldWatch), | ||
lib: { | ||
entry: filePath, | ||
fileName: () => fileName, | ||
formats: ['umd'], | ||
name: filenameWithoutExtension, | ||
}, | ||
}, | ||
}; | ||
|
||
const watcher = await build({ | ||
configFile: userConfigPath, | ||
...defaultConfig, | ||
}); | ||
|
||
if (shouldWatch && isWatcher(watcher)) { | ||
watcher.on('event', (event) => { | ||
if (event.code === 'END') { | ||
file.emit('rerun'); | ||
} | ||
|
||
if (event.code === 'ERROR') { | ||
console.error(event); | ||
} | ||
}); | ||
|
||
file.on('close', () => { | ||
watcher.close(); | ||
}); | ||
} | ||
|
||
return outputPath; | ||
}; | ||
} | ||
|
||
function getWatcherConfig(shouldWatch: boolean): WatcherOptions | null { | ||
return shouldWatch ? {} : null; | ||
} | ||
|
||
type BuildResult = RollupWatcher | RollupOutput | RollupOutput[]; | ||
|
||
function isWatcher(watcher: BuildResult): watcher is RollupWatcher { | ||
return (watcher as RollupWatcher).on !== undefined; | ||
} | ||
|
||
export default vitePreprocessor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters