Skip to content

Commit 8dd17b3

Browse files
authored
refactor: supersede entry query by entry dedicated loader (#896)
1 parent 49728b4 commit 8dd17b3

File tree

23 files changed

+128
-261
lines changed

23 files changed

+128
-261
lines changed

packages/core/src/config.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
DEFAULT_CONFIG_NAME,
2020
DTS_EXTENSIONS_PATTERN,
2121
JS_EXTENSIONS_PATTERN,
22-
RSLIB_ENTRY_QUERY,
2322
SWC_HELPERS,
2423
} from './constant';
2524
import {
@@ -940,9 +939,6 @@ const traverseEntryQuery = (
940939
return newEntry;
941940
};
942941

943-
export const appendEntryQuery = (entries: RsbuildConfigEntry): RsbuildEntry =>
944-
traverseEntryQuery(entries, (item) => `${item}?${RSLIB_ENTRY_QUERY}`);
945-
946942
export const resolveEntryPath = (
947943
entries: RsbuildConfigEntry,
948944
root: string,
@@ -1020,7 +1016,7 @@ const composeEntryConfig = async (
10201016
return {
10211017
entryConfig: {
10221018
source: {
1023-
entry: appendEntryQuery(resolveEntryPath(entries, root)),
1019+
entry: resolveEntryPath(entries, root),
10241020
},
10251021
},
10261022
outBase: null,
@@ -1126,7 +1122,7 @@ const composeEntryConfig = async (
11261122
rspack: {
11271123
entry: async () => {
11281124
const { resolvedEntries } = await scanGlobEntries(false);
1129-
return appendEntryQuery(resolvedEntries);
1125+
return resolvedEntries;
11301126
},
11311127
},
11321128
},

packages/core/src/constant.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export const DEFAULT_CONFIG_EXTENSIONS = [
1010
] as const;
1111

1212
export const SWC_HELPERS = '@swc/helpers';
13-
export const RSLIB_ENTRY_QUERY = '__rslib_entry__';
1413
export const SHEBANG_PREFIX = '#!';
1514
export const SHEBANG_REGEX: RegExp = /#!.*[\s\n\r]*$/;
1615
export const REACT_DIRECTIVE_REGEX: RegExp =

packages/core/src/plugins/EntryChunkPlugin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,9 @@ const entryModuleLoaderRsbuildPlugin = (): RsbuildPlugin => ({
205205
setup(api) {
206206
api.modifyBundlerChain((config, { CHAIN_ID }) => {
207207
config.module
208-
.rule(CHAIN_ID.RULE.JS)
208+
.rule(`Rslib:${CHAIN_ID.RULE.JS}-entry-loader`)
209+
.test(config.module.rule(CHAIN_ID.RULE.JS).get('test'))
210+
.issuer(/^$/)
209211
.use(LOADER_NAME)
210212
.loader(require.resolve('./entryModuleLoader.js'));
211213
});

packages/core/src/plugins/entryModuleLoader.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import type { Rspack } from '@rsbuild/core';
2-
import {
3-
REACT_DIRECTIVE_REGEX,
4-
RSLIB_ENTRY_QUERY,
5-
SHEBANG_REGEX,
6-
} from '../constant';
2+
import { REACT_DIRECTIVE_REGEX, SHEBANG_REGEX } from '../constant';
73

84
function splitFromFirstLine(text: string): [string, string] {
95
const match = text.match(/(\r\n|\n)/);
@@ -17,17 +13,15 @@ function splitFromFirstLine(text: string): [string, string] {
1713
const loader: Rspack.LoaderDefinition = function loader(source) {
1814
let result = source;
1915

20-
if (this.resourceQuery === `?${RSLIB_ENTRY_QUERY}`) {
21-
const [firstLine1, rest] = splitFromFirstLine(result);
16+
const [firstLine1, rest] = splitFromFirstLine(result);
2217

23-
if (SHEBANG_REGEX.test(firstLine1)) {
24-
result = rest;
25-
}
18+
if (SHEBANG_REGEX.test(firstLine1)) {
19+
result = rest;
20+
}
2621

27-
const [firstLine2, rest2] = splitFromFirstLine(result);
28-
if (REACT_DIRECTIVE_REGEX.test(firstLine2)) {
29-
result = rest2;
30-
}
22+
const [firstLine2, rest2] = splitFromFirstLine(result);
23+
if (REACT_DIRECTIVE_REGEX.test(firstLine2)) {
24+
result = rest2;
3125
}
3226

3327
return result;

packages/core/tests/entry.test.ts

Lines changed: 0 additions & 110 deletions
This file was deleted.

packages/plugin-dts/src/dts.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,10 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
191191
declarationDir!,
192192
relativePath,
193193
basename(entrySourcePath),
194-
)
195-
// Remove query in file path, such as RSLIB_ENTRY_QUERY.
196-
.replace(/\?.*$/, '')
197-
.replace(
198-
/\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/,
199-
'.d.ts',
200-
);
194+
).replace(
195+
/\.(js|mjs|jsx|ts|mts|tsx|cjs|cts|cjsx|ctsx|mjsx|mtsx)$/,
196+
'.d.ts',
197+
);
201198
return { name: entryName, path: newPath };
202199
})
203200
.filter(Boolean) as Required<DtsEntry>[];

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ console.log(draft_namespaceObject);
77
`;
88

99
exports[`use asset/source 2`] = `
10-
"const draft_rslib_entry_namespaceObject = "this is a txt file\\nthis is a txt file\\n";
11-
export { draft_rslib_entry_namespaceObject as default };
10+
"const draft_namespaceObject = "this is a txt file\\nthis is a txt file\\n";
11+
export { draft_namespaceObject as default };
1212
"
1313
`;
1414

@@ -19,8 +19,8 @@ console.log(draft_namespaceObject);
1919
`;
2020

2121
exports[`use source.assetInclude 2`] = `
22-
"import draft_rslib_entry_namespaceObject from "../static/assets/draft.txt";
23-
export { draft_rslib_entry_namespaceObject as default };
22+
"import draft_namespaceObject from "../static/assets/draft.txt";
23+
export { draft_namespaceObject as default };
2424
"
2525
`;
2626

@@ -49,8 +49,8 @@ const SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_r
4949
]
5050
})
5151
});
52-
const logo_rslib_entry_ = __rslib_svgr_url__0__;
53-
export { SvgLogo as ReactComponent, logo_rslib_entry_ as default };
52+
const logo = __rslib_svgr_url__0__;
53+
export { SvgLogo as ReactComponent, logo as default };
5454
"
5555
`;
5656

@@ -81,7 +81,7 @@ var __webpack_require__ = {};
8181
var __webpack_exports__ = {};
8282
__webpack_require__.r(__webpack_exports__);
8383
__webpack_require__.d(__webpack_exports__, {
84-
default: ()=>logo_rslib_entry_,
84+
default: ()=>logo,
8585
ReactComponent: ()=>SvgLogo
8686
});
8787
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
@@ -107,7 +107,7 @@ const SvgLogo = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("svg
107107
]
108108
})
109109
});
110-
const logo_rslib_entry_ = require("../static/svg/logo.svg");
110+
const logo = require("../static/svg/logo.svg");
111111
exports.ReactComponent = __webpack_exports__.ReactComponent;
112112
exports["default"] = __webpack_exports__["default"];
113113
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
@@ -144,8 +144,8 @@ const SvgLogo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_r
144144
]
145145
})
146146
});
147-
const logo_rslib_entry_ = SvgLogo;
148-
export { logo_rslib_entry_ as default };
147+
const logo = SvgLogo;
148+
export { logo as default };
149149
"
150150
`;
151151

@@ -176,7 +176,7 @@ var __webpack_require__ = {};
176176
var __webpack_exports__ = {};
177177
__webpack_require__.r(__webpack_exports__);
178178
__webpack_require__.d(__webpack_exports__, {
179-
default: ()=>logo_rslib_entry_
179+
default: ()=>logo
180180
});
181181
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
182182
require("react");
@@ -201,7 +201,7 @@ const SvgLogo = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("svg
201201
]
202202
})
203203
});
204-
const logo_rslib_entry_ = SvgLogo;
204+
const logo = SvgLogo;
205205
exports["default"] = __webpack_exports__["default"];
206206
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
207207
"default"
@@ -213,15 +213,15 @@ Object.defineProperty(exports, '__esModule', {
213213
`;
214214

215215
exports[`use svgr > should only contain url default export 1`] = `
216-
"import logo2_rslib_entry_namespaceObject from "../static/svg/logo2.svg";
217-
export { logo2_rslib_entry_namespaceObject as default };
216+
"import logo2_namespaceObject from "../static/svg/logo2.svg";
217+
export { logo2_namespaceObject as default };
218218
"
219219
`;
220220

221221
exports[`use svgr > should only contain url default export 2`] = `
222222
""use strict";
223223
var __webpack_modules__ = {
224-
"./src/assets/logo2.svg?__rslib_entry__": function(module) {
224+
"./src/assets/logo2.svg": function(module) {
225225
module.exports = require("../static/svg/logo2.svg");
226226
}
227227
};
@@ -235,7 +235,7 @@ function __webpack_require__(moduleId) {
235235
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
236236
return module.exports;
237237
}
238-
var __webpack_exports__ = __webpack_require__("./src/assets/logo2.svg?__rslib_entry__");
238+
var __webpack_exports__ = __webpack_require__("./src/assets/logo2.svg");
239239
exports["default"] = __webpack_exports__["default"];
240240
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
241241
"default"

0 commit comments

Comments
 (0)