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

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,10 @@ 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
+ await build.resolve(args.path, {
596
+ resolveDir: args.resolveDir
597
+ });
598
+ return undefined;
626
599
  });
627
600
  build.onLoad({
628
601
  filter,
@@ -859,15 +832,26 @@ const cjsToEsmPlugin = {
859
832
  throw new Error(`Unable to resolve path: ${args.path}`);
860
833
  }
861
834
  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
- };
835
+ try {
836
+ const { code } = await transform(fileContent);
837
+ return {
838
+ contents: code,
839
+ loader: 'js',
840
+ resolveDir: path__default["default"].dirname(resolver),
841
+ pluginData: _extends$1({}, args.pluginData, {
842
+ path: resolver
843
+ })
844
+ };
845
+ } catch (e) {
846
+ return {
847
+ contents: fileContent,
848
+ loader: 'js',
849
+ resolveDir: path__default["default"].dirname(resolver),
850
+ pluginData: _extends$1({}, args.pluginData, {
851
+ path: resolver
852
+ })
853
+ };
854
+ }
871
855
  });
872
856
  }
873
857
  };
@@ -875,7 +859,56 @@ const cjsToEsmPlugin = {
875
859
  const linkSharedPlugin = {
876
860
  name: 'linkShared',
877
861
  setup (build) {
878
- const filter = new RegExp(federationBuilder.federationBuilder.externals.map((name)=>`${name}$`).join('|'));
862
+ const filter = new RegExp(Object.keys(federationBuilder.federationBuilder.config.shared || {}).map((name)=>`${name}$`).join('|'));
863
+ build.onResolve({
864
+ filter
865
+ }, (args)=>{
866
+ if (args.namespace === 'esm-shares') return null;
867
+ return {
868
+ path: args.path,
869
+ namespace: 'virtual-share-module',
870
+ pluginData: {
871
+ kind: args.kind,
872
+ resolveDir: args.resolveDir
873
+ }
874
+ };
875
+ });
876
+ build.onResolve({
877
+ filter: /.*/,
878
+ namespace: 'esm-shares'
879
+ }, (args)=>{
880
+ if (filter.test(args.path)) {
881
+ return {
882
+ path: args.path,
883
+ namespace: 'virtual-share-module',
884
+ pluginData: {
885
+ kind: args.kind,
886
+ resolveDir: args.resolveDir
887
+ }
888
+ };
889
+ }
890
+ if (filter.test(args.importer)) {
891
+ return {
892
+ path: args.path,
893
+ namespace: 'esm-shares',
894
+ pluginData: {
895
+ kind: args.kind,
896
+ resolveDir: args.resolveDir
897
+ }
898
+ };
899
+ }
900
+ return undefined;
901
+ });
902
+ build.onResolve({
903
+ filter: /^federationShare/
904
+ }, async (args)=>({
905
+ path: args.path.replace('federationShare/', ''),
906
+ namespace: 'esm-shares',
907
+ pluginData: {
908
+ kind: args.kind,
909
+ resolveDir: args.resolveDir
910
+ }
911
+ }));
879
912
  build.onLoad({
880
913
  filter,
881
914
  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,10 @@ 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
+ await build.resolve(args.path, {
584
+ resolveDir: args.resolveDir
585
+ });
586
+ return undefined;
595
587
  });
596
588
  build.onLoad({
597
589
  filter,
@@ -828,15 +820,26 @@ const cjsToEsmPlugin = {
828
820
  throw new Error(`Unable to resolve path: ${args.path}`);
829
821
  }
830
822
  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
- };
823
+ try {
824
+ const { code } = await transform(fileContent);
825
+ return {
826
+ contents: code,
827
+ loader: 'js',
828
+ resolveDir: path__default.dirname(resolver),
829
+ pluginData: _extends$1({}, args.pluginData, {
830
+ path: resolver
831
+ })
832
+ };
833
+ } catch (e) {
834
+ return {
835
+ contents: fileContent,
836
+ loader: 'js',
837
+ resolveDir: path__default.dirname(resolver),
838
+ pluginData: _extends$1({}, args.pluginData, {
839
+ path: resolver
840
+ })
841
+ };
842
+ }
840
843
  });
841
844
  }
842
845
  };
@@ -844,7 +847,56 @@ const cjsToEsmPlugin = {
844
847
  const linkSharedPlugin = {
845
848
  name: 'linkShared',
846
849
  setup (build) {
847
- const filter = new RegExp(federationBuilder.externals.map((name)=>`${name}$`).join('|'));
850
+ const filter = new RegExp(Object.keys(federationBuilder.config.shared || {}).map((name)=>`${name}$`).join('|'));
851
+ build.onResolve({
852
+ filter
853
+ }, (args)=>{
854
+ if (args.namespace === 'esm-shares') return null;
855
+ return {
856
+ path: args.path,
857
+ namespace: 'virtual-share-module',
858
+ pluginData: {
859
+ kind: args.kind,
860
+ resolveDir: args.resolveDir
861
+ }
862
+ };
863
+ });
864
+ build.onResolve({
865
+ filter: /.*/,
866
+ namespace: 'esm-shares'
867
+ }, (args)=>{
868
+ if (filter.test(args.path)) {
869
+ return {
870
+ path: args.path,
871
+ namespace: 'virtual-share-module',
872
+ pluginData: {
873
+ kind: args.kind,
874
+ resolveDir: args.resolveDir
875
+ }
876
+ };
877
+ }
878
+ if (filter.test(args.importer)) {
879
+ return {
880
+ path: args.path,
881
+ namespace: 'esm-shares',
882
+ pluginData: {
883
+ kind: args.kind,
884
+ resolveDir: args.resolveDir
885
+ }
886
+ };
887
+ }
888
+ return undefined;
889
+ });
890
+ build.onResolve({
891
+ filter: /^federationShare/
892
+ }, async (args)=>({
893
+ path: args.path.replace('federationShare/', ''),
894
+ namespace: 'esm-shares',
895
+ pluginData: {
896
+ kind: args.kind,
897
+ resolveDir: args.resolveDir
898
+ }
899
+ }));
848
900
  build.onLoad({
849
901
  filter,
850
902
  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-20240524034417",
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-20240524034417"
67
69
  }
68
70
  }