Skip to content

Commit fe3d503

Browse files
committed
up
1 parent 6894beb commit fe3d503

File tree

4 files changed

+51
-32
lines changed

4 files changed

+51
-32
lines changed

packages/core/src/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
cssExternalHandler,
2525
isCssGlobalFile,
2626
} from './css/cssConfig';
27-
import { composePostEntryChunkConfig } from './plugins/PostEntryChunkPlugin';
27+
import { composeEntryChunkConfig } from './plugins/EntryChunkPlugin';
2828
import {
2929
pluginCjsImportMetaUrlShim,
3030
pluginEsmRequireShim,
@@ -1107,8 +1107,8 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11071107
cssModulesAuto,
11081108
);
11091109
const cssConfig = composeCssConfig(lcp, config.bundle);
1110-
const postEntryChunkConfig = composePostEntryChunkConfig({
1111-
importMetaUrlShim: !!resolvedShims?.cjs?.['import.meta.url'],
1110+
const entryChunkConfig = composeEntryChunkConfig({
1111+
enabledImportMetaUrlShim: !!resolvedShims?.cjs?.['import.meta.url'],
11121112
});
11131113
const dtsConfig = await composeDtsConfig(config, dtsExtension);
11141114
const externalsWarnConfig = composeExternalsWarnConfig(
@@ -1137,7 +1137,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11371137
targetConfig,
11381138
entryConfig,
11391139
cssConfig,
1140-
postEntryChunkConfig,
1140+
entryChunkConfig,
11411141
minifyConfig,
11421142
dtsConfig,
11431143
bannerFooterConfig,

packages/core/src/css/RemoveCssExtractAssetPlugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const pluginName = 'REMOVE_CSS_EXTRACT_ASSET_PLUGIN';
55
type Options = {
66
include: RegExp;
77
};
8+
89
class RemoveCssExtractAssetPlugin implements RspackPluginInstance {
910
readonly name: string = pluginName;
1011
options: Options;

packages/core/src/plugins/PostEntryChunkPlugin.ts renamed to packages/core/src/plugins/EntryChunkPlugin.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { chmodSync } from 'node:fs';
12
import { createRequire } from 'node:module';
23
import os from 'node:os';
34
import {
@@ -15,7 +16,8 @@ import {
1516
import { importMetaUrlShim } from './shims';
1617
const require = createRequire(import.meta.url);
1718

18-
const PLUGIN_NAME = 'rsbuild:entry';
19+
const PLUGIN_NAME = 'rsbuild:lib-entry-chunk';
20+
const LOADER_NAME = 'rsbuild:lib-entry-module';
1921

2022
const matchFirstLine = (source: string, regex: RegExp) => {
2123
const [firstLine] = source.split(os.EOL);
@@ -30,19 +32,23 @@ const matchFirstLine = (source: string, regex: RegExp) => {
3032
return matched[0];
3133
};
3234

33-
class PostEntryPlugin {
34-
private enabledImportMetaUrlShim: boolean;
35-
private shebangEntries: Record<string, string> = {};
35+
class EntryChunkPlugin {
3636
private reactDirectives: Record<string, string> = {};
37+
38+
private shebangChmod = 0o755;
39+
private shebangEntries: Record<string, string> = {};
40+
private shebangInjectedAssets: Set<string> = new Set();
41+
42+
private enabledImportMetaUrlShim: boolean;
3743
private importMetaUrlShims: Record<string, { startsWithUseStrict: boolean }> =
3844
{};
3945

4046
constructor({
41-
importMetaUrlShim = true,
47+
enabledImportMetaUrlShim = true,
4248
}: {
43-
importMetaUrlShim: boolean;
49+
enabledImportMetaUrlShim: boolean;
4450
}) {
45-
this.enabledImportMetaUrlShim = importMetaUrlShim;
51+
this.enabledImportMetaUrlShim = enabledImportMetaUrlShim;
4652
}
4753

4854
apply(compiler: Rspack.Compiler) {
@@ -136,7 +142,7 @@ class PostEntryPlugin {
136142
replaceSource.replace(
137143
0,
138144
11, // 'use strict;'.length,
139-
`"use strict";\n${importMetaUrlShim}`,
145+
`"use strict";${os.EOL}${importMetaUrlShim}`,
140146
);
141147
} else {
142148
replaceSource.insert(0, importMetaUrlShim);
@@ -168,12 +174,13 @@ class PostEntryPlugin {
168174
const replaceSource = new rspack.sources.ReplaceSource(old);
169175
// Shebang
170176
if (shebangValue) {
171-
replaceSource.insert(0, `${shebangValue}\n`);
177+
replaceSource.insert(0, `${shebangValue}${os.EOL}`);
178+
this.shebangInjectedAssets.add(name);
172179
}
173180

174181
// React directives
175182
if (reactDirectiveValue) {
176-
replaceSource.insert(0, `${reactDirectiveValue}\n`);
183+
replaceSource.insert(0, `${reactDirectiveValue}${os.EOL}`);
177184
}
178185

179186
return replaceSource;
@@ -183,34 +190,39 @@ class PostEntryPlugin {
183190
},
184191
);
185192
});
193+
194+
compiler.hooks.assetEmitted.tap(PLUGIN_NAME, (file, { targetPath }) => {
195+
if (this.shebangInjectedAssets.has(file)) {
196+
chmodSync(targetPath, this.shebangChmod);
197+
}
198+
});
186199
}
187200
}
188201

189-
const entryModuleLoaderPlugin = (): RsbuildPlugin => ({
202+
const entryModuleLoaderRsbuildPlugin = (): RsbuildPlugin => ({
190203
name: PLUGIN_NAME,
191204
setup(api) {
192205
api.modifyBundlerChain((config, { CHAIN_ID }) => {
193-
const rule = config.module.rule(CHAIN_ID.RULE.JS);
194-
rule
195-
.use('shebang')
196-
.loader(require.resolve('./entryModuleLoader.js'))
197-
.options({});
206+
config.module
207+
.rule(CHAIN_ID.RULE.JS)
208+
.use(LOADER_NAME)
209+
.loader(require.resolve('./entryModuleLoader.js'));
198210
});
199211
},
200212
});
201213

202-
export const composePostEntryChunkConfig = ({
203-
importMetaUrlShim,
214+
export const composeEntryChunkConfig = ({
215+
enabledImportMetaUrlShim,
204216
}: {
205-
importMetaUrlShim: boolean;
217+
enabledImportMetaUrlShim: boolean;
206218
}): RsbuildConfig => {
207219
return {
208-
plugins: [entryModuleLoaderPlugin()],
220+
plugins: [entryModuleLoaderRsbuildPlugin()],
209221
tools: {
210222
rspack: {
211223
plugins: [
212-
new PostEntryPlugin({
213-
importMetaUrlShim: importMetaUrlShim,
224+
new EntryChunkPlugin({
225+
enabledImportMetaUrlShim,
214226
}),
215227
],
216228
},

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
5555
},
5656
"plugins": [
5757
{
58-
"name": "rsbuild:entry",
58+
"name": "rsbuild:lib-entry-chunk",
5959
"setup": [Function],
6060
},
6161
],
@@ -151,11 +151,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
151151
},
152152
{
153153
"plugins": [
154-
PostEntryPlugin {
154+
EntryChunkPlugin {
155155
"enabledImportMetaUrlShim": true,
156156
"importMetaUrlShims": {},
157157
"reactDirectives": {},
158+
"shebangChmod": 493,
158159
"shebangEntries": {},
160+
"shebangInjectedAssets": Set {},
159161
},
160162
],
161163
},
@@ -233,7 +235,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
233235
"setup": [Function],
234236
},
235237
{
236-
"name": "rsbuild:entry",
238+
"name": "rsbuild:lib-entry-chunk",
237239
"setup": [Function],
238240
},
239241
],
@@ -322,11 +324,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
322324
},
323325
{
324326
"plugins": [
325-
PostEntryPlugin {
327+
EntryChunkPlugin {
326328
"enabledImportMetaUrlShim": true,
327329
"importMetaUrlShims": {},
328330
"reactDirectives": {},
331+
"shebangChmod": 493,
329332
"shebangEntries": {},
333+
"shebangInjectedAssets": Set {},
330334
},
331335
],
332336
},
@@ -400,7 +404,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
400404
},
401405
"plugins": [
402406
{
403-
"name": "rsbuild:entry",
407+
"name": "rsbuild:lib-entry-chunk",
404408
"setup": [Function],
405409
},
406410
],
@@ -477,11 +481,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
477481
},
478482
{
479483
"plugins": [
480-
PostEntryPlugin {
484+
EntryChunkPlugin {
481485
"enabledImportMetaUrlShim": true,
482486
"importMetaUrlShims": {},
483487
"reactDirectives": {},
488+
"shebangChmod": 493,
484489
"shebangEntries": {},
490+
"shebangInjectedAssets": Set {},
485491
},
486492
],
487493
},

0 commit comments

Comments
 (0)