Skip to content

Commit

Permalink
feat: add support for debugging
Browse files Browse the repository at this point in the history
feat: add support for debugging
  • Loading branch information
mammadataei committed Sep 24, 2022
2 parents b2b49d4 + 9e7cd02 commit f8f1678
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ export default defineConfig({
})
```

## Debugging

Run your tests with the following environment variable to log the debugging
output:

```bash
DEBUG=cypress-vite
```

## Credits

Thanks to
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"devDependencies": {
"@commitlint/cli": "^17.1.2",
"@commitlint/config-conventional": "^17.1.0",
"@types/debug": "^4.1.7",
"@types/node": "^18.7.18",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
Expand All @@ -62,5 +63,8 @@
"tsup": "^6.2.3",
"typescript": "^4.8.3",
"vite": "^3.1.3"
},
"dependencies": {
"debug": "^4.3.4"
}
}
17 changes: 15 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import path from 'path'
import Debug from 'debug'
import { build, InlineConfig } from 'vite'
import type { RollupOutput, RollupWatcher, WatcherOptions } from 'rollup'
import { getConfig, resolveConfig } from './resolveConfig'

type FileObject = Cypress.FileObject
type CypressPreprocessor = (file: FileObject) => string | Promise<string>

const debug = Debug('cypress-vite')
const cache: Record<string, string> = {}

/**
Expand All @@ -21,14 +23,20 @@ const cache: Record<string, string> = {}
* },
*/
function vitePreprocessor(userConfigPath?: string): CypressPreprocessor {
debug('User config path: %s', userConfigPath)

if (userConfigPath) {
resolveConfig(userConfigPath)
resolveConfig(userConfigPath).then((config) => {
debug('Resolved user config:', config)
})
}

return async (file) => {
const { outputPath, filePath, shouldWatch } = file
debug('Preprocessing file %s', filePath)

if (cache[filePath]) {
debug('Cached bundle exist for file %s', filePath)
return cache[filePath]
}

Expand Down Expand Up @@ -59,22 +67,35 @@ function vitePreprocessor(userConfigPath?: string): CypressPreprocessor {
},
}

const watcher = await build(getConfig(defaultConfig))
const buildConfig = getConfig(defaultConfig)
debug('Build config for file %s:', filePath, buildConfig)

const watcher = await build(buildConfig)

if (shouldWatch && isWatcher(watcher)) {
watcher.on('event', (event) => {
debug('Watcher %s for file %s', event.code, filePath)

if (event.code === 'END') {
file.emit('rerun')
}

if (event.code === 'ERROR') {
console.error(event)
}
})

file.on('close', () => {
delete cache[filePath]
watcher.close()

debug('File %s closed.', filePath)
})
}

cache[filePath] = outputPath
debug('Bundle for file %s cached at %s', filePath, outputPath)

return outputPath
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ let resolvedUserConfig: UserConfig | undefined = undefined
const configEnv: ConfigEnv = { command: 'build', mode: 'development' }

export function resolveConfig(userConfigPath: string) {
loadConfigFromFile(configEnv, userConfigPath).then((result) => {
return loadConfigFromFile(configEnv, userConfigPath).then((result) => {
resolvedUserConfig = result?.config
return resolvedUserConfig
})
}

Expand Down

0 comments on commit f8f1678

Please sign in to comment.