Skip to content

Commit b291e1e

Browse files
committed
feat: support iife format
1 parent c8a4e87 commit b291e1e

File tree

16 files changed

+431
-19
lines changed

16 files changed

+431
-19
lines changed

packages/core/src/config.ts

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type RsbuildPlugin,
88
type RsbuildPlugins,
99
type Rspack,
10+
type ToolsConfig,
1011
defineConfig as defineRsbuildConfig,
1112
loadConfig as loadRsbuildConfig,
1213
mergeRsbuildConfig,
@@ -679,6 +680,37 @@ const composeFormatConfig = ({
679680

680681
return config;
681682
}
683+
case 'iife': {
684+
if (bundle === false) {
685+
throw new Error(
686+
'When using "iife" format, "bundle" must be set to "true". Since the default value for "bundle" is "true", so you can either explicitly set it to "true" or remove the field entirely.',
687+
);
688+
}
689+
690+
const config: EnvironmentConfig = {
691+
tools: {
692+
rspack: {
693+
module: {
694+
parser: {
695+
javascript: {
696+
importMeta: false,
697+
},
698+
},
699+
},
700+
output: {
701+
iife: true,
702+
asyncChunks: false,
703+
library: 'modern-module',
704+
},
705+
optimization: {
706+
nodeEnv: process.env.NODE_ENV,
707+
},
708+
},
709+
},
710+
};
711+
712+
return config;
713+
}
682714
case 'mf':
683715
if (bundle === false) {
684716
throw new Error(
@@ -789,6 +821,7 @@ const composeShimsConfig = (
789821
};
790822
break;
791823
case 'umd':
824+
case 'iife':
792825
case 'mf':
793826
break;
794827
default:
@@ -813,36 +846,54 @@ const composeExternalsConfig = (
813846
// Rspack's externals as they will not be merged from different fields. All externals
814847
// should to be unified and merged together in the future.
815848

816-
const externalsTypeMap = {
849+
const externalsTypeMap: Record<Format, Rspack.ExternalsType> = {
817850
esm: 'module-import',
818851
cjs: 'commonjs-import',
819852
umd: 'umd',
820853
// If use 'var', when projects import an external package like '@pkg', this will cause a syntax error such as 'var pkg = @pkg'.
821854
// If use 'umd', the judgement conditions may be affected by other packages that define variables like 'define'.
822855
// Therefore, we use 'global' to satisfy both web and node environments.
823856
mf: 'global',
824-
} as const;
857+
iife: 'global',
858+
};
859+
860+
const globalObjectMap: Record<Format, string | undefined> = {
861+
esm: undefined,
862+
cjs: undefined,
863+
umd: undefined,
864+
mf: undefined,
865+
iife: 'globalThis',
866+
};
867+
868+
const rspackConfig: ToolsConfig['rspack'] = {};
825869

826870
switch (format) {
827871
case 'esm':
828872
case 'cjs':
829873
case 'umd':
830874
case 'mf':
831-
return {
832-
output: externals
833-
? {
834-
externals,
835-
}
836-
: {},
837-
tools: {
838-
rspack: {
839-
externalsType: externalsTypeMap[format],
840-
},
841-
},
842-
};
875+
case 'iife':
876+
if (externals) {
877+
rspackConfig.externals = externals;
878+
}
879+
rspackConfig.externalsType = externalsTypeMap[format];
880+
881+
if (globalObjectMap[format]) {
882+
rspackConfig.output = {
883+
globalObject: globalObjectMap[format],
884+
};
885+
}
886+
887+
break;
843888
default:
844889
throw new Error(`Unsupported format: ${format}`);
845890
}
891+
892+
return {
893+
tools: {
894+
rspack: rspackConfig,
895+
},
896+
};
846897
};
847898

848899
const composeAutoExtensionConfig = (

packages/core/src/types/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
} from '@rsbuild/core';
77
import type { GetAsyncFunctionFromUnion } from './utils';
88

9-
export type Format = 'esm' | 'cjs' | 'umd' | 'mf';
9+
export type Format = 'esm' | 'cjs' | 'umd' | 'mf' | 'iife';
1010

1111
export type FixedEcmaVersions =
1212
| 'es5'

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

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,229 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
724724
},
725725
"format": "umd",
726726
},
727+
{
728+
"config": {
729+
"dev": {
730+
"progressBar": false,
731+
},
732+
"output": {
733+
"distPath": {
734+
"css": "./",
735+
"cssAsync": "./",
736+
"js": "./",
737+
"jsAsync": "./",
738+
},
739+
"externals": [
740+
"assert",
741+
"assert/strict",
742+
"async_hooks",
743+
"buffer",
744+
"child_process",
745+
"cluster",
746+
"console",
747+
"constants",
748+
"crypto",
749+
"dgram",
750+
"diagnostics_channel",
751+
"dns",
752+
"dns/promises",
753+
"domain",
754+
"events",
755+
"fs",
756+
"fs/promises",
757+
"http",
758+
"http2",
759+
"https",
760+
"inspector",
761+
"inspector/promises",
762+
"module",
763+
"net",
764+
"os",
765+
"path",
766+
"path/posix",
767+
"path/win32",
768+
"perf_hooks",
769+
"process",
770+
"punycode",
771+
"querystring",
772+
"readline",
773+
"readline/promises",
774+
"repl",
775+
"stream",
776+
"stream/consumers",
777+
"stream/promises",
778+
"stream/web",
779+
"string_decoder",
780+
"sys",
781+
"timers",
782+
"timers/promises",
783+
"tls",
784+
"trace_events",
785+
"tty",
786+
"url",
787+
"util",
788+
"util/types",
789+
"v8",
790+
"vm",
791+
"wasi",
792+
"worker_threads",
793+
"zlib",
794+
/\\^node:/,
795+
"pnpapi",
796+
],
797+
"filename": {
798+
"js": "[name].js",
799+
},
800+
"filenameHash": false,
801+
"minify": {
802+
"css": false,
803+
"js": true,
804+
"jsOptions": {
805+
"minimizerOptions": {
806+
"compress": {
807+
"dead_code": true,
808+
"defaults": false,
809+
"toplevel": true,
810+
"unused": true,
811+
},
812+
"format": {
813+
"comments": "some",
814+
"preserve_annotations": true,
815+
},
816+
"mangle": false,
817+
"minify": false,
818+
},
819+
},
820+
},
821+
"overrideBrowserslist": [
822+
"last 1 node versions",
823+
],
824+
"target": "node",
825+
},
826+
"performance": {
827+
"chunkSplit": {
828+
"strategy": "custom",
829+
},
830+
},
831+
"plugins": [
832+
{
833+
"name": "rsbuild:lib-entry-chunk",
834+
"setup": [Function],
835+
},
836+
],
837+
"resolve": {
838+
"alias": {
839+
"bar": "bar",
840+
"foo": "foo",
841+
},
842+
},
843+
"source": {
844+
"entry": {},
845+
"preEntry": "./a.js",
846+
},
847+
"tools": {
848+
"htmlPlugin": false,
849+
"rspack": [
850+
{
851+
"experiments": {
852+
"rspackFuture": {
853+
"bundlerInfo": {
854+
"force": false,
855+
},
856+
},
857+
},
858+
"optimization": {
859+
"moduleIds": "named",
860+
"nodeEnv": false,
861+
"splitChunks": {
862+
"chunks": "async",
863+
},
864+
},
865+
"resolve": {
866+
"extensionAlias": {
867+
".cjs": [
868+
".cts",
869+
".cjs",
870+
],
871+
".js": [
872+
".ts",
873+
".tsx",
874+
".js",
875+
".jsx",
876+
],
877+
".jsx": [
878+
".tsx",
879+
".jsx",
880+
],
881+
".mjs": [
882+
".mts",
883+
".mjs",
884+
],
885+
},
886+
},
887+
},
888+
{
889+
"module": {
890+
"parser": {
891+
"javascript": {
892+
"importMeta": false,
893+
},
894+
},
895+
},
896+
"optimization": {
897+
"nodeEnv": "test",
898+
},
899+
"output": {
900+
"asyncChunks": false,
901+
"iife": true,
902+
"library": "modern-module",
903+
},
904+
},
905+
[Function],
906+
{
907+
"target": [
908+
"node",
909+
],
910+
},
911+
{
912+
"externalsType": "global",
913+
"output": {
914+
"globalObject": "globalThis",
915+
},
916+
},
917+
{
918+
"plugins": [
919+
EntryChunkPlugin {
920+
"contextToWatch": null,
921+
"enabledImportMetaUrlShim": false,
922+
"reactDirectives": {},
923+
"shebangChmod": 493,
924+
"shebangEntries": {},
925+
"shebangInjectedAssets": Set {},
926+
"shimsInjectedAssets": Set {},
927+
},
928+
],
929+
},
930+
{
931+
"resolve": {
932+
"extensionAlias": {
933+
".js": [
934+
".ts",
935+
".tsx",
936+
],
937+
},
938+
},
939+
},
940+
],
941+
"swc": {
942+
"jsc": {
943+
"externalHelpers": false,
944+
},
945+
},
946+
},
947+
},
948+
"format": "iife",
949+
},
727950
{
728951
"config": {
729952
"dev": {

packages/core/tests/config.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ describe('Should compose create Rsbuild config correctly', () => {
177177
{
178178
format: 'umd',
179179
},
180+
{
181+
format: 'iife',
182+
},
180183
{
181184
format: 'mf',
182185
plugins: [pluginModuleFederation({})],

0 commit comments

Comments
 (0)