Skip to content

Commit c5016fb

Browse files
committed
chore: update
1 parent 85b7d06 commit c5016fb

File tree

19 files changed

+154
-17
lines changed

19 files changed

+154
-17
lines changed
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 type 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 type 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+
}
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
}

packages/core/src/config.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { composeAssetConfig } from './asset/assetConfig';
1717
import {
1818
DEFAULT_CONFIG_EXTENSIONS,
1919
DEFAULT_CONFIG_NAME,
20-
ENTRY_EXTENSIONS_PATTERN,
2120
JS_EXTENSIONS_PATTERN,
2221
RSLIB_ENTRY_QUERY,
2322
SWC_HELPERS,
@@ -975,9 +974,7 @@ const composeEntryConfig = async (
975974
});
976975

977976
// Filter the glob resolved entry files based on the allowed extensions
978-
const resolvedEntryFiles = globEntryFiles.filter((file) =>
979-
ENTRY_EXTENSIONS_PATTERN.test(file),
980-
);
977+
const resolvedEntryFiles = globEntryFiles;
981978

982979
if (resolvedEntryFiles.length === 0) {
983980
throw new Error(`Cannot find ${resolvedEntryFiles}`);
@@ -1124,6 +1121,7 @@ const composeBundlelessExternalConfig = (
11241121
styleRedirectPath,
11251122
styleRedirectExtension,
11261123
redirectPath,
1124+
issuer
11271125
);
11281126

11291127
if (cssExternal !== false) {
@@ -1154,7 +1152,10 @@ const composeBundlelessExternalConfig = (
11541152
);
11551153
} else {
11561154
// If it does not match jsExtensionsPattern, we should do nothing, eg: ./foo.png
1157-
return callback();
1155+
resolvedRequest = resolvedRequest.replace(
1156+
/\.[^.]+$/,
1157+
jsExtension,
1158+
);
11581159
}
11591160
} else {
11601161
resolvedRequest = `${resolvedRequest}${jsExtension}`;

packages/core/src/css/LibCssExtractPlugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { RSLIB_CSS_ENTRY_FLAG } from './cssConfig';
33
import {
44
ABSOLUTE_PUBLIC_PATH,
55
AUTO_PUBLIC_PATH,
6+
BASE_URI,
67
SINGLE_DOT_PATH_SEGMENT,
78
} from './libCssExtractLoader';
89
import { getUndoPath } from './utils';
@@ -62,14 +63,15 @@ class LibCssExtractPlugin implements Rspack.RspackPluginInstance {
6263
}
6364
}
6465

65-
replace(ABSOLUTE_PUBLIC_PATH, '');
6666
replace(SINGLE_DOT_PATH_SEGMENT, '.');
6767
const undoPath = getUndoPath(
6868
name,
6969
compilation.outputOptions.path!,
7070
false,
7171
);
72-
replace(AUTO_PUBLIC_PATH, undoPath);
72+
replace(`${ABSOLUTE_PUBLIC_PATH}${AUTO_PUBLIC_PATH}`, undoPath);
73+
replace(ABSOLUTE_PUBLIC_PATH, '');
74+
replace(`${BASE_URI}/`, '');
7375

7476
return replaceSource;
7577
});

0 commit comments

Comments
 (0)