|
| 1 | +import { existsSync, mkdtempSync, readdirSync, readFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { createRsbuild } from '@rsbuild/core'; |
| 5 | +import { build } from 'vite'; |
| 6 | +import { describe, expect, it } from 'vitest'; |
| 7 | +import SymfonyRsbuild from '../../src/rsbuild'; |
| 8 | +import SymfonyVite from '../../src/vite'; |
| 9 | + |
| 10 | +// A CSS-only dynamic import (`import('x.css')`) makes rolldown-vite prune the async JS chunk while still |
| 11 | +// listing its name in dynamicImports. The collector must not carry that phantom into `dynamic`, or SRI |
| 12 | +// (integrityFromDisk) crashes reading a file that was never written. Common in the wild: a lazy UX |
| 13 | +// Stimulus controller whose autoimport is a stylesheet. |
| 14 | +const fixture = join(import.meta.dirname, '../fixtures/dynamic-css'); |
| 15 | + |
| 16 | +function referenced(app: { js: string[]; css: string[]; preload: string[]; dynamic: string[] }): string[] { |
| 17 | + return [...app.js, ...app.css, ...app.preload, ...app.dynamic]; |
| 18 | +} |
| 19 | + |
| 20 | +describe('CSS-only dynamic import leaves no phantom in `dynamic` (Vite/Rsbuild parity)', () => { |
| 21 | + it('vite: integrity build succeeds and every referenced file exists on disk', async () => { |
| 22 | + const out = mkdtempSync(join(tmpdir(), 'ups-dyncss-vite-')); |
| 23 | + await build({ |
| 24 | + root: fixture, |
| 25 | + logLevel: 'silent', |
| 26 | + build: { emptyOutDir: true, rollupOptions: { input: { app: join(fixture, 'app.js') } } }, |
| 27 | + plugins: [SymfonyVite({ outputPath: out, publicPath: '/build/', integrity: { enabled: true } })], |
| 28 | + }); |
| 29 | + |
| 30 | + const entry = JSON.parse(readFileSync(join(out, 'entrypoints.json'), 'utf8')); |
| 31 | + for (const ref of referenced(entry.entryPoints.app)) { |
| 32 | + expect(existsSync(join(out, ref.replace(/^build\//, '')))).toBe(true); |
| 33 | + } |
| 34 | + for (const ref of Object.keys(entry.integrity ?? {})) { |
| 35 | + expect(existsSync(join(out, ref.replace(/^build\//, '')))).toBe(true); |
| 36 | + } |
| 37 | + // the stylesheet is still emitted, it just carries no phantom JS reference |
| 38 | + expect(readdirSync(out).some((f) => f.endsWith('.css'))).toBe(true); |
| 39 | + }, 30_000); |
| 40 | + |
| 41 | + it('rsbuild: integrity build succeeds and every referenced file exists on disk', async () => { |
| 42 | + const out = mkdtempSync(join(tmpdir(), 'ups-dyncss-rsbuild-')); |
| 43 | + const rsbuild = await createRsbuild({ |
| 44 | + cwd: fixture, |
| 45 | + rsbuildConfig: { |
| 46 | + mode: 'production', |
| 47 | + source: { entry: { app: join(fixture, 'app.js') } }, |
| 48 | + plugins: [ |
| 49 | + SymfonyRsbuild({ |
| 50 | + outputPath: out, |
| 51 | + publicPath: '/build/', |
| 52 | + integrity: { enabled: true, algorithms: ['sha384'] }, |
| 53 | + }), |
| 54 | + ], |
| 55 | + }, |
| 56 | + }); |
| 57 | + await rsbuild.build(); |
| 58 | + |
| 59 | + const entry = JSON.parse(readFileSync(join(out, 'entrypoints.json'), 'utf8')); |
| 60 | + for (const ref of referenced(entry.entryPoints.app)) { |
| 61 | + expect(existsSync(join(out, ref.replace(/^build\//, '')))).toBe(true); |
| 62 | + } |
| 63 | + }, 60_000); |
| 64 | +}); |
0 commit comments