Skip to content

Commit 8214b1b

Browse files
committed
chore: update
1 parent 5a9d022 commit 8214b1b

File tree

14 files changed

+164
-14
lines changed

14 files changed

+164
-14
lines changed

examples/react-component-bundle-false/rslib.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { type LibConfig, defineConfig } from '@rslib/core';
55
export default defineConfig({
66
source: {
77
entry: {
8-
index: ['./src/**', '!./src/env.d.ts'],
8+
index: [
9+
'./src/**',
10+
'!./src/env.d.ts'
11+
],
912
},
1013
},
1114
lib: [
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.button {
2-
background: yellow;
2+
background: url('../../assets/logo.svg') no-repeat;
33
}

examples/react-component-bundle-false/src/components/CounterButton/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import styles from './index.module.scss';
3+
import logo from '../../assets/logo.svg'
34

45
interface CounterButtonProps {
56
onClick: () => void;
@@ -11,6 +12,7 @@ export const CounterButton: React.FC<CounterButtonProps> = ({
1112
label,
1213
}) => (
1314
<button type="button" className={styles.button} onClick={onClick}>
15+
<img src={logo} alt='react'/>
1416
{label}
1517
</button>
1618
);

examples/react-component-bundle-false/src/env.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ declare module '*.module.scss' {
22
const classes: { [key: string]: string };
33
export default classes;
44
}
5+
6+
declare module '*.svg' {
7+
const url: string;
8+
export default url;
9+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.button {
2-
background: yellow;
2+
background: url('../../assets/logo.svg') no-repeat;
33
}

examples/react-component-bundle/src/components/CounterButton/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import styles from './index.module.scss';
3+
import logo from '../../assets/logo.svg'
34

45
interface CounterButtonProps {
56
onClick: () => void;
@@ -11,6 +12,7 @@ export const CounterButton: React.FC<CounterButtonProps> = ({
1112
label,
1213
}) => (
1314
<button type="button" className={styles.button} onClick={onClick}>
15+
<img src={logo} alt='react'/>
1416
{label}
1517
</button>
1618
);

examples/react-component-bundle/src/env.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ declare module '*.module.scss' {
22
const classes: { [key: string]: string };
33
export default classes;
44
}
5+
6+
declare module '*.svg' {
7+
const url: string;
8+
export default url;
9+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464
},
6565
"pnpm": {
6666
"overrides": {
67-
"zx>@types/node": "-"
67+
"zx>@types/node": "-",
68+
"@rspack/core": "/Users/bytedance/Documents/codes/rspack/packages/rspack"
6869
}
6970
}
7071
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { type Rspack, rspack } from '@rsbuild/core';
2+
import { getUndoPath } from '../css/utils';
3+
4+
type Options = {
5+
}
6+
/**
7+
* these codes is written according to
8+
* https://github.com/web-infra-dev/rspack/blob/61f0cd2b4e313445a9d3329ca71240e99edfb352/crates/rspack_plugin_asset/src/lib.rs#L531
9+
*/
10+
const pattern: RegExp = /__webpack_require__\.p\s\+\s["'](.+)["']/g;
11+
function extractAssetFilenames (content: string): string[] {
12+
console.log([...content.matchAll(pattern)])
13+
return [...content.matchAll(pattern)].map(i => {
14+
return i?.[1]
15+
}).filter(Boolean) as string[];
16+
}
17+
18+
const RSLIB_NAMESPACE_OBJECT = `__rslib_asset__`;
19+
20+
const esmSingleFileTemplate = (url: string) => `import ${RSLIB_NAMESPACE_OBJECT} from '${url}';
21+
export default ${RSLIB_NAMESPACE_OBJECT};`;
22+
23+
const cjsSingleFileTemplate = (url: string) => `module.exports = require('${url}');`;
24+
25+
const pluginName = 'LIB_ASSET_EXTRACT_PLUGIN';
26+
27+
class LibAssetExtractPlugin implements Rspack.RspackPluginInstance {
28+
readonly name: string = pluginName;
29+
options: Options;
30+
constructor(options?: Options) {
31+
this.options = options ?? {};
32+
}
33+
34+
apply(compiler: Rspack.Compiler): void {
35+
compiler.hooks.make.tap(pluginName, (compilation) => {
36+
compilation.hooks.processAssets.tap(pluginName, (assets) => {
37+
const chunkAsset = Object.keys(assets).filter((name) =>
38+
/js/.test(name),
39+
);
40+
for (const name of chunkAsset) {
41+
const isEsmFormat = compilation.options.output.module;
42+
const undoPath = getUndoPath(
43+
name,
44+
compilation.outputOptions.path!,
45+
true,
46+
);
47+
compilation.updateAsset(name, (old) => {
48+
const oldSource = old.source().toString();
49+
const assetFilenames = extractAssetFilenames(oldSource);
50+
51+
if (assetFilenames.length === 1) {
52+
const assetFilename = assetFilenames[0];
53+
let newSource: string = '';
54+
const url = `${undoPath}${assetFilename}`;
55+
56+
if(isEsmFormat) {
57+
newSource = esmSingleFileTemplate(url);
58+
} else {
59+
newSource = cjsSingleFileTemplate(url);
60+
}
61+
return new rspack.sources.RawSource(newSource);
62+
} else {
63+
const newSource = new rspack.sources.ReplaceSource(old);
64+
assetFilenames.forEach(() => {
65+
})
66+
return newSource;
67+
}
68+
69+
});
70+
}
71+
})
72+
})}
73+
}
74+
export { LibAssetExtractPlugin};

packages/core/src/asset/assetConfig.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { EnvironmentConfig } from '@rsbuild/core';
22
import type { Format } from '../types';
3+
import { LibAssetExtractPlugin } from './LibAssetExtractPlugin';
34

45
// TODO: asset config document
56
export const composeAssetConfig = (
@@ -11,15 +12,27 @@ export const composeAssetConfig = (
1112
return {
1213
output: {
1314
dataUriLimit: 0, // default: no inline asset
14-
// assetPrefix: 'auto', // TODO: will turn on this with js support together in the future
15+
assetPrefix: 'auto', // TODO: will turn on this with js support together in the future
16+
},
17+
tools: {
18+
rspack: {
19+
plugins: [new LibAssetExtractPlugin()]
20+
21+
},
1522
},
1623
};
1724
}
18-
25+
// TODO: bundleless
1926
return {
2027
output: {
2128
dataUriLimit: 0, // default: no inline asset
22-
// assetPrefix: 'auto', // TODO: will turn on this with js support together in the future
29+
assetPrefix: 'auto',
30+
},
31+
tools: {
32+
rspack: {
33+
plugins: [new LibAssetExtractPlugin()]
34+
35+
},
2336
},
2437
};
2538
}

0 commit comments

Comments
 (0)