|
| 1 | +import path from 'node:path' |
| 2 | +import { expect, test } from 'vitest' |
| 3 | +import { type Plugin, rolldown } from 'rolldown' |
| 4 | +import pluginReact, { type Options } from '../src/index.ts' |
| 5 | + |
| 6 | +test('HMR related code should not be included when using rolldown', async () => { |
| 7 | + const { output } = await bundleWithRolldown() |
| 8 | + |
| 9 | + expect(output[0].code).toBeDefined() |
| 10 | + expect(output[0].code).not.toContain('import.meta.hot') |
| 11 | +}) |
| 12 | + |
| 13 | +test('HMR related code should not be included when using rolldown with babel plugin', async () => { |
| 14 | + const { output } = await bundleWithRolldown({ |
| 15 | + babel: { |
| 16 | + plugins: [['babel-plugin-react-compiler', {}]], |
| 17 | + }, |
| 18 | + }) |
| 19 | + |
| 20 | + expect(output[0].code).toBeDefined() |
| 21 | + expect(output[0].code).not.toContain('import.meta.hot') |
| 22 | +}) |
| 23 | + |
| 24 | +async function bundleWithRolldown(pluginReactOptions: Options = {}) { |
| 25 | + const ENTRY = '/entry.tsx' |
| 26 | + const files: Record<string, string> = { |
| 27 | + [ENTRY]: /* tsx */ ` |
| 28 | + import React from "react" |
| 29 | + import { hydrateRoot } from "react-dom/client" |
| 30 | + import App from "./App.tsx" |
| 31 | +
|
| 32 | + const container = document.getElementById("root"); |
| 33 | + hydrateRoot(container, <App />); |
| 34 | + `, |
| 35 | + '/App.tsx': /* tsx */ ` |
| 36 | + export default function App() { |
| 37 | + return <div>Hello World</div> |
| 38 | + } |
| 39 | + `, |
| 40 | + } |
| 41 | + |
| 42 | + const bundle = await rolldown({ |
| 43 | + input: ENTRY, |
| 44 | + plugins: [virtualFilesPlugin(files), pluginReact(pluginReactOptions)], |
| 45 | + external: [/^react(\/|$)/, /^react-dom(\/|$)/], |
| 46 | + }) |
| 47 | + return await bundle.generate({ format: 'esm' }) |
| 48 | +} |
| 49 | + |
| 50 | +function virtualFilesPlugin(files: Record<string, string>): Plugin { |
| 51 | + return { |
| 52 | + name: 'virtual-files', |
| 53 | + resolveId(id, importer) { |
| 54 | + const baseDir = importer ? path.posix.dirname(importer) : '/' |
| 55 | + const result = path.posix.resolve(baseDir, id) |
| 56 | + if (result in files) { |
| 57 | + return result |
| 58 | + } |
| 59 | + }, |
| 60 | + load(id) { |
| 61 | + if (id in files) { |
| 62 | + return files[id] |
| 63 | + } |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
0 commit comments