|
| 1 | +import { describe, expect, beforeAll, test } from 'vitest'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import * as fs from 'node:fs'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | + |
| 6 | +import webpack from 'webpack'; |
| 7 | +import { rollup } from 'rollup'; |
| 8 | +import { build as viteBuild } from 'vite'; |
| 9 | +import nodeResolve from '@rollup/plugin-node-resolve'; |
| 10 | + |
| 11 | +// Helper functions |
| 12 | +const __filename = fileURLToPath(import.meta.url); |
| 13 | +const __dirname = path.dirname(__filename); |
| 14 | + |
| 15 | +function distDir(name: string): string { |
| 16 | + const dir = path.join(__dirname, '..', 'dist', name); |
| 17 | + if (!fs.existsSync(dir)) { |
| 18 | + fs.mkdirSync(dir, { recursive: true }); |
| 19 | + } |
| 20 | + return dir; |
| 21 | +} |
| 22 | + |
| 23 | +function rimraf(dir: string): void { |
| 24 | + if (fs.existsSync(dir)) { |
| 25 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function readAllJs(outDir: string): string { |
| 30 | + let contents = ''; |
| 31 | + const stack = [outDir]; |
| 32 | + while (stack.length) { |
| 33 | + const current = stack.pop()!; |
| 34 | + for (const entry of fs.readdirSync(current)) { |
| 35 | + const full = path.join(current, entry); |
| 36 | + const stat = fs.statSync(full); |
| 37 | + if (stat.isDirectory()) { |
| 38 | + stack.push(full); |
| 39 | + } else if (entry.endsWith('.js') || entry.endsWith('.mjs')) { |
| 40 | + contents += fs.readFileSync(full, 'utf8'); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + return contents; |
| 45 | +} |
| 46 | + |
| 47 | +function fixtureEntry(name: string): string { |
| 48 | + return path.resolve(__dirname, '..', 'fixtures', name, 'index.js'); |
| 49 | +} |
| 50 | + |
| 51 | +function rootDir(): string { |
| 52 | + return path.join(__dirname, '../../..'); |
| 53 | +} |
| 54 | + |
| 55 | +const SPOTLIGHT_URL = 'localhost:8969'; |
| 56 | + |
| 57 | +type BundleMode = 'development' | 'production'; |
| 58 | + |
| 59 | +function bundleWithWebpack(mode: BundleMode): Promise<string> { |
| 60 | + return new Promise((resolve, reject) => { |
| 61 | + const outDir = distDir(`webpack-${mode}`); |
| 62 | + rimraf(outDir); |
| 63 | + const compiler = webpack({ |
| 64 | + mode, |
| 65 | + entry: fixtureEntry('basic'), |
| 66 | + output: { path: outDir, filename: 'bundle.js' }, |
| 67 | + }); |
| 68 | + compiler?.run((err: Error | null | undefined, stats: webpack.Stats | undefined) => { |
| 69 | + try { |
| 70 | + if (err) throw err; |
| 71 | + if (stats?.hasErrors()) { |
| 72 | + throw new Error(stats.toString('errors-only')); |
| 73 | + } |
| 74 | + resolve(readAllJs(outDir)); |
| 75 | + } catch (e) { |
| 76 | + reject(e); |
| 77 | + } finally { |
| 78 | + compiler.close(() => {}); |
| 79 | + } |
| 80 | + }); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +async function bundleWithRollup(mode: BundleMode): Promise<string> { |
| 85 | + const outDir = distDir(`rollup-${mode}`); |
| 86 | + rimraf(outDir); |
| 87 | + |
| 88 | + const bundle = await rollup({ |
| 89 | + input: fixtureEntry('basic'), |
| 90 | + plugins: [ |
| 91 | + nodeResolve({ |
| 92 | + // There should really be a default where these get specified automatically |
| 93 | + exportConditions: [mode === 'production' ? 'production' : 'development'], |
| 94 | + }), |
| 95 | + ], |
| 96 | + }); |
| 97 | + await bundle.write({ dir: outDir, format: 'esm' }); |
| 98 | + await bundle.close(); |
| 99 | + return readAllJs(outDir); |
| 100 | +} |
| 101 | + |
| 102 | +async function bundleWithVite(mode: BundleMode): Promise<string> { |
| 103 | + const outDir = distDir(`vite-${mode}`); |
| 104 | + rimraf(outDir); |
| 105 | + |
| 106 | + // In Vitest, NODE_ENV is always 'test', so we need to override it here |
| 107 | + const prev = process.env.NODE_ENV; |
| 108 | + process.env.NODE_ENV = mode; |
| 109 | + |
| 110 | + await viteBuild({ |
| 111 | + mode, |
| 112 | + root: path.dirname(fixtureEntry('basic')), |
| 113 | + build: { outDir, minify: mode === 'production' }, |
| 114 | + }); |
| 115 | + |
| 116 | + process.env.NODE_ENV = prev; |
| 117 | + |
| 118 | + return readAllJs(outDir); |
| 119 | +} |
| 120 | + |
| 121 | +describe('spotlight', () => { |
| 122 | + beforeAll(() => { |
| 123 | + const distRoot = path.join(rootDir(), 'dist'); |
| 124 | + rimraf(distRoot); |
| 125 | + }); |
| 126 | + |
| 127 | + const cases: [string, (mode: BundleMode) => Promise<string>][] = [ |
| 128 | + ['webpack', bundleWithWebpack], |
| 129 | + ['rollup', bundleWithRollup], |
| 130 | + ['vite', bundleWithVite], |
| 131 | + ]; |
| 132 | + |
| 133 | + for (const [name, bundler] of cases) { |
| 134 | + test(`${name} development bundle contains spotlight`, async () => { |
| 135 | + const code = await bundler('development'); |
| 136 | + expect(code).toContain(SPOTLIGHT_URL); |
| 137 | + }); |
| 138 | + |
| 139 | + test(`${name} production bundle does not contain spotlight`, async () => { |
| 140 | + const code = await bundler('production'); |
| 141 | + expect(code).not.toContain(SPOTLIGHT_URL); |
| 142 | + }); |
| 143 | + } |
| 144 | +}); |
0 commit comments