Skip to content

Commit 8dd517f

Browse files
authored
fix: correct use contextDependencies (#846)
1 parent 3619272 commit 8dd517f

File tree

4 files changed

+72
-42
lines changed

4 files changed

+72
-42
lines changed

packages/core/src/plugins/EntryChunkPlugin.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class EntryChunkPlugin {
4646

4747
private enabledImportMetaUrlShim: boolean;
4848
private contextToWatch: string | null = null;
49-
private contextWatched = false;
5049

5150
constructor({
5251
enabledImportMetaUrlShim = true,
@@ -60,12 +59,21 @@ class EntryChunkPlugin {
6059
}
6160

6261
apply(compiler: Rspack.Compiler) {
63-
compiler.hooks.afterCompile.tap(PLUGIN_NAME, (compilation) => {
64-
if (this.contextWatched || this.contextToWatch === null) return;
62+
// TODO: contextDependencies now can only be added in `tapPromise` hook.
63+
// Change to `.tap` when it's supported.
64+
compiler.hooks.afterCompile.tapPromise(PLUGIN_NAME, (compilation) => {
65+
return new Promise((resolve) => {
66+
if (this.contextToWatch === null) {
67+
resolve();
68+
return;
69+
}
6570

66-
const contextDep = compilation.contextDependencies;
67-
contextDep.add(this.contextToWatch);
68-
this.contextWatched = true;
71+
const contextDep = compilation.contextDependencies;
72+
if (!contextDep.has(this.contextToWatch)) {
73+
contextDep.add(this.contextToWatch);
74+
}
75+
resolve();
76+
});
6977
});
7078

7179
compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => {

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
225225
"plugins": [
226226
EntryChunkPlugin {
227227
"contextToWatch": null,
228-
"contextWatched": false,
229228
"enabledImportMetaUrlShim": false,
230229
"reactDirectives": {},
231230
"shebangChmod": 493,
@@ -469,7 +468,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
469468
"plugins": [
470469
EntryChunkPlugin {
471470
"contextToWatch": null,
472-
"contextWatched": false,
473471
"enabledImportMetaUrlShim": true,
474472
"reactDirectives": {},
475473
"shebangChmod": 493,
@@ -691,7 +689,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
691689
"plugins": [
692690
EntryChunkPlugin {
693691
"contextToWatch": null,
694-
"contextWatched": false,
695692
"enabledImportMetaUrlShim": false,
696693
"reactDirectives": {},
697694
"shebangChmod": 493,
@@ -848,7 +845,6 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
848845
"plugins": [
849846
EntryChunkPlugin {
850847
"contextToWatch": null,
851-
"contextWatched": false,
852848
"enabledImportMetaUrlShim": false,
853849
"reactDirectives": {},
854850
"shebangChmod": 493,

tests/integration/cli/build-watch/build.test.ts

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { exec } from 'node:child_process';
1+
import { exec, spawn } from 'node:child_process';
22
import path from 'node:path';
3+
import { after } from 'node:test';
34
import fse from 'fs-extra';
45
import { awaitFileChanges, awaitFileExists } from 'test-helper';
56
import { describe, expect, test } from 'vitest';
@@ -59,46 +60,66 @@ describe('build --watch should handle add / change / unlink', async () => {
5960
const tempSrcPath = path.join(__dirname, 'test-temp-src');
6061
await fse.remove(tempSrcPath);
6162
await fse.remove(path.join(__dirname, 'dist'));
62-
await fse.copy(path.join(__dirname, 'src'), tempSrcPath);
63+
await fse.copy(path.join(__dirname, 'src'), path.resolve(tempSrcPath));
6364
const tempConfigFile = path.join(__dirname, 'test-temp-rslib.config.mjs');
6465
await fse.remove(tempConfigFile);
6566
fse.outputFileSync(
6667
tempConfigFile,
6768
`import { defineConfig } from '@rslib/core';
68-
import { generateBundleEsmConfig } from 'test-helper';
69-
70-
export default defineConfig({
71-
lib: [
72-
generateBundleEsmConfig({
73-
source: {
74-
entry: {
75-
index: 'test-temp-src',
76-
},
77-
},
78-
bundle: false,
79-
}),
80-
],
81-
});
82-
`,
69+
import { generateBundleEsmConfig, generateBundleCjsConfig } from 'test-helper';
70+
71+
export default defineConfig({
72+
lib: [
73+
generateBundleEsmConfig({
74+
bundle: false,
75+
}),
76+
generateBundleCjsConfig({
77+
bundle: false,
78+
}),
79+
],
80+
source: {
81+
entry: {
82+
index: ['./test-temp-src/**'],
83+
},
84+
},
85+
});
86+
`,
8387
);
8488

8589
const srcIndexFile = path.join(tempSrcPath, 'index.ts');
86-
const srcFooFile = path.join(tempSrcPath, 'foo.js');
90+
const srcFooFile = path.join(tempSrcPath, 'foo.ts');
91+
const srcFoo2File = path.join(tempSrcPath, 'foo2.ts');
92+
const distIndexFile = path.join(__dirname, 'dist/esm/index.js');
8793
const distFooFile = path.join(__dirname, 'dist/esm/foo.js');
94+
const distFoo2File = path.join(__dirname, 'dist/esm/foo2.js');
95+
96+
const child = spawn(
97+
'npx',
98+
['rslib', 'build', '--watch', '-c', tempConfigFile],
99+
{
100+
cwd: __dirname,
101+
stdio: 'inherit',
102+
shell: true,
103+
},
104+
);
88105

89-
const process = exec(`npx rslib build --watch -c ${tempConfigFile}`, {
90-
cwd: __dirname,
91-
});
106+
await awaitFileExists(distIndexFile);
92107

93-
// add
94108
fse.outputFileSync(srcFooFile, `export const foo = 'foo';`);
95-
await awaitFileExists(distFooFile);
109+
fse.outputFileSync(srcFoo2File, `export const foo2 = 'foo2';`);
110+
await awaitFileExists(distFoo2File);
96111
const content1 = await fse.readFile(distFooFile, 'utf-8');
97112
expect(content1!).toMatchInlineSnapshot(`
98113
"const foo = 'foo';
99114
export { foo };
100115
"
101116
`);
117+
const content2 = await fse.readFile(distFoo2File, 'utf-8');
118+
expect(content2!).toMatchInlineSnapshot(`
119+
"const foo2 = 'foo2';
120+
export { foo2 };
121+
"
122+
`);
102123

103124
// unlink
104125
// Following "change" cases won't succeed if error is thrown in this step.
@@ -108,13 +129,15 @@ describe('build --watch should handle add / change / unlink', async () => {
108129
const wait = await awaitFileChanges(distFooFile);
109130
fse.outputFileSync(srcFooFile, `export const foo = 'foo1';`);
110131
await wait();
111-
const content2 = await fse.readFile(distFooFile, 'utf-8');
112-
expect(content2!).toMatchInlineSnapshot(`
132+
const content3 = await fse.readFile(distFooFile, 'utf-8');
133+
expect(content3!).toMatchInlineSnapshot(`
113134
"const foo = 'foo1';
114135
export { foo };
115136
"
116137
`);
117138

118-
process.kill();
139+
after(() => {
140+
child.kill();
141+
});
119142
});
120143
});
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { defineConfig } from '@rslib/core';
2-
import { generateBundleEsmConfig } from 'test-helper';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
33

44
export default defineConfig({
55
lib: [
6+
generateBundleCjsConfig({
7+
bundle: false,
8+
}),
69
generateBundleEsmConfig({
7-
source: {
8-
entry: {
9-
index: 'src',
10-
},
11-
},
1210
bundle: false,
1311
}),
1412
],
13+
source: {
14+
entry: {
15+
index: ['./src/**'],
16+
},
17+
},
1518
});

0 commit comments

Comments
 (0)