Skip to content

Commit accf40c

Browse files
authored
feat: add EntryChunkPlugin to handle shebang and shims (#399)
1 parent 096c435 commit accf40c

File tree

33 files changed

+801
-69
lines changed

33 files changed

+801
-69
lines changed

packages/core/rslib.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default defineConfig({
1515
entry: {
1616
index: './src/index.ts',
1717
libCssExtractLoader: './src/css/libCssExtractLoader.ts',
18+
entryModuleLoader: './src/plugins/entryModuleLoader.ts',
1819
},
1920
define: {
2021
RSLIB_VERSION: JSON.stringify(require('./package.json').version),

packages/core/src/config.ts

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
DEFAULT_CONFIG_NAME,
1515
ENTRY_EXTENSIONS_PATTERN,
1616
JS_EXTENSIONS_PATTERN,
17+
RSLIB_ENTRY_QUERY,
1718
SWC_HELPERS,
1819
} from './constant';
1920
import {
@@ -23,13 +24,15 @@ import {
2324
cssExternalHandler,
2425
isCssGlobalFile,
2526
} from './css/cssConfig';
27+
import { composeEntryChunkConfig } from './plugins/EntryChunkPlugin';
2628
import {
2729
pluginCjsImportMetaUrlShim,
2830
pluginEsmRequireShim,
2931
} from './plugins/shims';
3032
import type {
3133
AutoExternal,
3234
BannerAndFooter,
35+
DeepRequired,
3336
Format,
3437
LibConfig,
3538
LibOnlyConfig,
@@ -598,7 +601,10 @@ const composeFormatConfig = ({
598601
}
599602
};
600603

601-
const composeShimsConfig = (format: Format, shims?: Shims): RsbuildConfig => {
604+
const composeShimsConfig = (
605+
format: Format,
606+
shims?: Shims,
607+
): { rsbuildConfig: RsbuildConfig; enabledShims: DeepRequired<Shims> } => {
602608
const resolvedShims = {
603609
cjs: {
604610
'import.meta.url': shims?.cjs?.['import.meta.url'] ?? true,
@@ -610,9 +616,27 @@ const composeShimsConfig = (format: Format, shims?: Shims): RsbuildConfig => {
610616
},
611617
};
612618

619+
const enabledShims = {
620+
cjs:
621+
format === 'cjs'
622+
? resolvedShims.cjs
623+
: {
624+
'import.meta.url': false,
625+
},
626+
esm:
627+
format === 'esm'
628+
? resolvedShims.esm
629+
: {
630+
__filename: false,
631+
__dirname: false,
632+
require: false,
633+
},
634+
};
635+
636+
let rsbuildConfig: RsbuildConfig = {};
613637
switch (format) {
614-
case 'esm':
615-
return {
638+
case 'esm': {
639+
rsbuildConfig = {
616640
tools: {
617641
rspack: {
618642
node: {
@@ -626,19 +650,23 @@ const composeShimsConfig = (format: Format, shims?: Shims): RsbuildConfig => {
626650
Boolean,
627651
),
628652
};
653+
break;
654+
}
629655
case 'cjs':
630-
return {
656+
rsbuildConfig = {
631657
plugins: [
632658
resolvedShims.cjs['import.meta.url'] && pluginCjsImportMetaUrlShim(),
633659
].filter(Boolean),
634660
};
661+
break;
635662
case 'umd':
636-
return {};
637663
case 'mf':
638-
return {};
664+
break;
639665
default:
640666
throw new Error(`Unsupported format: ${format}`);
641667
}
668+
669+
return { rsbuildConfig, enabledShims };
642670
};
643671

644672
export const composeModuleImportWarn = (request: string): string => {
@@ -746,6 +774,16 @@ const composeSyntaxConfig = (
746774
};
747775
};
748776

777+
const appendEntryQuery = (
778+
entry: NonNullable<RsbuildConfig['source']>['entry'],
779+
): NonNullable<RsbuildConfig['source']>['entry'] => {
780+
const newEntry: Record<string, string> = {};
781+
for (const key in entry) {
782+
newEntry[key] = `${entry[key]}?${RSLIB_ENTRY_QUERY}`;
783+
}
784+
return newEntry;
785+
};
786+
749787
const composeEntryConfig = async (
750788
entries: NonNullable<RsbuildConfig['source']>['entry'],
751789
bundle: LibConfig['bundle'],
@@ -760,7 +798,7 @@ const composeEntryConfig = async (
760798
return {
761799
entryConfig: {
762800
source: {
763-
entry: entries,
801+
entry: appendEntryQuery(entries),
764802
},
765803
},
766804
lcp: null,
@@ -836,7 +874,7 @@ const composeEntryConfig = async (
836874
const lcp = await calcLongestCommonPath(Object.values(resolvedEntries));
837875
const entryConfig: RsbuildConfig = {
838876
source: {
839-
entry: resolvedEntries,
877+
entry: appendEntryQuery(resolvedEntries),
840878
},
841879
};
842880

@@ -1000,7 +1038,7 @@ const composeTargetConfig = (
10001038
const composeExternalHelpersConfig = (
10011039
externalHelpers: boolean,
10021040
pkgJson?: PkgJson,
1003-
): RsbuildConfig => {
1041+
) => {
10041042
let defaultConfig = {
10051043
tools: {
10061044
swc: {
@@ -1057,7 +1095,10 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
10571095
redirect = {},
10581096
umdName,
10591097
} = config;
1060-
const shimsConfig = composeShimsConfig(format!, shims);
1098+
const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig(
1099+
format!,
1100+
shims,
1101+
);
10611102
const formatConfig = composeFormatConfig({
10621103
format: format!,
10631104
pkgJson: pkgJson!,
@@ -1100,6 +1141,9 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11001141
cssModulesAuto,
11011142
);
11021143
const cssConfig = composeCssConfig(lcp, config.bundle);
1144+
const entryChunkConfig = composeEntryChunkConfig({
1145+
enabledImportMetaUrlShim: enabledShims.cjs['import.meta.url'],
1146+
});
11031147
const dtsConfig = await composeDtsConfig(config, dtsExtension);
11041148
const externalsWarnConfig = composeExternalsWarnConfig(
11051149
format!,
@@ -1127,6 +1171,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11271171
targetConfig,
11281172
entryConfig,
11291173
cssConfig,
1174+
entryChunkConfig,
11301175
minifyConfig,
11311176
dtsConfig,
11321177
bannerFooterConfig,

packages/core/src/constant.ts

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

1212
export const SWC_HELPERS = '@swc/helpers';
13+
export const RSLIB_ENTRY_QUERY = '__rslib_entry__';
14+
export const SHEBANG_PREFIX = '#!';
15+
export const SHEBANG_REGEX: RegExp = /#!.*[\s\n\r]*$/;
16+
export const REACT_DIRECTIVE_REGEX: RegExp =
17+
/^['"]use (client|server)['"](;?)[\s\n\r]*$/;
1318

1419
export const JS_EXTENSIONS: string[] = [
1520
'js',

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/css/cssConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ export function cssExternalHandler(
110110
return false;
111111
}
112112

113-
const pluginName = 'rsbuild:lib-css';
113+
const PLUGIN_NAME = 'rsbuild:lib-css';
114114

115115
const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
116-
name: pluginName,
116+
name: PLUGIN_NAME,
117117
setup(api) {
118118
api.modifyBundlerChain((config, { CHAIN_ID }) => {
119119
let isUsingCssExtract = false;

0 commit comments

Comments
 (0)