Skip to content

Commit 6968aae

Browse files
committed
test: add assetInclude and update lock
1 parent 1b4d8bd commit 6968aae

File tree

9 files changed

+96
-9
lines changed

9 files changed

+96
-9
lines changed

packages/core/src/asset/assetConfig.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
3636
? originalGeneratorOptions
3737
: {
3838
...originalGeneratorOptions,
39-
experimentalLibPreserveImport: true,
39+
importMode: 'preserve',
4040
};
4141

4242
const rule = config.module.rule(CHAIN_ID.RULE.SVG);
@@ -57,20 +57,25 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
5757
CHAIN_ID.RULE.FONT,
5858
CHAIN_ID.RULE.MEDIA,
5959
CHAIN_ID.RULE.IMAGE,
60+
CHAIN_ID.RULE.ADDITIONAL_ASSETS,
6061
];
6162
for (const ruleId of ruleIds) {
6263
const oneOfId = `${ruleId}-asset`;
63-
const assetRule = config.module.rules.get(ruleId).oneOfs.get(oneOfId);
64+
const assetRule = config.module.rules.get(ruleId);
65+
if (!assetRule) {
66+
continue;
67+
}
68+
const assetRuleOneOf = assetRule.oneOfs.get(oneOfId);
6469

65-
const originalTypeOptions = assetRule.get('type');
66-
const originalParserOptions = assetRule.get('parser');
67-
const originalGeneratorOptions = assetRule.get('generator');
70+
const originalTypeOptions = assetRuleOneOf.get('type');
71+
const originalParserOptions = assetRuleOneOf.get('parser');
72+
const originalGeneratorOptions = assetRuleOneOf.get('generator');
6873

6974
const generatorOptions = isUserSetPublicPath
7075
? originalGeneratorOptions
7176
: {
7277
...originalGeneratorOptions,
73-
experimentalLibPreserveImport: true,
78+
importMode: 'preserve',
7479
};
7580

7681
const rule = config.module.rule(ruleId);

pnpm-lock.yaml

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/asset/__snapshots__/index.test.ts.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ export { __webpack_exports__bar as bar, __webpack_exports__foo as foo };
2525
"
2626
`;
2727

28+
exports[`use source.assetInclude 1`] = `
29+
"import draft_namespaceObject from "./static/assets/draft.txt";
30+
console.log(draft_namespaceObject);
31+
"
32+
`;
33+
34+
exports[`use source.assetInclude 2`] = `
35+
"import draft_rslib_entry_namespaceObject from "../static/assets/draft.txt";
36+
export { draft_rslib_entry_namespaceObject as default };
37+
"
38+
`;
39+
2840
exports[`use svgr 1`] = `
2941
"import __rslib_svgr_url__0__ from "./static/svg/logo.svg";
3042
import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__ from "react/jsx-runtime";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "assets-include-test",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module"
6+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
// 0. bundle
7+
// esm
8+
generateBundleEsmConfig({
9+
source: {
10+
assetsInclude: /\.txt/,
11+
},
12+
output: {
13+
distPath: {
14+
root: './dist/esm/bundle',
15+
},
16+
},
17+
}),
18+
// 1. bundleless
19+
// esm
20+
generateBundleEsmConfig({
21+
bundle: false,
22+
source: {
23+
assetsInclude: /\.txt/,
24+
},
25+
output: {
26+
distPath: {
27+
root: './dist/esm/bundleless',
28+
},
29+
},
30+
}),
31+
],
32+
output: {
33+
target: 'web',
34+
},
35+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
this is a txt file
2+
this is a txt file
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import draft from './assets/draft.txt';
2+
3+
console.log(draft);

tests/integration/asset/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,17 @@ test('use asset/source', async () => {
322322
const { content: dataJs } = queryContent(contents.esm1!, /assets\/draft\.js/);
323323
expect(dataJs).matchSnapshot();
324324
});
325+
326+
test('use source.assetInclude', async () => {
327+
const fixturePath = join(__dirname, 'asset-include');
328+
const { contents } = await buildAndGetResults({ fixturePath });
329+
330+
// 0. bundle
331+
// esm
332+
const { content: indexJs } = queryContent(contents.esm0!, /index\.js/);
333+
expect(indexJs).matchSnapshot();
334+
// 1. bundleless
335+
// esm
336+
const { content: dataJs } = queryContent(contents.esm1!, /assets\/draft\.js/);
337+
expect(dataJs).matchSnapshot();
338+
});

tests/integration/asset/source/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "assets-include-test",
2+
"name": "assets-source-test",
33
"version": "1.0.0",
44
"private": true,
55
"type": "module"

0 commit comments

Comments
 (0)