File tree Expand file tree Collapse file tree 5 files changed +55
-3
lines changed
e2e/cases/plugin-api/plugin-hooks Expand file tree Collapse file tree 5 files changed +55
-3
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,39 @@ rspackOnlyTest(
104
104
} ,
105
105
) ;
106
106
107
+ rspackOnlyTest (
108
+ 'should run plugin hooks correctly when running build and mode is development' ,
109
+ async ( ) => {
110
+ const { plugin, names } = createPlugin ( ) ;
111
+ const rsbuild = await createRsbuild ( {
112
+ cwd : __dirname ,
113
+ rsbuildConfig : {
114
+ mode : 'development' ,
115
+ plugins : [ plugin ] ,
116
+ } ,
117
+ } ) ;
118
+
119
+ const buildInstance = await rsbuild . build ( ) ;
120
+
121
+ await buildInstance . close ( ) ;
122
+
123
+ expect ( names ) . toEqual ( [
124
+ 'ModifyRsbuildConfig' ,
125
+ 'ModifyEnvironmentConfig' ,
126
+ 'ModifyBundlerChain' ,
127
+ 'ModifyBundlerConfig' ,
128
+ 'BeforeCreateCompiler' ,
129
+ 'AfterCreateCompiler' ,
130
+ 'BeforeBuild' ,
131
+ 'BeforeEnvironmentCompile' ,
132
+ 'ModifyHTMLTags' ,
133
+ 'AfterEnvironmentCompile' ,
134
+ 'AfterBuild' ,
135
+ 'OnCloseBuild' ,
136
+ ] ) ;
137
+ } ,
138
+ ) ;
139
+
107
140
rspackOnlyTest (
108
141
'should run plugin hooks correctly when running startDevServer' ,
109
142
async ( { page } ) => {
Original file line number Diff line number Diff line change @@ -46,7 +46,7 @@ export async function createCompiler(options: InitConfigsOptions) {
46
46
done ( stats as Rspack . Stats ) ;
47
47
} ) ;
48
48
49
- if ( context . normalizedConfig ?. mode === 'development ' ) {
49
+ if ( context . command === 'dev ' ) {
50
50
helpers . registerDevHook ( {
51
51
compiler,
52
52
context,
Original file line number Diff line number Diff line change @@ -144,6 +144,8 @@ export async function createRsbuild(
144
144
} ) ;
145
145
146
146
const preview = async ( options : PreviewOptions = { } ) => {
147
+ context . command = 'preview' ;
148
+
147
149
if ( ! getNodeEnv ( ) ) {
148
150
setNodeEnv ( 'production' ) ;
149
151
}
@@ -174,9 +176,12 @@ export async function createRsbuild(
174
176
} ;
175
177
176
178
const build : Build = async ( ...args ) => {
179
+ context . command = 'build' ;
180
+
177
181
if ( ! getNodeEnv ( ) ) {
178
182
setNodeEnv ( 'production' ) ;
179
183
}
184
+
180
185
const buildInstance = await providerInstance . build ( ...args ) ;
181
186
return {
182
187
...buildInstance ,
@@ -188,16 +193,22 @@ export async function createRsbuild(
188
193
} ;
189
194
190
195
const startDevServer : StartDevServer = ( ...args ) => {
196
+ context . command = 'dev' ;
197
+
191
198
if ( ! getNodeEnv ( ) ) {
192
199
setNodeEnv ( 'development' ) ;
193
200
}
201
+
194
202
return providerInstance . startDevServer ( ...args ) ;
195
203
} ;
196
204
197
205
const createDevServer : CreateDevServer = ( ...args ) => {
206
+ context . command = 'dev' ;
207
+
198
208
if ( ! getNodeEnv ( ) ) {
199
209
setNodeEnv ( 'development' ) ;
200
210
}
211
+
201
212
return providerInstance . createDevServer ( ...args ) ;
202
213
} ;
203
214
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
57
57
isCompiling = true ;
58
58
} ) ;
59
59
60
- if ( context . normalizedConfig ?. mode === 'production ' ) {
60
+ if ( context . command === 'build ' ) {
61
61
compiler . hooks . run . tap ( 'rsbuild:run' , logRspackVersion ) ;
62
62
}
63
63
@@ -113,7 +113,7 @@ export async function createCompiler(options: InitConfigsOptions): Promise<{
113
113
} ,
114
114
) ;
115
115
116
- if ( context . normalizedConfig ?. mode === 'development ' ) {
116
+ if ( context . command === 'dev ' ) {
117
117
registerDevHook ( {
118
118
context,
119
119
compiler,
Original file line number Diff line number Diff line change @@ -44,4 +44,12 @@ export type InternalContext = RsbuildContext & {
44
44
environments : Record < string , EnvironmentContext > ;
45
45
/** Only build specified environment. */
46
46
specifiedEnvironments ?: string [ ] ;
47
+ /**
48
+ * The command type.
49
+ *
50
+ * - dev: `rsbuild dev` or `rsbuild.startDevServer()`
51
+ * - build: `rsbuild build` or `rsbuild.build()`
52
+ * - preview: `rsbuild preview` or `rsbuild.preview()`
53
+ */
54
+ command ?: 'dev' | 'build' | 'preview' ;
47
55
} ;
You can’t perform that action at this time.
0 commit comments