|
| 1 | +import { join } from 'node:path'; |
| 2 | +import { |
| 3 | + getRandomPort, |
| 4 | + gotoPage, |
| 5 | + proxyConsole, |
| 6 | + rspackOnlyTest, |
| 7 | +} from '@e2e/helper'; |
| 8 | +import { expect } from '@playwright/test'; |
| 9 | +import { createRsbuild, type RsbuildPlugin } from '@rsbuild/core'; |
| 10 | +import fse from 'fs-extra'; |
| 11 | + |
| 12 | +const createPlugin = () => { |
| 13 | + const names: string[] = []; |
| 14 | + |
| 15 | + const plugin: RsbuildPlugin = { |
| 16 | + name: 'test-plugin', |
| 17 | + setup(api) { |
| 18 | + api.onBeforeEnvironmentCompile(({ environment }) => { |
| 19 | + names.push(`BeforeEnvironmentCompile ${environment.name}`); |
| 20 | + }); |
| 21 | + api.onBeforeDevCompile(() => { |
| 22 | + names.push('BeforeDevCompile'); |
| 23 | + }); |
| 24 | + }, |
| 25 | + }; |
| 26 | + |
| 27 | + return { plugin, names }; |
| 28 | +}; |
| 29 | + |
| 30 | +rspackOnlyTest( |
| 31 | + 'should run onBeforeDevCompile hook correctly when rebuild in dev with multiple environments', |
| 32 | + async ({ page }) => { |
| 33 | + process.env.NODE_ENV = 'development'; |
| 34 | + const cwd = __dirname; |
| 35 | + const filePath = join(cwd, 'test-temp-src', 'index.js'); |
| 36 | + await fse.outputFile(filePath, "console.log('1');"); |
| 37 | + |
| 38 | + const port = await getRandomPort(); |
| 39 | + |
| 40 | + const { plugin, names } = createPlugin(); |
| 41 | + |
| 42 | + const { expectLog, restore, expectBuildEnd } = proxyConsole(); |
| 43 | + |
| 44 | + const rsbuild = await createRsbuild({ |
| 45 | + cwd: __dirname, |
| 46 | + rsbuildConfig: { |
| 47 | + plugins: [plugin], |
| 48 | + server: { |
| 49 | + port, |
| 50 | + }, |
| 51 | + environments: { |
| 52 | + web: {}, |
| 53 | + node: { |
| 54 | + source: { |
| 55 | + entry: { |
| 56 | + index: './test-temp-src/index.js', |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + performance: { |
| 62 | + printFileSize: false, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + const result = await rsbuild.startDevServer(); |
| 68 | + |
| 69 | + await gotoPage(page, result); |
| 70 | + |
| 71 | + expect(names.includes('BeforeDevCompile')).toBeTruthy(); |
| 72 | + expect(names.includes('BeforeEnvironmentCompile node')).toBeTruthy(); |
| 73 | + expect(names.includes('BeforeEnvironmentCompile web')).toBeTruthy(); |
| 74 | + |
| 75 | + names.length = 0; |
| 76 | + |
| 77 | + // rebuild |
| 78 | + await fse.outputFile(filePath, "console.log('2');"); |
| 79 | + await expectLog('building test-temp-src'); |
| 80 | + await expectBuildEnd(); |
| 81 | + |
| 82 | + expect(names).toEqual([ |
| 83 | + 'BeforeDevCompile', |
| 84 | + // only recompile the node environment which is affected by the file change |
| 85 | + 'BeforeEnvironmentCompile node', |
| 86 | + ]); |
| 87 | + |
| 88 | + await result.server.close(); |
| 89 | + |
| 90 | + restore(); |
| 91 | + |
| 92 | + process.env.NODE_ENV = 'test'; |
| 93 | + }, |
| 94 | +); |
0 commit comments