Skip to content

Commit c321ed6

Browse files
committed
chore: only take effect in web environment
1 parent dc7aafc commit c321ed6

6 files changed

Lines changed: 96 additions & 6 deletions

File tree

src/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ export const pluginAssetsRetry = (
112112

113113
if (inlineScript) {
114114
api.modifyHTMLTags(async ({ headTags, bodyTags }, { environment }) => {
115+
if (environment.config.output.target !== 'web') {
116+
return { headTags, bodyTags };
117+
}
115118
const { minify, crossorigin } = getDefaultValueFromRsbuildConfig(
116119
environment.config,
117120
);
@@ -131,6 +134,9 @@ export const pluginAssetsRetry = (
131134
} else {
132135
api.modifyHTMLTags(
133136
async ({ headTags, bodyTags }, { assetPrefix, environment }) => {
137+
if (environment.config.output.target !== 'web') {
138+
return { headTags, bodyTags };
139+
}
134140
const scriptPath = getScriptPath(environment);
135141
const url = ensureAssetPrefix(scriptPath, assetPrefix);
136142

@@ -149,10 +155,13 @@ export const pluginAssetsRetry = (
149155
api.processAssets(
150156
{ stage: 'additional' },
151157
async ({ sources, compilation, environment }) => {
158+
const { config } = environment;
159+
if (config.output.target !== 'web') {
160+
return;
161+
}
152162
const scriptPath = getScriptPath(environment);
153-
const { crossorigin, minify } = getDefaultValueFromRsbuildConfig(
154-
environment.config,
155-
);
163+
const { crossorigin, minify } =
164+
getDefaultValueFromRsbuildConfig(config);
156165
const runtimeOptions = getRuntimeOptions(userOptions, crossorigin);
157166
const code = await getRetryCode(runtimeOptions, minify);
158167
compilation.emitAsset(scriptPath, new sources.RawSource(code));
@@ -161,9 +170,8 @@ export const pluginAssetsRetry = (
161170
}
162171

163172
api.modifyBundlerChain(async (chain, { environment }) => {
164-
const { config, htmlPaths } = environment;
165-
166-
if (!userOptions || Object.keys(htmlPaths).length === 0) {
173+
const { config } = environment;
174+
if (config.output.target !== 'web') {
167175
return;
168176
}
169177

test/node/asyncChunk.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('asyncChunk');

test/node/index.client.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import('./asyncChunk.js').then(({ default: value }) => {
2+
console.log('asyncChunk', value);
3+
});

test/node/index.server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fs = require('node:fs');
2+
3+
module.exports = {
4+
myFs: fs,
5+
};

test/node/index.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fs from 'node:fs/promises';
2+
import { expect, test } from '@playwright/test';
3+
import { createRsbuild } from '@rsbuild/core';
4+
5+
const build = async () => {
6+
const rsbuild = await createRsbuild({
7+
cwd: import.meta.dirname,
8+
});
9+
await rsbuild.build();
10+
};
11+
12+
test('should not work in node environment', async () => {
13+
await build();
14+
15+
// dist/server only contains one file
16+
expect((await fs.readdir('dist/server')).length).toBe(1);
17+
// only dist/server/index.js exists
18+
expect(
19+
await fs.access('dist/server/index.js').then(
20+
() => true,
21+
() => false,
22+
),
23+
).toBe(true);
24+
25+
// dist/static/js contains two files, index.js and assets-retry.js
26+
expect((await fs.readdir('dist/static/js')).length).toBe(2);
27+
// index.js contains "registerAsyncChunkRetry" function calling
28+
expect(await fs.readFile('dist/static/js/index.js', 'utf-8')).toContain(
29+
'registerAsyncChunkRetry',
30+
);
31+
});

test/node/rsbuild.config.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { defineConfig } from '@rsbuild/core';
2+
import { pluginAssetsRetry } from '@rsbuild/plugin-assets-retry';
3+
import { pluginReact } from '@rsbuild/plugin-react';
4+
5+
export default defineConfig({
6+
plugins: [
7+
pluginReact(),
8+
pluginAssetsRetry({
9+
domain: ['http://localhost:3000', 'http://localhost:3001'],
10+
inlineScript: false,
11+
}),
12+
],
13+
environments: {
14+
node: {
15+
output: {
16+
target: 'node',
17+
distPath: {
18+
root: 'dist/server',
19+
},
20+
},
21+
source: {
22+
entry: {
23+
index: './index.server.js',
24+
},
25+
},
26+
},
27+
web: {
28+
source: {
29+
entry: {
30+
index: './index.client.js',
31+
},
32+
},
33+
output: {
34+
minify: false,
35+
filenameHash: false,
36+
},
37+
},
38+
},
39+
tools: {
40+
htmlPlugin: false,
41+
},
42+
});

0 commit comments

Comments
 (0)