Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default defineConfig({
entry: {
index: './src/index.ts',
libCssExtractLoader: './src/css/libCssExtractLoader.ts',
entryModuleLoader: './src/plugins/entryModuleLoader.ts',
},
define: {
RSLIB_VERSION: JSON.stringify(require('./package.json').version),
Expand Down
68 changes: 58 additions & 10 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
DEFAULT_CONFIG_NAME,
ENTRY_EXTENSIONS_PATTERN,
JS_EXTENSIONS_PATTERN,
RSLIB_ENTRY_QUERY,
SWC_HELPERS,
} from './constant';
import {
Expand All @@ -23,6 +24,7 @@ import {
cssExternalHandler,
isCssGlobalFile,
} from './css/cssConfig';
import { composeEntryChunkConfig } from './plugins/EntryChunkPlugin';
import {
pluginCjsImportMetaUrlShim,
pluginEsmRequireShim,
Expand Down Expand Up @@ -598,7 +600,14 @@ const composeFormatConfig = ({
}
};

const composeShimsConfig = (format: Format, shims?: Shims): RsbuildConfig => {
type DeepRequired<T> = Required<{
[K in keyof T]: T[K] extends Required<T[K]> ? T[K] : DeepRequired<T[K]>;
}>;

const composeShimsConfig = (
format: Format,
shims?: Shims,
): { rsbuildConfig: RsbuildConfig; enabledShims: DeepRequired<Shims> } => {
const resolvedShims = {
cjs: {
'import.meta.url': shims?.cjs?.['import.meta.url'] ?? true,
Expand All @@ -610,9 +619,27 @@ const composeShimsConfig = (format: Format, shims?: Shims): RsbuildConfig => {
},
};

const enabledShims = {
cjs:
format === 'cjs'
? resolvedShims.cjs
: {
'import.meta.url': false,
},
esm:
format === 'esm'
? resolvedShims.esm
: {
__filename: false,
__dirname: false,
require: false,
},
};

let rsbuildConfig: RsbuildConfig = {};
switch (format) {
case 'esm':
return {
case 'esm': {
rsbuildConfig = {
tools: {
rspack: {
node: {
Expand All @@ -626,19 +653,23 @@ const composeShimsConfig = (format: Format, shims?: Shims): RsbuildConfig => {
Boolean,
),
};
break;
}
case 'cjs':
return {
rsbuildConfig = {
plugins: [
resolvedShims.cjs['import.meta.url'] && pluginCjsImportMetaUrlShim(),
].filter(Boolean),
};
break;
case 'umd':
return {};
case 'mf':
return {};
break;
default:
throw new Error(`Unsupported format: ${format}`);
}

return { rsbuildConfig, enabledShims };
};

export const composeModuleImportWarn = (request: string): string => {
Expand Down Expand Up @@ -746,6 +777,16 @@ const composeSyntaxConfig = (
};
};

const appendEntryQuery = (
entry: NonNullable<RsbuildConfig['source']>['entry'],
): NonNullable<RsbuildConfig['source']>['entry'] => {
const newEntry: Record<string, string> = {};
for (const key in entry) {
newEntry[key] = `${entry[key]}?${RSLIB_ENTRY_QUERY}`;
}
return newEntry;
};

const composeEntryConfig = async (
entries: NonNullable<RsbuildConfig['source']>['entry'],
bundle: LibConfig['bundle'],
Expand All @@ -760,7 +801,7 @@ const composeEntryConfig = async (
return {
entryConfig: {
source: {
entry: entries,
entry: appendEntryQuery(entries),
},
},
lcp: null,
Expand Down Expand Up @@ -836,7 +877,7 @@ const composeEntryConfig = async (
const lcp = await calcLongestCommonPath(Object.values(resolvedEntries));
const entryConfig: RsbuildConfig = {
source: {
entry: resolvedEntries,
entry: appendEntryQuery(resolvedEntries),
},
};

Expand Down Expand Up @@ -1000,7 +1041,7 @@ const composeTargetConfig = (
const composeExternalHelpersConfig = (
externalHelpers: boolean,
pkgJson?: PkgJson,
): RsbuildConfig => {
) => {
let defaultConfig = {
tools: {
swc: {
Expand Down Expand Up @@ -1057,7 +1098,10 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
redirect = {},
umdName,
} = config;
const shimsConfig = composeShimsConfig(format!, shims);
const { rsbuildConfig: shimsConfig, enabledShims } = composeShimsConfig(
format!,
shims,
);
const formatConfig = composeFormatConfig({
format: format!,
pkgJson: pkgJson!,
Expand Down Expand Up @@ -1100,6 +1144,9 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
cssModulesAuto,
);
const cssConfig = composeCssConfig(lcp, config.bundle);
const entryChunkConfig = composeEntryChunkConfig({
enabledImportMetaUrlShim: enabledShims.cjs['import.meta.url'],
});
const dtsConfig = await composeDtsConfig(config, dtsExtension);
const externalsWarnConfig = composeExternalsWarnConfig(
format!,
Expand Down Expand Up @@ -1127,6 +1174,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
targetConfig,
entryConfig,
cssConfig,
entryChunkConfig,
minifyConfig,
dtsConfig,
bannerFooterConfig,
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export const DEFAULT_CONFIG_EXTENSIONS = [
] as const;

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

export const JS_EXTENSIONS: string[] = [
'js',
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/css/RemoveCssExtractAssetPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const pluginName = 'REMOVE_CSS_EXTRACT_ASSET_PLUGIN';
type Options = {
include: RegExp;
};

class RemoveCssExtractAssetPlugin implements RspackPluginInstance {
readonly name: string = pluginName;
options: Options;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/css/cssConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export function cssExternalHandler(
return false;
}

const pluginName = 'rsbuild:lib-css';
const PLUGIN_NAME = 'rsbuild:lib-css';

const pluginLibCss = (rootDir: string): RsbuildPlugin => ({
name: pluginName,
name: PLUGIN_NAME,
setup(api) {
api.modifyBundlerChain((config, { CHAIN_ID }) => {
let isUsingCssExtract = false;
Expand Down
Loading
Loading