Skip to content

Commit 3a7c07b

Browse files
author
nyqykk
committed
chore: sync code
2 parents 5a2de62 + fc152dd commit 3a7c07b

File tree

56 files changed

+612
-175
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+612
-175
lines changed

.changeset/proud-sheep-end.md

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

.npmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
registry = 'https://registry.npmjs.org/'
22
strict-peer-dependencies=false
3-
hoist-patterns[]=[]
3+
hoist-patterns[]=[]

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"test": "pnpm run test:unit && pnpm run test:integration && pnpm run test:e2e",
2222
"test:benchmark": "cd ./tests && pnpm run test:benchmark",
2323
"test:e2e": "pnpm run build:examples && cd tests && pnpm run test:e2e",
24-
"test:integration": "vitest run --project integration",
25-
"test:integration:watch": "vitest --project integration",
24+
"test:integration": "cross-env NODE_OPTIONS='--experimental-vm-modules' vitest run --project integration",
25+
"test:integration:watch": "cross-env NODE_OPTIONS='--experimental-vm-modules' vitest --project integration",
2626
"test:unit": "vitest run --project unit*",
2727
"test:unit:watch": "vitest --project unit*",
2828
"testu": "pnpm run test:unit -u && pnpm run test:integration -u",

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rslib/core",
3-
"version": "0.0.11",
3+
"version": "0.0.12",
44
"description": "The Rspack-based library build tool.",
55
"homepage": "https://lib.rsbuild.dev",
66
"bugs": {

packages/core/src/config.ts

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import {
2424
cssExternalHandler,
2525
isCssGlobalFile,
2626
} from './css/cssConfig';
27-
import { pluginCjsShim } from './plugins/cjsShim';
27+
import {
28+
pluginCjsImportMetaUrlShim,
29+
pluginEsmRequireShim,
30+
} from './plugins/shims';
2831
import type {
2932
AutoExternal,
3033
BannerAndFooter,
@@ -37,6 +40,7 @@ import type {
3740
RslibConfigAsyncFn,
3841
RslibConfigExport,
3942
RslibConfigSyncFn,
43+
Shims,
4044
Syntax,
4145
} from './types';
4246
import { getDefaultExtension } from './utils/extension';
@@ -451,12 +455,16 @@ const composeFormatConfig = (
451455
pkgJson: PkgJson,
452456
): RsbuildConfig => {
453457
const jsParserOptions = {
454-
importMeta: false,
455-
requireResolve: false,
456-
requireDynamic: false,
457-
requireAsExpression: false,
458-
importDynamic: false,
459-
};
458+
cjs: {
459+
requireResolve: false,
460+
requireDynamic: false,
461+
requireAsExpression: false,
462+
},
463+
esm: {
464+
importMeta: false,
465+
importDynamic: false,
466+
},
467+
} as const;
460468

461469
switch (format) {
462470
case 'esm':
@@ -465,7 +473,10 @@ const composeFormatConfig = (
465473
rspack: {
466474
module: {
467475
parser: {
468-
javascript: jsParserOptions,
476+
javascript: {
477+
...jsParserOptions.esm,
478+
...jsParserOptions.cjs,
479+
},
469480
},
470481
},
471482
optimization: {
@@ -490,12 +501,11 @@ const composeFormatConfig = (
490501
};
491502
case 'cjs':
492503
return {
493-
plugins: [pluginCjsShim()],
494504
tools: {
495505
rspack: {
496506
module: {
497507
parser: {
498-
javascript: jsParserOptions,
508+
javascript: { ...jsParserOptions.esm, ...jsParserOptions.cjs },
499509
},
500510
},
501511
output: {
@@ -545,6 +555,47 @@ const composeFormatConfig = (
545555
}
546556
};
547557

558+
const composeShimsConfig = (format: Format, shims?: Shims): RsbuildConfig => {
559+
const resolvedShims = {
560+
cjs: {
561+
'import.meta.url': shims?.cjs?.['import.meta.url'] ?? true,
562+
},
563+
esm: {
564+
__filename: shims?.esm?.__filename ?? false,
565+
__dirname: shims?.esm?.__dirname ?? false,
566+
require: shims?.esm?.require ?? false,
567+
},
568+
};
569+
570+
switch (format) {
571+
case 'esm':
572+
return {
573+
tools: {
574+
rspack: {
575+
node: {
576+
// "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`
577+
__dirname: resolvedShims.esm.__dirname ? 'node-module' : false,
578+
__filename: resolvedShims.esm.__filename ? 'node-module' : false,
579+
},
580+
},
581+
},
582+
plugins: [resolvedShims.esm.require && pluginEsmRequireShim()].filter(
583+
Boolean,
584+
),
585+
};
586+
case 'cjs':
587+
return {
588+
plugins: [
589+
resolvedShims.cjs['import.meta.url'] && pluginCjsImportMetaUrlShim(),
590+
].filter(Boolean),
591+
};
592+
case 'umd':
593+
return {};
594+
default:
595+
throw new Error(`Unsupported format: ${format}`);
596+
}
597+
};
598+
548599
export const composeModuleImportWarn = (request: string): string => {
549600
return `The externalized commonjs request ${color.green(`"${request}"`)} will use ${color.blue('"module"')} external type in ESM format. If you want to specify other external type, considering set the request and type with ${color.blue('"output.externals"')}.`;
550601
};
@@ -854,9 +905,6 @@ const composeTargetConfig = (
854905
tools: {
855906
rspack: {
856907
target: ['node'],
857-
// "__dirname" and "__filename" shims will automatically be enabled when `output.module` is `true`,
858-
// and leave them as-is in the rest of the cases. Leave the comments here to explain the behavior.
859-
// { node: { __dirname: ..., __filename: ... } }
860908
},
861909
},
862910
output: {
@@ -931,13 +979,15 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
931979

932980
const {
933981
format,
982+
shims,
934983
banner = {},
935984
footer = {},
936985
autoExtension = true,
937986
autoExternal = true,
938987
externalHelpers = false,
939988
redirect = {},
940989
} = config;
990+
const shimsConfig = composeShimsConfig(format!, shims);
941991
const formatConfig = composeFormatConfig(format!, pkgJson!);
942992
const externalHelpersConfig = composeExternalHelpersConfig(
943993
externalHelpers,
@@ -990,6 +1040,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
9901040

9911041
return mergeRsbuildConfig(
9921042
formatConfig,
1043+
shimsConfig,
9931044
externalHelpersConfig,
9941045
// externalsWarnConfig should before other externals config
9951046
externalsWarnConfig,
@@ -1069,6 +1120,7 @@ export async function composeCreateRsbuildConfig(
10691120
'banner',
10701121
'footer',
10711122
'dts',
1123+
'shims',
10721124
]),
10731125
),
10741126
};

packages/core/src/plugins/cjsShim.ts renamed to packages/core/src/plugins/shims.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { RsbuildPlugin } from '@rsbuild/core';
1+
import { type RsbuildPlugin, rspack } from '@rsbuild/core';
22

33
const importMetaUrlShim = `/*#__PURE__*/ (function () {
44
return typeof document === 'undefined'
@@ -11,9 +11,8 @@ const importMetaUrlShim = `/*#__PURE__*/ (function () {
1111
// - Replace `import.meta.url` with `importMetaUrl`.
1212
// - Inject `importMetaUrl` to the end of the module (can't inject at the beginning because of `"use strict";`).
1313
// This is a short-term solution, and we hope to provide built-in polyfills like `node.__filename` on Rspack side.
14-
export const pluginCjsShim = (): RsbuildPlugin => ({
15-
name: 'rsbuild-plugin-cjs-shim',
16-
14+
export const pluginCjsImportMetaUrlShim = (): RsbuildPlugin => ({
15+
name: 'rsbuild:cjs-import-meta-url-shim',
1716
setup(api) {
1817
api.modifyEnvironmentConfig((config) => {
1918
config.source.define = {
@@ -23,3 +22,26 @@ export const pluginCjsShim = (): RsbuildPlugin => ({
2322
});
2423
},
2524
});
25+
26+
const requireShim = `// Rslib ESM shims
27+
import __rslib_shim_module__ from 'module';
28+
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(import.meta.url);
29+
`;
30+
31+
export const pluginEsmRequireShim = (): RsbuildPlugin => ({
32+
name: 'rsbuild:esm-require-shim',
33+
setup(api) {
34+
api.modifyRspackConfig((config) => {
35+
config.plugins ??= [];
36+
config.plugins.push(
37+
new rspack.BannerPlugin({
38+
banner: requireShim,
39+
// Just before minify stage, to perform tree shaking.
40+
stage: rspack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE - 1,
41+
raw: true,
42+
include: /\.(js|cjs)$/,
43+
}),
44+
);
45+
});
46+
},
47+
});

packages/core/src/types/config/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ export type BannerAndFooter = {
4848
dts?: string;
4949
};
5050

51+
export type Shims = {
52+
cjs?: {
53+
'import.meta.url'?: boolean;
54+
};
55+
esm?: {
56+
__filename?: boolean;
57+
__dirname?: boolean;
58+
require?: boolean;
59+
};
60+
};
61+
5162
export type Redirect = {
5263
// TODO: support other redirects
5364
// alias?: boolean;
@@ -67,6 +78,7 @@ export interface LibConfig extends RsbuildConfig {
6778
externalHelpers?: boolean;
6879
banner?: BannerAndFooter;
6980
footer?: BannerAndFooter;
81+
shims?: Shims;
7082
dts?: Dts;
7183
}
7284

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
4848
"not dead",
4949
],
5050
},
51+
"plugins": [],
5152
"source": {
5253
"alias": {
5354
"bar": "bar",
@@ -110,6 +111,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
110111
},
111112
},
112113
},
114+
"node": {
115+
"__dirname": false,
116+
"__filename": false,
117+
},
113118
"optimization": {
114119
"concatenateModules": true,
115120
"sideEffects": "flag",
@@ -196,7 +201,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
196201
},
197202
"plugins": [
198203
{
199-
"name": "rsbuild-plugin-cjs-shim",
204+
"name": "rsbuild:cjs-import-meta-url-shim",
200205
"setup": [Function],
201206
},
202207
],

packages/create-rslib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-rslib",
3-
"version": "0.0.11",
3+
"version": "0.0.12",
44
"description": "Create a new Rslib project",
55
"homepage": "https://lib.rsbuild.dev",
66
"repository": {

packages/plugin-dts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rsbuild-plugin-dts",
3-
"version": "0.0.11",
3+
"version": "0.0.12",
44
"description": "Dts plugin for Rsbuild",
55
"homepage": "https://lib.rsbuild.dev",
66
"bugs": {

0 commit comments

Comments
 (0)