@module-federation/esbuild 0.0.0-next-20240524225331 → 0.0.0-next-20240528203231

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,8 +12,7 @@ var esModuleLexer = require('es-module-lexer');
12
12
  var cjsModuleLexer = require('cjs-module-lexer');
13
13
  var util = require('util');
14
14
  var enhancedResolve = require('enhanced-resolve');
15
- var federationBuilder = require('./federation-builder.cjs.js');
16
- require('npmlog');
15
+ var getExternals = require('./get-externals.cjs.js');
17
16
 
18
17
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
18
 
@@ -116,20 +115,17 @@ const writeRemoteManifest = async (config, result)=>{
116
115
  name: config.name
117
116
  };
118
117
  }
119
- const mfConfig = config;
120
118
  var _process_env_NODE_ENV;
121
119
  const envType = process.env['NODE_ENV'] === 'development' ? 'local' : (_process_env_NODE_ENV = process.env['NODE_ENV']) != null ? _process_env_NODE_ENV : '';
122
120
  const publicPath = config.publicPath || 'auto';
123
121
  let containerName = '';
124
122
  const outputMap = Object.entries(((_result_metafile = result.metafile) == null ? void 0 : _result_metafile.outputs) || {}).reduce((acc, [chunkKey, chunkValue])=>{
125
- //@ts-ignore
126
- const { entryPoint, kind = 'static-import' } = chunkValue;
123
+ const { entryPoint } = chunkValue;
127
124
  const key = entryPoint || chunkKey;
128
- if (key.startsWith('container:') && key.endsWith(mfConfig.filename)) {
125
+ if (key.startsWith('container:') && key.endsWith(config.filename)) {
129
126
  containerName = key;
130
127
  }
131
128
  acc[key] = _extends$2({}, chunkValue, {
132
- kind,
133
129
  chunk: chunkKey
134
130
  });
135
131
  return acc;
@@ -172,7 +168,7 @@ const writeRemoteManifest = async (config, result)=>{
172
168
  }
173
169
  return assets;
174
170
  };
175
- const shared = mfConfig.shared ? await Promise.all(Object.entries(mfConfig.shared).map(async ([pkg, config])=>{
171
+ const shared = config.shared ? await Promise.all(Object.entries(config.shared).map(async ([pkg, config])=>{
176
172
  const meta = outputMap['esm-shares:' + pkg];
177
173
  const chunks = getChunks(meta, outputMap);
178
174
  let { version } = config;
@@ -187,7 +183,7 @@ const writeRemoteManifest = async (config, result)=>{
187
183
  }
188
184
  }
189
185
  return {
190
- id: `${mfConfig.name}:${pkg}`,
186
+ id: `${config.name}:${pkg}`,
191
187
  name: pkg,
192
188
  version: version || config.version,
193
189
  singleton: config.singleton || false,
@@ -195,7 +191,7 @@ const writeRemoteManifest = async (config, result)=>{
195
191
  assets: chunks
196
192
  };
197
193
  })) : [];
198
- const remotes = mfConfig.remotes ? Object.entries(mfConfig.remotes).map(([alias, remote])=>{
194
+ const remotes = config.remotes ? Object.entries(config.remotes).map(([alias, remote])=>{
199
195
  const [federationContainerName, entry] = remote.includes('@') ? remote.split('@') : [
200
196
  alias,
201
197
  remote
@@ -207,11 +203,11 @@ const writeRemoteManifest = async (config, result)=>{
207
203
  entry
208
204
  };
209
205
  }) : [];
210
- const exposes = mfConfig.exposes ? await Promise.all(Object.entries(mfConfig.exposes).map(async ([expose, value])=>{
206
+ const exposes = config.exposes ? await Promise.all(Object.entries(config.exposes).map(async ([expose, value])=>{
211
207
  const exposedFound = outputMapWithoutExt[value.replace('./', '')];
212
208
  const chunks = getChunks(exposedFound, outputMap);
213
209
  return {
214
- id: `${mfConfig.name}:${expose.replace(/^\.\//, '')}`,
210
+ id: `${config.name}:${expose.replace(/^\.\//, '')}`,
215
211
  name: expose.replace(/^\.\//, ''),
216
212
  assets: chunks,
217
213
  path: expose
@@ -225,22 +221,22 @@ const writeRemoteManifest = async (config, result)=>{
225
221
  };
226
222
  var _packageJson_name;
227
223
  const manifest = {
228
- id: mfConfig.name,
229
- name: mfConfig.name,
224
+ id: config.name,
225
+ name: config.name,
230
226
  metaData: {
231
- name: mfConfig.name,
227
+ name: config.name,
232
228
  type: 'app',
233
229
  buildInfo: {
234
230
  buildVersion: envType,
235
231
  buildName: ((_packageJson_name = packageJson.name) != null ? _packageJson_name : 'default').replace(/[^a-zA-Z0-9]/g, '_')
236
232
  },
237
233
  remoteEntry: {
238
- name: mfConfig.filename,
234
+ name: config.filename,
239
235
  path: outputMap[containerName] ? path__default["default"].dirname(outputMap[containerName].chunk) : '',
240
236
  type: 'esm'
241
237
  },
242
238
  types,
243
- globalName: mfConfig.name,
239
+ globalName: config.name,
244
240
  pluginVersion: version,
245
241
  publicPath
246
242
  },
@@ -434,9 +430,7 @@ const createContainer = (federationOptions) => {
434
430
  return __webpack_exports__;
435
431
  }`;
436
432
 
437
- //replace with createContainer from bundler runtime
438
- //@ts-ignore
439
- const buildContainerHost = ({ config })=>{
433
+ const buildContainerHost = (config)=>{
440
434
  const { name, remotes = {}, shared = {}, exposes = {} } = config;
441
435
  const remoteConfigs = Object.entries(remotes).map(([remoteAlias, remote])=>({
442
436
  type: 'esm',
@@ -504,7 +498,6 @@ const buildContainerHost = ({ config })=>{
504
498
 
505
499
  await Promise.all(remotePrefetch);
506
500
 
507
- console.log('module map',moduleMap);
508
501
  const map = Object.keys(moduleMap).reduce((acc, expose) => {
509
502
  const importMap = importShim.getImportMap().imports;
510
503
  const key = args.origin.name + expose.replace('.', '');
@@ -517,7 +510,6 @@ console.log('module map',moduleMap);
517
510
  return acc;
518
511
  }, {});
519
512
  await importShim.addImportMap({ imports: map });
520
- console.log('final map', importShim.getImportMap());
521
513
 
522
514
  return args;
523
515
  }
@@ -543,11 +535,11 @@ console.log('module map',moduleMap);
543
535
  const createContainerPlugin = (config)=>({
544
536
  name: 'createContainer',
545
537
  setup (build) {
546
- const { externals, config: { filename } } = config;
538
+ const { filename } = config;
547
539
  const filter = new RegExp([
548
540
  filename
549
541
  ].map((name)=>`${name}$`).join('|'));
550
- const sharedExternals = new RegExp(Object.keys(federationBuilder.federationBuilder.config.shared || {}).map((name)=>`${name}$`).join('|'));
542
+ const sharedExternals = new RegExp(Object.keys(config.shared || {}).map((name)=>`${name}$`).join('|'));
551
543
  build.onResolve({
552
544
  filter
553
545
  }, async (args)=>({
@@ -609,8 +601,8 @@ const createContainerPlugin = (config)=>({
609
601
  });
610
602
 
611
603
  // Builds the federation host code
612
- const buildFederationHost = ()=>{
613
- const { name, remotes, shared } = federationBuilder.federationBuilder.config;
604
+ const buildFederationHost = (config)=>{
605
+ const { name, remotes, shared } = config;
614
606
  const remoteConfigs = remotes ? JSON.stringify(Object.entries(remotes).map(([remoteAlias, remote])=>({
615
607
  name: remoteAlias,
616
608
  entry: remote,
@@ -696,104 +688,103 @@ const buildFederationHost = ()=>{
696
688
  });
697
689
  `;
698
690
  };
699
- const initializeHostPlugin = {
700
- name: 'host-initialization',
701
- setup (build) {
702
- build.onResolve({
703
- filter: /federation-host/
704
- }, (args)=>({
705
- path: args.path,
706
- namespace: 'federation-host',
707
- pluginData: {
708
- kind: args.kind,
709
- resolveDir: args.resolveDir
710
- }
711
- }));
712
- build.onLoad({
713
- filter: /.*/,
714
- namespace: 'federation-host'
715
- }, async (args)=>({
716
- contents: buildFederationHost(),
717
- resolveDir: args.pluginData.resolveDir
718
- }));
719
- // Add custom loaders
720
- const loaders = build.initialOptions.loader || {};
721
- // Apply custom loaders
722
- for (const [ext, loader] of Object.entries(loaders)){
691
+ const initializeHostPlugin = (config)=>({
692
+ name: 'host-initialization',
693
+ setup (build) {
694
+ build.onResolve({
695
+ filter: /federation-host/
696
+ }, (args)=>({
697
+ path: args.path,
698
+ namespace: 'federation-host',
699
+ pluginData: {
700
+ kind: args.kind,
701
+ resolveDir: args.resolveDir
702
+ }
703
+ }));
723
704
  build.onLoad({
724
- filter: new RegExp(`\\${ext}$`),
705
+ filter: /.*/,
706
+ namespace: 'federation-host'
707
+ }, async (args)=>({
708
+ contents: buildFederationHost(config),
709
+ resolveDir: args.pluginData.resolveDir
710
+ }));
711
+ // Add custom loaders
712
+ const loaders = build.initialOptions.loader || {};
713
+ // Apply custom loaders
714
+ for (const [ext, loader] of Object.entries(loaders)){
715
+ build.onLoad({
716
+ filter: new RegExp(`\\${ext}$`),
717
+ namespace: 'file'
718
+ }, async (args)=>{
719
+ const contents = await fs__default["default"].promises.readFile(args.path, 'utf8');
720
+ return {
721
+ contents: buildFederationHost(config) + contents,
722
+ loader
723
+ };
724
+ });
725
+ }
726
+ // Fallback loader for files not matched by custom loaders
727
+ const fallbackFilter = new RegExp(Object.keys(loaders).map((ext)=>`\\${ext}$`).join('|'));
728
+ build.onLoad({
729
+ filter: /.*\.(ts|js|mjs)$/,
725
730
  namespace: 'file'
726
- }, async (args)=>{
727
- const contents = await fs__default["default"].promises.readFile(args.path, 'utf8');
728
- return {
729
- contents: buildFederationHost() + contents,
730
- loader
731
- };
731
+ }, //@ts-ignore
732
+ async (args)=>{
733
+ if (!fallbackFilter.test(args.path)) {
734
+ if (!build.initialOptions.entryPoints.some((e)=>args.path.includes(e))) {
735
+ return;
736
+ }
737
+ const contents = await fs__default["default"].promises.readFile(args.path, 'utf8');
738
+ return {
739
+ contents: 'import "federation-host"; \n ' + contents
740
+ };
741
+ }
732
742
  });
733
743
  }
734
- // Fallback loader for files not matched by custom loaders
735
- const fallbackFilter = new RegExp(Object.keys(loaders).map((ext)=>`\\${ext}$`).join('|'));
736
- build.onLoad({
737
- filter: /.*\.(ts|js|mjs)$/,
738
- namespace: 'file'
739
- }, //@ts-ignore
740
- async (args)=>{
741
- if (!fallbackFilter.test(args.path)) {
742
- if (!build.initialOptions.entryPoints.some((e)=>args.path.includes(e))) {
743
- return;
744
- }
745
- const contents = await fs__default["default"].promises.readFile(args.path, 'utf8');
746
- return {
747
- contents: buildFederationHost() + contents
748
- };
749
- }
750
- });
751
- }
752
- };
744
+ });
753
745
 
754
746
  // relys on import map since i dont know the named exports of a remote to return.
755
747
  const createVirtualRemoteModule = (name, ref)=>`
756
748
  export * from ${JSON.stringify('federationRemote/' + ref)}
757
749
  `;
758
- const linkRemotesPlugin = {
759
- name: 'linkRemotes',
760
- setup (build) {
761
- //@ts-ignore
762
- const remotes = federationBuilder.federationBuilder.config.remotes || {};
763
- const filter = new RegExp(Object.keys(remotes).reduce((acc, key)=>{
764
- if (!key) return acc;
765
- acc.push(`^${key}`);
766
- return acc;
767
- }, []).join('|'));
768
- build.onResolve({
769
- filter: filter
770
- }, async (args)=>{
771
- return {
772
- path: args.path,
750
+ const linkRemotesPlugin = (config)=>({
751
+ name: 'linkRemotes',
752
+ setup (build) {
753
+ const remotes = config.remotes || {};
754
+ const filter = new RegExp(Object.keys(remotes).reduce((acc, key)=>{
755
+ if (!key) return acc;
756
+ acc.push(`^${key}`);
757
+ return acc;
758
+ }, []).join('|'));
759
+ build.onResolve({
760
+ filter: filter
761
+ }, async (args)=>{
762
+ return {
763
+ path: args.path,
764
+ namespace: 'remote-module'
765
+ };
766
+ });
767
+ build.onResolve({
768
+ filter: /^federationRemote/
769
+ }, async (args)=>{
770
+ return {
771
+ path: args.path.replace('federationRemote/', ''),
772
+ external: true,
773
+ namespace: 'externals'
774
+ };
775
+ });
776
+ build.onLoad({
777
+ filter,
773
778
  namespace: 'remote-module'
774
- };
775
- });
776
- build.onResolve({
777
- filter: /^federationRemote/
778
- }, async (args)=>{
779
- return {
780
- path: args.path.replace('federationRemote/', ''),
781
- external: true,
782
- namespace: 'externals'
783
- };
784
- });
785
- build.onLoad({
786
- filter,
787
- namespace: 'remote-module'
788
- }, async (args)=>{
789
- return {
790
- contents: createVirtualRemoteModule(federationBuilder.federationBuilder.config.name, args.path),
791
- loader: 'js',
792
- resolveDir: path__default["default"].dirname(args.path)
793
- };
794
- });
795
- }
796
- };
779
+ }, async (args)=>{
780
+ return {
781
+ contents: createVirtualRemoteModule(config.name, args.path),
782
+ loader: 'js',
783
+ resolveDir: path__default["default"].dirname(args.path)
784
+ };
785
+ });
786
+ }
787
+ });
797
788
 
798
789
  function _extends$1() {
799
790
  _extends$1 = Object.assign || function(target) {
@@ -859,28 +850,14 @@ const cjsToEsmPlugin = {
859
850
  }
860
851
  };
861
852
  // Plugin to link shared dependencies
862
- const linkSharedPlugin = {
863
- name: 'linkShared',
864
- setup (build) {
865
- const filter = new RegExp(Object.keys(federationBuilder.federationBuilder.config.shared || {}).map((name)=>`${name}$`).join('|'));
866
- build.onResolve({
867
- filter
868
- }, (args)=>{
869
- if (args.namespace === 'esm-shares') return null;
870
- return {
871
- path: args.path,
872
- namespace: 'virtual-share-module',
873
- pluginData: {
874
- kind: args.kind,
875
- resolveDir: args.resolveDir
876
- }
877
- };
878
- });
879
- build.onResolve({
880
- filter: /.*/,
881
- namespace: 'esm-shares'
882
- }, (args)=>{
883
- if (filter.test(args.path)) {
853
+ const linkSharedPlugin = (config)=>({
854
+ name: 'linkShared',
855
+ setup (build) {
856
+ const filter = new RegExp(Object.keys(config.shared || {}).map((name)=>`${name}$`).join('|'));
857
+ build.onResolve({
858
+ filter
859
+ }, (args)=>{
860
+ if (args.namespace === 'esm-shares') return null;
884
861
  return {
885
862
  path: args.path,
886
863
  namespace: 'virtual-share-module',
@@ -889,58 +866,83 @@ const linkSharedPlugin = {
889
866
  resolveDir: args.resolveDir
890
867
  }
891
868
  };
892
- }
893
- if (filter.test(args.importer)) {
894
- return {
895
- path: args.path,
869
+ });
870
+ build.onResolve({
871
+ filter: /.*/,
872
+ namespace: 'esm-shares'
873
+ }, (args)=>{
874
+ if (filter.test(args.path)) {
875
+ return {
876
+ path: args.path,
877
+ namespace: 'virtual-share-module',
878
+ pluginData: {
879
+ kind: args.kind,
880
+ resolveDir: args.resolveDir
881
+ }
882
+ };
883
+ }
884
+ if (filter.test(args.importer)) {
885
+ return {
886
+ path: args.path,
887
+ namespace: 'esm-shares',
888
+ pluginData: {
889
+ kind: args.kind,
890
+ resolveDir: args.resolveDir
891
+ }
892
+ };
893
+ }
894
+ return undefined;
895
+ });
896
+ build.onResolve({
897
+ filter: /^federationShare/
898
+ }, async (args)=>({
899
+ path: args.path.replace('federationShare/', ''),
896
900
  namespace: 'esm-shares',
897
901
  pluginData: {
898
902
  kind: args.kind,
899
903
  resolveDir: args.resolveDir
900
904
  }
905
+ }));
906
+ build.onLoad({
907
+ filter,
908
+ namespace: 'virtual-share-module'
909
+ }, async (args)=>{
910
+ const exp = await getExports(args.path);
911
+ return {
912
+ contents: createVirtualShareModule(config.name, args.path, exp),
913
+ loader: 'js',
914
+ resolveDir: path__default["default"].dirname(args.path)
901
915
  };
902
- }
903
- return undefined;
904
- });
905
- build.onResolve({
906
- filter: /^federationShare/
907
- }, async (args)=>({
908
- path: args.path.replace('federationShare/', ''),
909
- namespace: 'esm-shares',
910
- pluginData: {
911
- kind: args.kind,
912
- resolveDir: args.resolveDir
913
- }
914
- }));
915
- build.onLoad({
916
- filter,
917
- namespace: 'virtual-share-module'
918
- }, async (args)=>{
919
- const exp = await getExports(args.path);
920
- return {
921
- contents: createVirtualShareModule(federationBuilder.federationBuilder.config.name, args.path, exp),
922
- loader: 'js',
923
- resolveDir: path__default["default"].dirname(args.path)
924
- };
925
- });
926
- }
927
- };
916
+ });
917
+ }
918
+ });
928
919
  // Main module federation plugin
929
920
  const moduleFederationPlugin = (config)=>({
930
921
  name: 'module-federation',
931
922
  setup (build) {
932
923
  build.initialOptions.metafile = true;
924
+ const externals = getExternals.getExternals(config);
925
+ if (build.initialOptions.external) {
926
+ build.initialOptions.external = [
927
+ ...new Set([
928
+ ...build.initialOptions.external,
929
+ ...externals
930
+ ])
931
+ ];
932
+ } else {
933
+ build.initialOptions.external = externals;
934
+ }
933
935
  const pluginStack = [];
934
- const remotes = Object.keys(federationBuilder.federationBuilder.config.remotes || {}).length;
935
- const shared = Object.keys(federationBuilder.federationBuilder.config.shared || {}).length;
936
- const exposes = Object.keys(federationBuilder.federationBuilder.config.exposes || {}).length;
936
+ const remotes = Object.keys(config.remotes || {}).length;
937
+ const shared = Object.keys(config.shared || {}).length;
938
+ const exposes = Object.keys(config.exposes || {}).length;
937
939
  const entryPoints = build.initialOptions.entryPoints;
938
- const filename = federationBuilder.federationBuilder.config.filename || 'remoteEntry.js';
940
+ const filename = config.filename || 'remoteEntry.js';
939
941
  if (remotes) {
940
- pluginStack.push(linkRemotesPlugin);
942
+ pluginStack.push(linkRemotesPlugin(config));
941
943
  }
942
944
  if (shared) {
943
- pluginStack.push(linkSharedPlugin);
945
+ pluginStack.push(linkSharedPlugin(config));
944
946
  }
945
947
  if (!entryPoints) {
946
948
  build.initialOptions.entryPoints = [];
@@ -957,7 +959,7 @@ const moduleFederationPlugin = (config)=>({
957
959
  }
958
960
  }
959
961
  [
960
- initializeHostPlugin,
962
+ initializeHostPlugin(config),
961
963
  createContainerPlugin(config),
962
964
  cjsToEsmPlugin,
963
965
  ...pluginStack
@@ -965,8 +967,8 @@ const moduleFederationPlugin = (config)=>({
965
967
  build.onEnd(async (result)=>{
966
968
  if (!result.metafile) return;
967
969
  if (exposes) {
968
- const exposedConfig = federationBuilder.federationBuilder.config.exposes || {};
969
- const remoteFile = federationBuilder.federationBuilder.config.filename;
970
+ const exposedConfig = config.exposes || {};
971
+ const remoteFile = config.filename;
970
972
  const exposedEntries = {};
971
973
  const outputMapWithoutExt = Object.entries(result.metafile.outputs).reduce((acc, [chunkKey, chunkValue])=>{
972
974
  //@ts-ignore
@@ -999,7 +1001,7 @@ const moduleFederationPlugin = (config)=>({
999
1001
  fs__default["default"].writeFileSync(outputPath, withExports, 'utf-8');
1000
1002
  }
1001
1003
  }
1002
- await writeRemoteManifest(federationBuilder.federationBuilder.config, result);
1004
+ await writeRemoteManifest(config, result);
1003
1005
  console.log(`build ended with ${result.errors.length} errors`);
1004
1006
  });
1005
1007
  }