|
16 | 16 | * @type {Cypress.PluginConfig} |
17 | 17 | */ |
18 | 18 | const webpack = require('@cypress/webpack-preprocessor'); |
| 19 | +const fs = require('fs'); |
| 20 | +const path = require('path'); |
19 | 21 |
|
20 | 22 | module.exports = (on, config) => { |
21 | 23 | config.env = config.env || {}; |
22 | 24 | config.env.BUILD_ENV = 'production'; |
23 | | - |
| 25 | + // Resolve path to the temporary bundle test fixture |
| 26 | + const bundleFixturePath = path.resolve((config.projectRoot || process.cwd()), 'cypress/fixtures/bundle-test.html'); |
| 27 | + |
| 28 | + // Expose cleanup tasks to the Cypress runner |
| 29 | + on('task', { |
| 30 | + deleteFile(filePath) { |
| 31 | + try { |
| 32 | + if (fs.existsSync(filePath)) { |
| 33 | + fs.unlinkSync(filePath); |
| 34 | + } |
| 35 | + return true; |
| 36 | + } catch (err) { |
| 37 | + // ENOENT is fine; any other error bubble up for visibility |
| 38 | + if (err && err.code === 'ENOENT') return true; |
| 39 | + throw err; |
| 40 | + } |
| 41 | + }, |
| 42 | + cleanupBundleFixture() { |
| 43 | + try { |
| 44 | + if (fs.existsSync(bundleFixturePath)) { |
| 45 | + fs.unlinkSync(bundleFixturePath); |
| 46 | + } |
| 47 | + } catch (_) { |
| 48 | + // Ignore errors during cleanup |
| 49 | + } |
| 50 | + return true; |
| 51 | + }, |
| 52 | + fileExists(filePath) { |
| 53 | + return fs.existsSync(filePath); |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + // Best-effort cleanup on process exit/interrupt, guarded to avoid duplicate handlers |
| 58 | + if (!global.__bundleCleanupHandlersRegistered) { |
| 59 | + const safeUnlink = () => { |
| 60 | + try { |
| 61 | + if (fs.existsSync(bundleFixturePath)) { |
| 62 | + fs.unlinkSync(bundleFixturePath); |
| 63 | + } |
| 64 | + } catch (_) {} |
| 65 | + }; |
| 66 | + process.on('exit', safeUnlink); |
| 67 | + process.on('SIGINT', () => { safeUnlink(); process.exit(1); }); |
| 68 | + process.on('SIGTERM', () => { safeUnlink(); process.exit(1);}); |
| 69 | + global.__bundleCleanupHandlersRegistered = true; |
| 70 | + } |
| 71 | + |
24 | 72 | if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') { |
25 | 73 | const webpackOptions = { |
26 | 74 | webpackOptions: require('../../configs/webpack.config'), |
27 | 75 | watchOptions: {}, |
28 | 76 | }; |
29 | 77 | on('file:preprocessor', webpack(webpackOptions)); |
30 | 78 | } |
31 | | - |
| 79 | + |
32 | 80 | // on('file:preprocessor', require('@cypress/code-coverage/use-babelrc')); |
33 | 81 | return config; |
34 | 82 | }; |
0 commit comments