Skip to content

Commit f4684f4

Browse files
committed
chore: update publicPath
1 parent ceaae5a commit f4684f4

File tree

3 files changed

+91
-20
lines changed

3 files changed

+91
-20
lines changed

packages/core/src/asset/assetConfig.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ const RSBUILD_SVGR_PLUGIN_NAME = 'rsbuild:svgr';
1212

1313
/**
1414
* Be compatible to css-extract importModule and experimentalLibPreserveExports
15-
* when set experimentalLibPreserveExports to true, the css-loader result can not executed in node side
15+
* when set experimentalLibPreserveExports to true, the css-loader result can not executed in node side, so clone the assets rule
16+
* 1. js assets: original rule set issuer and experimentalLibPreserveExports
17+
* 2. css assets: a copy of original rule
1618
*/
1719
const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
1820
name: PLUGIN_NAME,
1921
pre: [RSBUILD_SVGR_PLUGIN_NAME],
2022
setup(api) {
2123
api.modifyBundlerChain((config, { CHAIN_ID }) => {
22-
// clone asset rules to separate css and js assets
2324
// 1. modify svg rule first, svg is special because of svgr
2425
const svgAssetRule = config.module.rules
2526
.get(CHAIN_ID.RULE.SVG)
@@ -28,10 +29,15 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
2829
const originalParserOptions = svgAssetRule.get('parser');
2930
const originalGeneratorOptions = svgAssetRule.get('generator');
3031

31-
const generatorOptions = {
32-
...originalGeneratorOptions,
33-
experimentalLibPreserveImport: true,
34-
};
32+
const isUserSetPublicPath = config.output.get('publicPath') !== 'auto';
33+
34+
// if user sets publicPath, do not preserve asset import
35+
const generatorOptions = isUserSetPublicPath
36+
? originalGeneratorOptions
37+
: {
38+
...originalGeneratorOptions,
39+
experimentalLibPreserveImport: true,
40+
};
3541

3642
const rule = config.module.rule(CHAIN_ID.RULE.SVG);
3743

@@ -60,10 +66,12 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
6066
const originalParserOptions = assetRule.get('parser');
6167
const originalGeneratorOptions = assetRule.get('generator');
6268

63-
const generatorOptions = {
64-
...originalGeneratorOptions,
65-
experimentalLibPreserveImport: true,
66-
};
69+
const generatorOptions = isUserSetPublicPath
70+
? originalGeneratorOptions
71+
: {
72+
...originalGeneratorOptions,
73+
experimentalLibPreserveImport: true,
74+
};
6775

6876
const rule = config.module.rule(ruleId);
6977
rule.oneOf(oneOfId).generator(generatorOptions).issuer({
@@ -78,7 +86,7 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
7886
.issuer(CSS_EXTENSIONS_PATTERN);
7987
}
8088

81-
// svgr
89+
// for svgr
8290
// 1. remove __webpack_require__.p in svgr url-loader and file-loader
8391
const isUsingSvgr = Boolean(
8492
config.module

tests/integration/asset/index.test.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,66 @@ test('set the assets public path', async () => {
201201
const fixturePath = join(__dirname, 'public-path');
202202
const { contents } = await buildAndGetResults({ fixturePath });
203203

204-
// umd should preserve '__webpack_require__.p'
204+
// 1. umd should preserve '__webpack_require__.p'
205205
const { content: indexUmdJs } = queryContent(contents.umd!, /index\.js/);
206206

207207
expect(indexUmdJs).toContain(
208-
'__webpack_require__.p = "/public/path/bundleless/";',
208+
'__webpack_require__.p = "/public/path/";',
209209
);
210210
expect(indexUmdJs).toContain(
211211
'const image_namespaceObject = __webpack_require__.p + "static/image/image.png";',
212212
);
213+
214+
// 2. bundle
215+
// esm
216+
const { content: indexJs } = queryContent(
217+
contents.esm0!,
218+
/index\.js/,
219+
);
220+
expect(indexJs).toMatchInlineSnapshot(`
221+
"var __webpack_module_cache__ = {};
222+
function __webpack_require__(moduleId) {
223+
var cachedModule = __webpack_module_cache__[moduleId];
224+
if (void 0 !== cachedModule) return cachedModule.exports;
225+
var module = __webpack_module_cache__[moduleId] = {
226+
exports: {}
227+
};
228+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
229+
return module.exports;
230+
}
231+
(()=>{
232+
__webpack_require__.p = "/public/path/";
233+
})();
234+
const image_namespaceObject = __webpack_require__.p + "static/image/image.png";
235+
const src = image_namespaceObject;
236+
export { src as default };
237+
"
238+
`);
239+
240+
// 3. bundleless
241+
// esm
242+
const { content: imageJs } = queryContent(
243+
contents.esm1!,
244+
/assets\/image\.js/,
245+
);
246+
expect(imageJs).toMatchInlineSnapshot(`
247+
"var __webpack_module_cache__ = {};
248+
function __webpack_require__(moduleId) {
249+
var cachedModule = __webpack_module_cache__[moduleId];
250+
if (void 0 !== cachedModule) return cachedModule.exports;
251+
var module = __webpack_module_cache__[moduleId] = {
252+
exports: {}
253+
};
254+
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
255+
return module.exports;
256+
}
257+
(()=>{
258+
__webpack_require__.p = "/public/path/";
259+
})();
260+
const image_rslib_entry_namespaceObject = __webpack_require__.p + "static/image/image.png";
261+
export { image_rslib_entry_namespaceObject as default };
262+
"
263+
`);
213264
});
214265

215266
test('use svgr', async () => {

tests/integration/asset/public-path/rslib.config.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
import { defineConfig } from '@rslib/core';
2-
import { generateBundleUmdConfig } from 'test-helper';
2+
import { generateBundleEsmConfig, generateBundleUmdConfig } from 'test-helper';
33

44
export default defineConfig({
55
lib: [
66
generateBundleUmdConfig({
77
output: {
8-
assetPrefix: '/public/path/bundleless',
8+
assetPrefix: '/public/path',
9+
},
10+
}),
11+
generateBundleEsmConfig({
12+
output: {
13+
distPath: {
14+
root: './dist/esm/bundle',
15+
},
16+
assetPrefix: '/public/path',
17+
},
18+
}),
19+
generateBundleEsmConfig({
20+
bundle: false,
21+
output: {
22+
distPath: {
23+
root: './dist/esm/bundle-false',
24+
},
25+
assetPrefix: '/public/path',
926
},
1027
}),
1128
],
12-
source: {
13-
entry: {
14-
index: './src/index.js',
15-
},
16-
},
1729
output: {
1830
target: 'web',
1931
},

0 commit comments

Comments
 (0)