Skip to content

Commit b1064e3

Browse files
committed
chore: update
1 parent 6358cde commit b1064e3

File tree

4 files changed

+14
-52
lines changed

4 files changed

+14
-52
lines changed

packages/core/src/asset/LibAssetExtractPlugin.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ function extractAssetFilenames(content: string): string[] {
2626
.filter(Boolean) as string[];
2727
}
2828

29-
// 2. bundle: concatenated
29+
// 2. bundle
30+
// remove __webpack_require__.p and __webpack_require__.g
31+
const WEBPACK_REQUIRE_G_IIFE =
32+
/\(\(\)\s*=>\s*{.*?__webpack_require__\.g\s\=.*?}\)\(\);/gms;
33+
const WEBPACK_REQUIRE_P_IIFE =
34+
/\(\(\)\s*=>\s*{.*?__webpack_require__\.p\s\=.*?}\)\(\);/gms;
35+
36+
// 2.1 bundle: concatenated
3037
const CONCATENATED_PATTERN: RegExp =
3138
/(const|var)\s(\w+)\s=\s\(?__webpack_require__\.p\s\+\s["'](.+)["']\)?/g;
3239
const concatenatedEsmReplaceTemplate = (variableName: string, url: string) =>
@@ -37,7 +44,7 @@ const concatenatedCjsReplaceTemplate = (
3744
url: string,
3845
) => `${declarationKind} ${variableName} = require('${url}');`;
3946

40-
// 3. bundle: not concatenated, in __webpack_require__.m
47+
// 2.2 bundle: not concatenated, in __webpack_require__.m
4148
const NOT_CONCATENATED_PATTERN: RegExp =
4249
/module\.exports = __webpack_require__\.p\s\+\s["'](.+)["']/g;
4350
const nonConcatenatedReplaceTemplate = (url: string) =>
@@ -115,6 +122,9 @@ class LibAssetExtractPlugin implements Rspack.RspackPluginInstance {
115122
);
116123
}
117124
}
125+
replace(WEBPACK_REQUIRE_G_IIFE, () => '');
126+
replace(WEBPACK_REQUIRE_P_IIFE, () => '');
127+
118128
replace(CONCATENATED_PATTERN, (match) => {
119129
const declarationKind = match[1];
120130
const variableName = match[2];

packages/core/src/asset/assetConfig.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const pluginLibAsset = ({ bundle }: { bundle: boolean }): RsbuildPlugin => ({
2424
.oneOf(CHAIN_ID.RULE.SVG);
2525
rule.issuer([]);
2626
}
27-
2827
config
2928
.plugin(LibAssetExtractPlugin.name)
3029
.use(LibAssetExtractPlugin, [{ bundle, isUsingSvgr }]);

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

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,6 @@
33
exports[`use svgr 1`] = `
44
"import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__ from "react/jsx-runtime";
55
import "react";
6-
var __webpack_require__ = {};
7-
(()=>{
8-
__webpack_require__.g = function() {
9-
if ('object' == typeof globalThis) return globalThis;
10-
try {
11-
return this || new Function('return this')();
12-
} catch (e) {
13-
if ('object' == typeof window) return window;
14-
}
15-
}();
16-
})();
17-
(()=>{
18-
var scriptUrl;
19-
if ("string" == typeof import.meta.url) scriptUrl = import.meta.url;
20-
if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");
21-
scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\\?.*$/, "").replace(/\\/[^\\/]+$/, "/");
22-
__webpack_require__.p = scriptUrl;
23-
})();
246
const SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)("svg", {
257
xmlns: "http://www.w3.org/2000/svg",
268
viewBox: "0 0 841.9 595.3",
@@ -73,35 +55,6 @@ console.log(logo);
7355

7456
exports[`use svgr 2`] = `
7557
""use strict";
76-
var __webpack_require__ = {};
77-
(()=>{
78-
__webpack_require__.g = function() {
79-
if ('object' == typeof globalThis) return globalThis;
80-
try {
81-
return this || new Function('return this')();
82-
} catch (e) {
83-
if ('object' == typeof window) return window;
84-
}
85-
}();
86-
})();
87-
(()=>{
88-
var scriptUrl;
89-
if (__webpack_require__.g.importScripts) scriptUrl = __webpack_require__.g.location + "";
90-
var document = __webpack_require__.g.document;
91-
if (!scriptUrl && document) {
92-
if (document.currentScript && 'SCRIPT' === document.currentScript.tagName.toUpperCase()) scriptUrl = document.currentScript.src;
93-
if (!scriptUrl) {
94-
var scripts = document.getElementsByTagName("script");
95-
if (scripts.length) {
96-
var i = scripts.length - 1;
97-
while(i > -1 && (!scriptUrl || !/^http(s?):/.test(scriptUrl)))scriptUrl = scripts[i--].src;
98-
}
99-
}
100-
}
101-
if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");
102-
scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\\?.*$/, "").replace(/\\/[^\\/]+$/, "/");
103-
__webpack_require__.p = scriptUrl;
104-
})();
10558
var __webpack_exports__ = {};
10659
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
10760
require("react");

tests/integration/asset/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ test('set the assets output path', async () => {
137137
test('set the assets public path', async () => {
138138
const fixturePath = join(__dirname, 'public-path');
139139
const { contents } = await buildAndGetResults({ fixturePath });
140-
140+
141+
// umd should preserve '__webpack_require__.p'
141142
const { content: indexUmdJs } = queryContent(contents.umd!, /index\.js/);
142143

143-
// umd
144144
expect(indexUmdJs).toContain(
145145
'__webpack_require__.p = "/public/path/bundleless/";',
146146
);

0 commit comments

Comments
 (0)