@module-federation/esbuild 0.0.0-next-20240524010758 → 0.0.0-next-20240524042957

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.
@@ -8,33 +8,15 @@ var fs = require('fs');
8
8
  var path = require('path');
9
9
  var commonjs = require('@rollup/plugin-commonjs');
10
10
  var replace = require('@rollup/plugin-replace');
11
- require('acorn');
11
+ var esModuleLexer = require('es-module-lexer');
12
+ var cjsModuleLexer = require('cjs-module-lexer');
12
13
  var util = require('util');
13
14
  var enhancedResolve = require('enhanced-resolve');
14
- var moduleLexer = require('cjs-module-lexer');
15
15
  var federationBuilder = require('./federation-builder.cjs.js');
16
16
  require('npmlog');
17
17
 
18
18
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
19
 
20
- function _interopNamespace(e) {
21
- if (e && e.__esModule) return e;
22
- var n = Object.create(null);
23
- if (e) {
24
- Object.keys(e).forEach(function (k) {
25
- if (k !== 'default') {
26
- var d = Object.getOwnPropertyDescriptor(e, k);
27
- Object.defineProperty(n, k, d.get ? d : {
28
- enumerable: true,
29
- get: function () { return e[k]; }
30
- });
31
- }
32
- });
33
- }
34
- n["default"] = e;
35
- return Object.freeze(n);
36
- }
37
-
38
20
  var esbuild__default = /*#__PURE__*/_interopDefaultLegacy(esbuild$1);
39
21
  var pluginNodeResolve__default = /*#__PURE__*/_interopDefaultLegacy(pluginNodeResolve);
40
22
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
@@ -42,7 +24,6 @@ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
42
24
  var commonjs__default = /*#__PURE__*/_interopDefaultLegacy(commonjs);
43
25
  var replace__default = /*#__PURE__*/_interopDefaultLegacy(replace);
44
26
  var enhancedResolve__default = /*#__PURE__*/_interopDefaultLegacy(enhancedResolve);
45
- var moduleLexer__namespace = /*#__PURE__*/_interopNamespace(moduleLexer);
46
27
 
47
28
  function getAugmentedNamespace(n) {
48
29
  if (n.__esModule) return n;
@@ -68,12 +49,9 @@ const resolve = util.promisify(enhancedResolve__default["default"].create({
68
49
  'main'
69
50
  ]
70
51
  }));
