-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add environment hooks about compiler (#3108)
Co-authored-by: neverland <[email protected]>
- Loading branch information
1 parent
7ce5251
commit ac4e2a3
Showing
27 changed files
with
1,034 additions
and
417 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import { getRandomPort, gotoPage, rspackOnlyTest } from '@e2e/helper'; | ||
import { expect } from '@playwright/test'; | ||
import { type RsbuildPlugin, createRsbuild } from '@rsbuild/core'; | ||
|
||
const createPlugin = () => { | ||
const names: string[] = []; | ||
|
||
const plugin: RsbuildPlugin = { | ||
name: 'test-plugin', | ||
setup(api) { | ||
api.modifyRspackConfig((_config, { environment }) => { | ||
names.push(`ModifyBundlerConfig ${environment.name}`); | ||
}); | ||
api.modifyWebpackChain((_config, { environment }) => { | ||
names.push(`ModifyBundlerConfig ${environment.name}`); | ||
}); | ||
api.modifyRsbuildConfig(() => { | ||
names.push('ModifyRsbuildConfig'); | ||
}); | ||
api.modifyEnvironmentConfig((_config, { name }) => { | ||
names.push(`ModifyEnvironmentConfig ${name}`); | ||
}); | ||
api.modifyBundlerChain((_chain, { environment }) => { | ||
names.push(`ModifyBundlerChain ${environment.name}`); | ||
}); | ||
api.modifyHTMLTags((tags, { environment }) => { | ||
names.push(`ModifyHTMLTags ${environment.name}`); | ||
return tags; | ||
}); | ||
api.onBeforeStartDevServer(() => { | ||
names.push('BeforeStartDevServer'); | ||
}); | ||
api.onAfterStartDevServer(() => { | ||
names.push('AfterStartDevServer'); | ||
}); | ||
api.onBeforeCreateCompiler(() => { | ||
names.push('BeforeCreateCompiler'); | ||
}); | ||
api.onAfterCreateCompiler(() => { | ||
names.push('AfterCreateCompiler'); | ||
}); | ||
api.onBeforeBuild(() => { | ||
names.push('BeforeBuild'); | ||
}); | ||
api.onAfterBuild(() => { | ||
names.push('AfterBuild'); | ||
}); | ||
api.onBeforeEnvironmentCompile(({ environment }) => { | ||
names.push(`BeforeEnvironmentCompile ${environment.name}`); | ||
}); | ||
api.onAfterEnvironmentCompile(({ stats, environment }) => { | ||
expect(stats?.compilation.name).toBe(environment.name); | ||
names.push(`AfterEnvironmentCompile ${environment.name}`); | ||
}); | ||
api.onBeforeStartProdServer(() => { | ||
names.push('BeforeStartProdServer'); | ||
}); | ||
api.onCloseDevServer(() => { | ||
names.push('OnCloseDevServer'); | ||
}); | ||
api.onAfterStartProdServer(() => { | ||
names.push('AfterStartProdServer'); | ||
}); | ||
api.onDevCompileDone(() => { | ||
names.push('OnDevCompileDone'); | ||
}); | ||
}, | ||
}; | ||
|
||
return { plugin, names }; | ||
}; | ||
|
||
rspackOnlyTest( | ||
'should run plugin hooks correctly when running build with multiple environments', | ||
async () => { | ||
const { plugin, names } = createPlugin(); | ||
const rsbuild = await createRsbuild({ | ||
cwd: __dirname, | ||
rsbuildConfig: { | ||
plugins: [plugin], | ||
environments: { | ||
web: {}, | ||
node: {}, | ||
}, | ||
}, | ||
}); | ||
|
||
await rsbuild.build(); | ||
|
||
// Test environment hook is always called twice | ||
expect(names.filter((name) => name.includes(' web')).length).toBe( | ||
names.filter((name) => name.includes(' node')).length, | ||
); | ||
|
||
// The execution order between different Environments of the same hook is not fixed | ||
// Therefore, we only test the execution order of a single Environment | ||
expect(names.filter((name) => !name.includes(' node'))).toEqual([ | ||
'ModifyRsbuildConfig', | ||
'ModifyEnvironmentConfig web', | ||
'ModifyBundlerChain web', | ||
'ModifyBundlerConfig web', | ||
'BeforeCreateCompiler', | ||
'AfterCreateCompiler', | ||
'BeforeEnvironmentCompile web', | ||
'BeforeBuild', | ||
'ModifyHTMLTags web', | ||
'AfterEnvironmentCompile web', | ||
'AfterBuild', | ||
]); | ||
|
||
expect(names.filter((name) => !name.includes(' web'))).toEqual([ | ||
'ModifyRsbuildConfig', | ||
'ModifyEnvironmentConfig node', | ||
'ModifyBundlerChain node', | ||
'ModifyBundlerConfig node', | ||
'BeforeCreateCompiler', | ||
'AfterCreateCompiler', | ||
'BeforeEnvironmentCompile node', | ||
'BeforeBuild', | ||
'ModifyHTMLTags node', | ||
'AfterEnvironmentCompile node', | ||
'AfterBuild', | ||
]); | ||
}, | ||
); | ||
|
||
rspackOnlyTest( | ||
'should run plugin hooks correctly when running startDevServer with multiple environments', | ||
async ({ page }) => { | ||
process.env.NODE_ENV = 'development'; | ||
const port = await getRandomPort(); | ||
|
||
const { plugin, names } = createPlugin(); | ||
const rsbuild = await createRsbuild({ | ||
cwd: __dirname, | ||
rsbuildConfig: { | ||
plugins: [plugin], | ||
server: { | ||
port, | ||
}, | ||
environments: { | ||
web: {}, | ||
node: {}, | ||
}, | ||
}, | ||
}); | ||
|
||
const result = await rsbuild.startDevServer(); | ||
|
||
await gotoPage(page, result); | ||
|
||
await result.server.close(); | ||
|
||
expect(names.filter((name) => name.includes(' web')).length).toBe( | ||
names.filter((name) => name.includes(' node')).length, | ||
); | ||
|
||
expect(names.filter((name) => !name.includes(' node'))).toEqual([ | ||
'ModifyRsbuildConfig', | ||
'ModifyEnvironmentConfig web', | ||
'BeforeStartDevServer', | ||
'ModifyBundlerChain web', | ||
'ModifyBundlerConfig web', | ||
'BeforeCreateCompiler', | ||
'AfterCreateCompiler', | ||
'BeforeEnvironmentCompile web', | ||
'AfterStartDevServer', | ||
'ModifyHTMLTags web', | ||
'AfterEnvironmentCompile web', | ||
'OnDevCompileDone', | ||
'OnCloseDevServer', | ||
]); | ||
|
||
expect(names.filter((name) => !name.includes(' web'))).toEqual([ | ||
'ModifyRsbuildConfig', | ||
'ModifyEnvironmentConfig node', | ||
'BeforeStartDevServer', | ||
'ModifyBundlerChain node', | ||
'ModifyBundlerConfig node', | ||
'BeforeCreateCompiler', | ||
'AfterCreateCompiler', | ||
'BeforeEnvironmentCompile node', | ||
'AfterStartDevServer', | ||
'ModifyHTMLTags node', | ||
'AfterEnvironmentCompile node', | ||
'OnDevCompileDone', | ||
'OnCloseDevServer', | ||
]); | ||
|
||
process.env.NODE_ENV = 'test'; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.