Skip to content

Commit 9b2a12a

Browse files
committed
up
1 parent 2e5544d commit 9b2a12a

File tree

5 files changed

+56
-37
lines changed

5 files changed

+56
-37
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,
@@ -1123,8 +1123,8 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11231123
cssModulesAuto,
11241124
);
11251125
const cssConfig = composeCssConfig(lcp, config.bundle);
1126-
const postEntryChunkConfig = composePostEntryChunkConfig({
1127-
importMetaUrlShim: !!resolvedShims?.cjs?.['import.meta.url'],
1126+
const entryChunkConfig = composeEntryChunkConfig({
1127+
enabledImportMetaUrlShim: !!resolvedShims?.cjs?.['import.meta.url'],
11281128
});
11291129
const dtsConfig = await composeDtsConfig(config, dtsExtension);
11301130
const externalsWarnConfig = composeExternalsWarnConfig(
@@ -1153,7 +1153,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11531153
targetConfig,
11541154
entryConfig,
11551155
cssConfig,
1156-
postEntryChunkConfig,
1156+
entryChunkConfig,
11571157
minifyConfig,
11581158
dtsConfig,
11591159
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/src/plugins/entryModuleLoader.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import type { LoaderDefinition } from '@rspack/core';
33
import {
44
REACT_DIRECTIVE_REGEX,
55
RSLIB_ENTRY_QUERY,
6-
SHEBANG_PREFIX,
6+
SHEBANG_REGEX,
77
} from '../constant';
88

99
const loader: LoaderDefinition = function loader(source) {
1010
let result = source;
1111

1212
if (this.resourceQuery === `?${RSLIB_ENTRY_QUERY}`) {
13-
const rest1 = result.split(os.EOL).slice(1);
14-
if (source.startsWith(SHEBANG_PREFIX)) {
13+
const [firstLine1, ...rest1] = result.split(os.EOL).slice(1);
14+
if (SHEBANG_REGEX.test(firstLine1!)) {
1515
result = rest1.join(os.EOL);
1616
}
1717

18-
const [firstLine, ...rest2] = result.split(os.EOL);
19-
if (REACT_DIRECTIVE_REGEX.test(firstLine!)) {
18+
const [firstLine2, ...rest2] = result.split(os.EOL);
19+
if (REACT_DIRECTIVE_REGEX.test(firstLine2!)) {
2020
result = rest2.join(os.EOL);
2121
}
2222
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
106106
},
107107
"plugins": [
108108
{
109-
"name": "rsbuild:entry",
109+
"name": "rsbuild:lib-entry-chunk",
110110
"setup": [Function],
111111
},
112112
],
@@ -202,11 +202,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
202202
},
203203
{
204204
"plugins": [
205-
PostEntryPlugin {
205+
EntryChunkPlugin {
206206
"enabledImportMetaUrlShim": true,
207207
"importMetaUrlShims": {},
208208
"reactDirectives": {},
209+
"shebangChmod": 493,
209210
"shebangEntries": {},
211+
"shebangInjectedAssets": Set {},
210212
},
211213
],
212214
},
@@ -337,7 +339,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
337339
"setup": [Function],
338340
},
339341
{
340-
"name": "rsbuild:entry",
342+
"name": "rsbuild:lib-entry-chunk",
341343
"setup": [Function],
342344
},
343345
],
@@ -426,11 +428,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
426428
},
427429
{
428430
"plugins": [
429-
PostEntryPlugin {
431+
EntryChunkPlugin {
430432
"enabledImportMetaUrlShim": true,
431433
"importMetaUrlShims": {},
432434
"reactDirectives": {},
435+
"shebangChmod": 493,
433436
"shebangEntries": {},
437+
"shebangInjectedAssets": Set {},
434438
},
435439
],
436440
},
@@ -557,7 +561,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
557561
},
558562
"plugins": [
559563
{
560-
"name": "rsbuild:entry",
564+
"name": "rsbuild:lib-entry-chunk",
561565
"setup": [Function],
562566
},
563567
],
@@ -634,11 +638,13 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
634638
},
635639
{
636640
"plugins": [
637-
PostEntryPlugin {
641+
EntryChunkPlugin {
638642
"enabledImportMetaUrlShim": true,
639643
"importMetaUrlShims": {},
640644
"reactDirectives": {},
645+
"shebangChmod": 493,
641646
"shebangEntries": {},
647+
"shebangInjectedAssets": Set {},
642648
},
643649
],
644650
},

0 commit comments

Comments
 (0)