Skip to content

Commit 31efabc

Browse files
feat: support iife format (#998)
Co-authored-by: Timeless0911 <[email protected]>
1 parent c8a4e87 commit 31efabc

File tree

17 files changed

+458
-19
lines changed

17 files changed

+458
-19
lines changed

packages/core/src/config.ts

Lines changed: 66 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,39 @@ 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: {
704+
type: 'modern-module',
705+
},
706+
},
707+
optimization: {
708+
nodeEnv: process.env.NODE_ENV,
709+
},
710+
},
711+
},
712+
};
713+
714+
return config;
715+
}
682716
case 'mf':
683717
if (bundle === false) {
684718
throw new Error(
@@ -789,6 +823,7 @@ const composeShimsConfig = (
789823
};
790824
break;
791825
case 'umd':
826+
case 'iife':
792827
case 'mf':
793828
break;
794829
default:
@@ -813,36 +848,53 @@ const composeExternalsConfig = (
813848
// Rspack's externals as they will not be merged from different fields. All externals
814849
// should to be unified and merged together in the future.
815850

816-
const externalsTypeMap = {
851+
const externalsTypeMap: Record<Format, Rspack.ExternalsType> = {
817852
esm: 'module-import',
818853
cjs: 'commonjs-import',
819854
umd: 'umd',
820855
// If use 'var', when projects import an external package like '@pkg', this will cause a syntax error such as 'var pkg = @pkg'.
821856
// If use 'umd', the judgement conditions may be affected by other packages that define variables like 'define'.
822857
// Therefore, we use 'global' to satisfy both web and node environments.
823858
mf: 'global',
824-
} as const;
859+
iife: 'global',
860+
};
861+
862+
const globalObjectMap: Record<Format, string | undefined> = {
863+
esm: undefined,
864+
cjs: undefined,
865+
umd: undefined,
866+
mf: undefined,
867+
iife: 'globalThis',
868+
};
869+
870+
const rspackConfig: ToolsConfig['rspack'] = {};
871+
const rsbuildConfig: EnvironmentConfig = {};
825872

826873
switch (format) {
827874
case 'esm':
828875
case 'cjs':
829876
case 'umd':
830877
case 'mf':
831-
return {
832-
output: externals
833-
? {
834-
externals,
835-
}
836-
: {},
837-
tools: {
838-
rspack: {
839-
externalsType: externalsTypeMap[format],
840-
},
841-
},
842-
};
878+
case 'iife':
879+
rsbuildConfig.output = externals ? { externals } : {};
880+
rspackConfig.externalsType = externalsTypeMap[format];
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+
...rsbuildConfig,
894+
tools: {
895+
rspack: rspackConfig,
896+
},
897+
};
846898
};
847899

848900
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: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,231 @@ 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": {
903+
"type": "modern-module",
904+
},
905+
},
906+
},
907+
[Function],
908+
{
909+
"target": [
910+
"node",
911+
],
912+
},
913+
{
914+
"externalsType": "global",
915+
"output": {
916+
"globalObject": "globalThis",
917+
},
918+
},
919+
{
920+
"plugins": [
921+
EntryChunkPlugin {
922+
"contextToWatch": null,
923+
"enabledImportMetaUrlShim": false,
924+
"reactDirectives": {},
925+
"shebangChmod": 493,
926+
"shebangEntries": {},
927+
"shebangInjectedAssets": Set {},
928+
"shimsInjectedAssets": Set {},
929+
},
930+
],
931+
},
932+
{
933+
"resolve": {
934+
"extensionAlias": {
935+
".js": [
936+
".ts",
937+
".tsx",
938+
],
939+
},
940+
},
941+
},
942+
],
943+
"swc": {
944+
"jsc": {
945+
"externalHelpers": false,
946+
},
947+
},
948+
},
949+
},
950+
"format": "iife",
951+
},
727952
{
728953
"config": {
729954
"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({})],

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)