71
- let lexerInitialized = false;
72
52
  async function getExports(modulePath) {
73
- if (!lexerInitialized) {
74
- await moduleLexer__namespace.init();
75
- lexerInitialized = true;
76
- }
53
+ await esModuleLexer.init;
54
+ await cjsModuleLexer.init;
77
55
  try {
78
56
  const exports = [];
79
57
  const paths = [];
@@ -84,19 +62,18 @@ async function getExports(modulePath) {
84
62
  while(paths.length > 0){
85
63
  const currentPath = paths.pop();
86
64
  if (currentPath) {
87
- const results = moduleLexer__namespace.parse(await fs__default["default"].readFileSync(currentPath, 'utf8'));
88
- exports.push(...results.exports);
89
- for (const reexport of results.reexports){
90
- const resolvedPath = await resolve(path__default["default"].dirname(currentPath), reexport);
91
- if (typeof resolvedPath === 'string') {
92
- paths.push(resolvedPath);
93
- }
65
+ const content = await fs__default["default"].promises.readFile(currentPath, 'utf8');
66
+ try {
67
+ const { exports: cjsExports } = cjsModuleLexer.parse(content);
68
+ exports.push(...cjsExports);
69
+ } catch (e) {
70
+ const [, esExports] = esModuleLexer.parse(content);
71
+ exports.push(...esExports.map((exp)=>exp.n));
94
72
  }
73
+ // TODO: Handle re-exports
95
74
  }
96
75
  }
97
- /**
98
- * 追加default
99
- */ if (!exports.includes('default')) {
76
+ if (!exports.includes('default')) {
100
77
  exports.push('default');
101
78
  }
102
79
  return exports;
@@ -567,7 +544,7 @@ const createContainerPlugin = (config)=>({
567
544
  const filter = new RegExp([
568
545
  filename
569
546
  ].map((name)=>`${name}$`).join('|'));
570
- const sharedExternals = new RegExp(externals.map((name)=>`${name}$`).join('|'));
547
+ const sharedExternals = new RegExp(Object.keys(federationBuilder.federationBuilder.config.shared || {}).map((name)=>`${name}$`).join('|'));
571
548
  build.onResolve({
572
549
  filter
573
550
  }, async (args)=>({
@@ -604,7 +581,7 @@ const createContainerPlugin = (config)=>({
604
581
  build.onResolve({
605
582
  filter: /.*/,
606
583
  namespace: 'esm-shares'
607
- }, (args)=>{
584
+ }, async (args)=>{
608
585
  if (sharedExternals.test(args.path)) {
609
586
  return {
610
587
  path: args.path,
@@ -615,14 +592,7 @@ const createContainerPlugin = (config)=>({
615
592
  }
616
593
  };
617
594
  }
618
- return {
619
- path: args.path,
620
- namespace: 'esm-shares',
621
- pluginData: {
622
- kind: args.kind,
623
- resolveDir: args.resolveDir
624
- }
625
- };
595
+ return undefined;
626
596
  });
627
597
  build.onLoad({
628
598
  filter,
@@ -859,15 +829,26 @@ const cjsToEsmPlugin = {
859
829
  throw new Error(`Unable to resolve path: ${args.path}`);
860
830
  }
861
831
  const fileContent = fs__default["default"].readFileSync(resolver, 'utf-8');
862
- const { code } = await transform(fileContent);
863
- return {
864
- contents: code,
865
- loader: 'js',
866
- resolveDir: path__default["default"].dirname(resolver),
867
- pluginData: _extends$1({}, args.pluginData, {
868
- path: resolver
869
- })
870
- };
832
+ try {
833
+ const { code } = await transform(fileContent);
834
+ return {
835
+ contents: code,
836
+ loader: 'js',
837
+ resolveDir: path__default["default"].dirname(resolver),
838
+ pluginData: _extends$1({}, args.pluginData, {
839
+ path: resolver
840
+ })
841
+ };
842
+ } catch (e) {
843
+ return {
844
+ contents: fileContent,
845
+ loader: 'js',
846
+ resolveDir: path__default["default"].dirname(resolver),
847
+ pluginData: _extends$1({}, args.pluginData, {
848
+ path: resolver
849
+ })
850
+ };
851
+ }
871
852
  });
872
853
  }
873
854
  };
@@ -875,7 +856,56 @@ const cjsToEsmPlugin = {
875
856
  const linkSharedPlugin = {
876
857
  name: 'linkShared',
877
858
  setup (build) {
878
- const filter = new RegExp(federationBuilder.federationBuilder.externals.map((name)=>`${name}$`).join('|'));
859
+ const filter = new RegExp(Object.keys(federationBuilder.federationBuilder.config.shared || {}).map((name)=>`${name}$`).join('|'));
860
+ build.onResolve({
861
+ filter
862
+ }, (args)=>{
863
+ if (args.namespace === 'esm-shares') return null;
864
+ return {
865
+ path: args.path,
866
+ namespace: 'virtual-share-module',
867
+ pluginData: {
868
+ kind: args.kind,
869
+ resolveDir: args.resolveDir
870
+ }
871
+ };
872
+ });
873
+ build.onResolve({
874
+ filter: /.*/,
875
+ namespace: 'esm-shares'
876
+ }, (args)=>{
877
+ if (filter.test(args.path)) {
878
+ return {
879
+ path: args.path,
880
+ namespace: 'virtual-share-module',
881
+ pluginData: {
882
+ kind: args.kind,
883
+ resolveDir: args.resolveDir
884
+ }
885
+ };
886
+ }
887
+ if (filter.test(args.importer)) {
888
+ return {
889
+ path: args.path,
890
+ namespace: 'esm-shares',
891
+ pluginData: {
892
+ kind: args.kind,
893
+ resolveDir: args.resolveDir
894
+ }
895
+ };
896
+ }
897
+ return undefined;
898
+ });
899
+ build.onResolve({
900
+ filter: /^federationShare/
901
+ }, async (args)=>({
902
+ path: args.path.replace('federationShare/', ''),
903
+ namespace: 'esm-shares',
904
+ pluginData: {
905
+ kind: args.kind,
906
+ resolveDir: args.resolveDir
907
+ }
908
+ }));
879
909
  build.onLoad({
880
910
  filter,
881
911
  namespace: 'virtual-share-module'
@@ -6,10 +6,10 @@ import fs__default from 'fs';
6
6
  import path__default from 'path';
7
7
  import commonjs from '@rollup/plugin-commonjs';
8
8
  import replace from '@rollup/plugin-replace';
9
- import 'acorn';
9
+ import { init, parse as parse$1 } from 'es-module-lexer';
10
+ import { init as init$1, parse } from 'cjs-module-lexer';
10
11
  import { promisify } from 'util';
11
12
  import enhancedResolve from 'enhanced-resolve';
12
- import * as moduleLexer from 'cjs-module-lexer';
13
13
  import { f as federationBuilder } from './federation-builder.esm.js';
14
14
  import 'npmlog';
15
15
 
@@ -37,12 +37,9 @@ const resolve = promisify(enhancedResolve.create({
37
37
  'main'
38
38
  ]
39
39
  }));
40
- let lexerInitialized = false;
41
40
  async function getExports(modulePath) {
42
- if (!lexerInitialized) {
43
- await moduleLexer.init();
44
- lexerInitialized = true;
45
- }
41
+ await init;
42
+ await init$1;
46
43
  try {
47
44
  const exports = [];
48
45
  const paths = [];
@@ -53,19 +50,18 @@ async function getExports(modulePath) {
53
50
  while(paths.length > 0){
54
51
  const currentPath = paths.pop();
55
52
  if (currentPath) {
56
- const results = moduleLexer.parse(await fs__default.readFileSync(currentPath, 'utf8'));
57
- exports.push(...results.exports);
58
- for (const reexport of results.reexports){
59
- const resolvedPath = await resolve(path__default.dirname(currentPath), reexport);
60
- if (typeof resolvedPath === 'string') {
61
- paths.push(resolvedPath);
62
- }
53
+ const content = await fs__default.promises.readFile(currentPath, 'utf8');
54
+ try {
55
+ const { exports: cjsExports } = parse(content);
56
+ exports.push(...cjsExports);
57
+ } catch (e) {
58
+ const [, esExports] = parse$1(content);
59
+ exports.push(...esExports.map((exp)=>exp.n));
63
60
  }
61
+ // TODO: Handle re-exports
64
62
  }
65
63
  }
66
- /**
67
- * 追加default
68
- */ if (!exports.includes('default')) {
64
+ if (!exports.includes('default')) {
69
65
  exports.push('default');
70
66
  }
71
67
  return exports;
@@ -536,7 +532,7 @@ const createContainerPlugin = (config)=>({
536
532
  const filter = new RegExp([
537
533
  filename
538
534
  ].map((name)=>`${name}$`).join('|'));
539
- const sharedExternals = new RegExp(externals.map((name)=>`${name}$`).join('|'));
535
+ const sharedExternals = new RegExp(Object.keys(federationBuilder.config.shared || {}).map((name)=>`${name}$`).join('|'));
540
536
  build.onResolve({
541
537
  filter
542
538
  }, async (args)=>({
@@ -573,7 +569,7 @@ const createContainerPlugin = (config)=>({
573
569
  build.onResolve({
574
570
  filter: /.*/,
575
571
  namespace: 'esm-shares'
576
- }, (args)=>{
572
+ }, async (args)=>{
577
573
  if (sharedExternals.test(args.path)) {
578
574
  return {
579
575
  path: args.path,
@@ -584,14 +580,7 @@ const createContainerPlugin = (config)=>({
584
580
  }
585
581
  };
586
582
  }
587
- return {
588
- path: args.path,
589
- namespace: 'esm-shares',
590
- pluginData: {
591
- kind: args.kind,
592
- resolveDir: args.resolveDir
593
- }
594
- };
583
+ return undefined;
595
584
  });
596
585
  build.onLoad({
597
586
  filter,
@@ -828,15 +817,26 @@ const cjsToEsmPlugin = {
828
817
  throw new Error(`Unable to resolve path: ${args.path}`);
829
818
  }
830
819
  const fileContent = fs__default.readFileSync(resolver, 'utf-8');
831
- const { code } = await transform(fileContent);
832
- return {
833
- contents: code,
834
- loader: 'js',
835
- resolveDir: path__default.dirname(resolver),
836
- pluginData: _extends$1({}, args.pluginData, {
837
- path: resolver
838
- })
839
- };
820
+ try {
821
+ const { code } = await transform(fileContent);
822
+ return {
823
+ contents: code,
824
+ loader: 'js',
825
+ resolveDir: path__default.dirname(resolver),
826
+ pluginData: _extends$1({}, args.pluginData, {
827
+ path: resolver
828
+ })
829
+ };
830
+ } catch (e) {
831
+ return {
832
+ contents: fileContent,
833
+ loader: 'js',
834
+ resolveDir: path__default.dirname(resolver),
835
+ pluginData: _extends$1({}, args.pluginData, {
836
+ path: resolver
837
+ })
838
+ };
839
+ }
840
840
  });
841
841
  }
842
842
  };
@@ -844,7 +844,56 @@ const cjsToEsmPlugin = {
844
844
  const linkSharedPlugin = {
845
845
  name: 'linkShared',
846
846
  setup (build) {
847
- const filter = new RegExp(federationBuilder.externals.map((name)=>`${name}$`).join('|'));
847
+ const filter = new RegExp(Object.keys(federationBuilder.config.shared || {}).map((name)=>`${name}$`).join('|'));
848
+ build.onResolve({
849
+ filter
850
+ }, (args)=>{
851
+ if (args.namespace === 'esm-shares') return null;
852
+ return {
853
+ path: args.path,
854
+ namespace: 'virtual-share-module',
855
+ pluginData: {
856
+ kind: args.kind,
857
+ resolveDir: args.resolveDir
858
+ }
859
+ };
860
+ });
861
+ build.onResolve({
862
+ filter: /.*/,
863
+ namespace: 'esm-shares'
864
+ }, (args)=>{
865
+ if (filter.test(args.path)) {
866
+ return {
867
+ path: args.path,
868
+ namespace: 'virtual-share-module',
869
+ pluginData: {
870
+ kind: args.kind,
871
+ resolveDir: args.resolveDir
872
+ }
873
+ };
874
+ }
875
+ if (filter.test(args.importer)) {
876
+ return {
877
+ path: args.path,
878
+ namespace: 'esm-shares',
879
+ pluginData: {
880
+ kind: args.kind,
881
+ resolveDir: args.resolveDir
882
+ }
883
+ };
884
+ }
885
+ return undefined;
886
+ });
887
+ build.onResolve({
888
+ filter: /^federationShare/
889
+ }, async (args)=>({
890
+ path: args.path.replace('federationShare/', ''),
891
+ namespace: 'esm-shares',
892
+ pluginData: {
893
+ kind: args.kind,
894
+ resolveDir: args.resolveDir
895
+ }
896
+ }));
848
897
  build.onLoad({
849
898
  filter,
850
899
  namespace: 'virtual-share-module'
package/dist/package.json CHANGED
@@ -54,6 +54,8 @@
54
54
  "esm-cjs-lexer": "^0.10.0",
55
55
  "cjs-module-lexer": "^1.3.1",
56
56
  "@chialab/cjs-to-esm": "^0.18.0",
57
+ "es-module-lexer": "^1.5.3",
58
+ "@chialab/estransform": "^0.18.1",
57
59
  "@module-federation/sdk": "workspace:*",
58
60
  "json5": "^2.2.3",
59
61
  "@rollup/plugin-commonjs": "^22.0.2",
@@ -1,11 +1,2 @@
1
1
  export declare const resolve: (arg1: string, arg2: string) => Promise<string | false | undefined>;
2
2
  export declare function getExports(modulePath: string): Promise<string[]>;
3
- declare function collectExports(filePath: string): {
4
- hasDefaultExport: boolean;
5
- hasFurtherExports: boolean;
6
- defaultExportName: string;
7
- exports: string[];
8
- };
9
- declare function traverse(node: any, visit: (node: any) => void): void;
10
- declare function isDefaultExport(exportSpecifier: any): boolean;
11
- export { collectExports, traverse, isDefaultExport };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/esbuild",
3
- "version": "0.0.0-next-20240524010758",
3
+ "version": "0.0.0-next-20240524042957",
4
4
  "author": "Zack Jackson (@ScriptedAlchemy)",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "module": "./dist/index.esm.js",
@@ -54,6 +54,8 @@
54
54
  "esm-cjs-lexer": "^0.10.0",
55
55
  "cjs-module-lexer": "^1.3.1",
56
56
  "@chialab/cjs-to-esm": "^0.18.0",
57
+ "es-module-lexer": "^1.5.3",
58
+ "@chialab/estransform": "^0.18.1",
57
59
  "json5": "^2.2.3",
58
60
  "@rollup/plugin-commonjs": "^22.0.2",
59
61
  "@rollup/plugin-node-resolve": "^13.3.0",
@@ -63,6 +65,6 @@
63
65
  "esbuild": "^0.18.12",
64
66
  "npmlog": "^6.0.2",
65
67
  "acorn": "^8.8.1",
66
- "@module-federation/sdk": "0.0.0-next-20240524010758"
68
+ "@module-federation/sdk": "0.0.0-next-20240524042957"
67
69
  }
68
70
  }