Skip to content

Commit d7a6903

Browse files
committed
chore: update
1 parent e246175 commit d7a6903

File tree

24 files changed

+1307
-231
lines changed

24 files changed

+1307
-231
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"pnpm-lock.yaml": "pnpm dedupe --check"
4141
},
4242
"devDependencies": {
43+
"@rsdoctor/rspack-plugin": "0.4.13",
4344
"@biomejs/biome": "^1.9.4",
4445
"@changesets/cli": "^2.27.11",
4546
"@types/fs-extra": "^11.0.4",

packages/core/src/asset/LibAssetExtractPlugin.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function extractAssetFilenames(content: string): string[] {
2828

2929
// 2. bundle: concatenated
3030
const CONCATENATED_PATTERN: RegExp =
31-
/(const|var) (\w+) = __webpack_require__\.p\s\+\s["'](.+)["']/g;
31+
/(const|var)\s(\w+)\s=\s\(?__webpack_require__\.p\s\+\s["'](.+)["']\)?/g;
3232
const concatenatedEsmReplaceTemplate = (variableName: string, url: string) =>
3333
`import ${variableName} from '${url}';`;
3434
const concatenatedCjsReplaceTemplate = (
@@ -46,8 +46,9 @@ const nonConcatenatedReplaceTemplate = (url: string) =>
4646
const pluginName = 'LIB_ASSET_EXTRACT_PLUGIN';
4747

4848
type Options = {
49-
// just for perf, in bundleless we can replace the entire file
49+
// bundle and isUsingSvgr options: just for perf, in bundleless we can replace the entire file directly
5050
bundle: boolean;
51+
isUsingSvgr: boolean;
5152
};
5253

5354
class LibAssetExtractPlugin implements Rspack.RspackPluginInstance {
@@ -63,19 +64,22 @@ class LibAssetExtractPlugin implements Rspack.RspackPluginInstance {
6364
const chunkAsset = Object.keys(assets).filter((name) =>
6465
/js$/.test(name),
6566
);
67+
const isEsmFormat = compilation.options.output.module;
68+
const canEntireFileReplacedDirectly = !this.options.bundle && !this.options.isUsingSvgr;
6669
for (const name of chunkAsset) {
67-
const isEsmFormat = compilation.options.output.module;
6870
const undoPath = getUndoPath(
6971
name,
7072
compilation.outputOptions.path!,
7173
true,
7274
);
7375
compilation.updateAsset(name, (old) => {
7476
const oldSource = old.source().toString();
77+
console.log(canEntireFileReplacedDirectly, this.options)
7578

7679
// bundleless
77-
if (this.options.bundle === false) {
80+
if (canEntireFileReplacedDirectly) {
7881
const assetFilenames = extractAssetFilenames(oldSource);
82+
// asset/resource
7983
if (assetFilenames.length === 0) {
8084
return old;
8185
}

packages/core/src/asset/assetConfig.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
1-
import type { EnvironmentConfig } from '@rsbuild/core';
1+
import type { EnvironmentConfig, RsbuildPlugin } from '@rsbuild/core';
22
import type { Format } from '../types';
33
import { LibAssetExtractPlugin } from './LibAssetExtractPlugin';
4-
LibAssetExtractPlugin;
4+
5+
const PLUGIN_NAME = 'rsbuild:lib-asset-bundleless';
6+
7+
const RSBUILD_SVGR_PLUGIN_NAME = 'rsbuild:svgr';
8+
const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
9+
name: PLUGIN_NAME,
10+
pre: [RSBUILD_SVGR_PLUGIN_NAME],
11+
setup(api) {
12+
api.modifyBundlerChain((config, { CHAIN_ID }) => {
13+
// only support transform the svg asset to mixedImport svgr file
14+
// remove issuer to make every svg asset is transformed
15+
const isUsingSvgr = Boolean(
16+
config.module
17+
.rule(CHAIN_ID.RULE.SVG)
18+
.oneOf(CHAIN_ID.RULE.SVG)
19+
.uses.has(CHAIN_ID.USE.SVGR),
20+
);
21+
if (isUsingSvgr) {
22+
const rule = config.module
23+
.rule(CHAIN_ID.RULE.SVG)
24+
.oneOf(CHAIN_ID.RULE.SVG);
25+
rule.issuer([]);
26+
}
27+
28+
config
29+
.plugin(LibAssetExtractPlugin.name)
30+
.use(LibAssetExtractPlugin, [{ bundle, isUsingSvgr }]);
31+
});
32+
},
33+
});
534

635
// TODO: asset config document
736
export const composeAssetConfig = (
@@ -17,22 +46,19 @@ export const composeAssetConfig = (
1746
},
1847
tools: {
1948
rspack: {
20-
// plugins: [new LibAssetExtractPlugin({ bundle: true })],
49+
plugins: [
50+
new LibAssetExtractPlugin({ bundle: true, isUsingSvgr: false }),
51+
],
2152
},
2253
},
2354
};
2455
}
25-
// TODO: bundleless
2656
return {
2757
output: {
2858
dataUriLimit: 0, // default: no inline asset
2959
assetPrefix: 'auto',
3060
},
31-
tools: {
32-
rspack: {
33-
// plugins: [new LibAssetExtractPlugin({ bundle: false })],
34-
},
35-
},
61+
plugins: [pluginLibAsset({ bundle: false })],
3662
};
3763
}
3864

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
220220
"name": "LIB_ASSET_EXTRACT_PLUGIN",
221221
"options": {
222222
"bundle": true,
223+
"isUsingSvgr": false,
223224
},
224225
},
225226
],
@@ -466,6 +467,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
466467
"name": "LIB_ASSET_EXTRACT_PLUGIN",
467468
"options": {
468469
"bundle": true,
470+
"isUsingSvgr": false,
469471
},
470472
},
471473
],

packages/core/tests/constant.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { expect, test } from 'vitest';
2-
import {
3-
CSS_EXTENSIONS_PATTERN,
4-
ENTRY_EXTENSIONS_PATTERN,
5-
JS_EXTENSIONS_PATTERN,
6-
} from '../src/constant';
2+
import { CSS_EXTENSIONS_PATTERN, DTS_EXTENSIONS_PATTERN, JS_EXTENSIONS_PATTERN } from '../src/constant';
73

84
const jsTestStrings = [
95
{ str: 'index.js', expected: true },
@@ -25,6 +21,17 @@ const cssTestStrings = [
2521
{ str: '/Users/path/index.sass', expected: true },
2622
];
2723

24+
const dtsTestStrings = [
25+
{ str: 'index.js', expected: false },
26+
{ str: './index.ts', expected: false },
27+
{ str: './index.d.ts', expected: true },
28+
{ str: '/Users/path/index.ts', expected: false },
29+
{ str: '/Users/path/index.d.ts', expected: true },
30+
{ str: '/Users/path/index.d.mts', expected: true },
31+
{ str: '/Users/path/index.d.cts', expected: true },
32+
{ str: '/Users/path/index.tsx', expected: false },
33+
];
34+
2835
test('JS_EXTENSIONS_PATTERN', () => {
2936
for (const { str, expected } of jsTestStrings) {
3037
expect(JS_EXTENSIONS_PATTERN.test(str)).toBe(expected);
@@ -37,8 +44,8 @@ test('CSS_EXTENSIONS_PATTERN', () => {
3744
}
3845
});
3946

40-
test('ENTRY_EXTENSIONS_PATTERN', () => {
41-
for (const { str, expected } of [...jsTestStrings, ...cssTestStrings]) {
42-
expect(ENTRY_EXTENSIONS_PATTERN.test(str)).toBe(expected);
47+
test('DTS_EXTENSIONS_PATTERN', () => {
48+
for (const { str, expected } of dtsTestStrings) {
49+
expect(DTS_EXTENSIONS_PATTERN.test(str)).toBe(expected);
4350
}
4451
});

0 commit comments

Comments
 (0)