@module-federation/esbuild 0.0.0-next-20240530210646 → 0.0.2
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.
- package/README.md +13 -1
- package/dist/build.cjs.js +37 -27
- package/dist/build.esm.js +37 -27
- package/dist/package.json +4 -6
- package/dist/plugin.cjs.js +398 -55
- package/dist/plugin.esm.js +381 -56
- package/dist/src/adapters/lib/collect-exports.d.ts +1 -0
- package/dist/src/adapters/lib/commonjs.d.ts +101 -0
- package/dist/src/adapters/lib/lexer.d.ts +9 -0
- package/dist/src/adapters/lib/transform.d.ts +8 -0
- package/dist/src/adapters/lib/utils.d.ts +3 -0
- package/package.json +4 -6
package/dist/plugin.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import fs__default from 'fs';
|
|
1
|
+
import fs__default, { promises } from 'fs';
|
|
2
2
|
import { init, parse as parse$1 } from 'es-module-lexer';
|
|
3
3
|
import { init as init$1, parse } from 'cjs-module-lexer';
|
|
4
4
|
import { promisify } from 'util';
|
|
@@ -49,7 +49,7 @@ async function getExports(modulePath) {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
var version = "0.0.
|
|
52
|
+
var version = "0.0.2";
|
|
53
53
|
|
|
54
54
|
function _extends$1() {
|
|
55
55
|
_extends$1 = Object.assign || function(target) {
|
|
@@ -505,7 +505,9 @@ const createContainerPlugin = (config)=>({
|
|
|
505
505
|
const filter = new RegExp([
|
|
506
506
|
filename
|
|
507
507
|
].map((name)=>`${name}$`).join('|'));
|
|
508
|
-
const
|
|
508
|
+
const hasShared = Object.keys(config.shared || {}).length;
|
|
509
|
+
const shared = Object.keys(config.shared || {}).map((name)=>`${name}$`).join('|');
|
|
510
|
+
const sharedExternals = new RegExp(shared);
|
|
509
511
|
build.onResolve({
|
|
510
512
|
filter
|
|
511
513
|
}, async (args)=>({
|
|
@@ -526,24 +528,11 @@ const createContainerPlugin = (config)=>({
|
|
|
526
528
|
resolveDir: args.resolveDir
|
|
527
529
|
}
|
|
528
530
|
}));
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
path: args.path,
|
|
535
|
-
namespace: 'virtual-share-module',
|
|
536
|
-
pluginData: {
|
|
537
|
-
kind: args.kind,
|
|
538
|
-
resolveDir: args.resolveDir
|
|
539
|
-
}
|
|
540
|
-
};
|
|
541
|
-
});
|
|
542
|
-
build.onResolve({
|
|
543
|
-
filter: /.*/,
|
|
544
|
-
namespace: 'esm-shares'
|
|
545
|
-
}, async (args)=>{
|
|
546
|
-
if (sharedExternals.test(args.path)) {
|
|
531
|
+
if (hasShared) {
|
|
532
|
+
build.onResolve({
|
|
533
|
+
filter: sharedExternals
|
|
534
|
+
}, (args)=>{
|
|
535
|
+
if (args.namespace === 'esm-shares') return null;
|
|
547
536
|
return {
|
|
548
537
|
path: args.path,
|
|
549
538
|
namespace: 'virtual-share-module',
|
|
@@ -552,9 +541,24 @@ const createContainerPlugin = (config)=>({
|
|
|
552
541
|
resolveDir: args.resolveDir
|
|
553
542
|
}
|
|
554
543
|
};
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
|
|
544
|
+
});
|
|
545
|
+
build.onResolve({
|
|
546
|
+
filter: /.*/,
|
|
547
|
+
namespace: 'esm-shares'
|
|
548
|
+
}, async (args)=>{
|
|
549
|
+
if (sharedExternals.test(args.path)) {
|
|
550
|
+
return {
|
|
551
|
+
path: args.path,
|
|
552
|
+
namespace: 'virtual-share-module',
|
|
553
|
+
pluginData: {
|
|
554
|
+
kind: args.kind,
|
|
555
|
+
resolveDir: args.resolveDir
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
return undefined;
|
|
560
|
+
});
|
|
561
|
+
}
|
|
558
562
|
build.onLoad({
|
|
559
563
|
filter,
|
|
560
564
|
namespace: 'container'
|
|
@@ -595,7 +599,7 @@ const buildFederationHost = (config)=>{
|
|
|
595
599
|
return `
|
|
596
600
|
import { init as initFederationHost } from "@module-federation/runtime";
|
|
597
601
|
|
|
598
|
-
|
|
602
|
+
const createVirtualRemoteModule = (name, ref, exports) => {
|
|
599
603
|
const genExports = exports.map(e =>
|
|
600
604
|
e === 'default'
|
|
601
605
|
? 'export default mfLsZJ92.default;'
|
|
@@ -646,14 +650,14 @@ const buildFederationHost = (config)=>{
|
|
|
646
650
|
}
|
|
647
651
|
});
|
|
648
652
|
|
|
649
|
-
const
|
|
653
|
+
const mfHoZJ92 = initFederationHost({
|
|
650
654
|
name: ${JSON.stringify(name)},
|
|
651
655
|
remotes: ${remoteConfigs},
|
|
652
656
|
shared: ${sharedConfig},
|
|
653
657
|
plugins: [runtimePlugin()],
|
|
654
658
|
});
|
|
655
659
|
|
|
656
|
-
await Promise.all(
|
|
660
|
+
await Promise.all(mfHoZJ92.initializeSharing('default', 'version-first'));
|
|
657
661
|
|
|
658
662
|
|
|
659
663
|
`;
|
|
@@ -756,6 +760,331 @@ const linkRemotesPlugin = (config)=>({
|
|
|
756
760
|
}
|
|
757
761
|
});
|
|
758
762
|
|
|
763
|
+
// simplified from acorn (MIT license)
|
|
764
|
+
function isNewLine(code) {
|
|
765
|
+
return code === 10 || code === 13 || code === 0x2028 || code === 0x2029;
|
|
766
|
+
}
|
|
767
|
+
function codePointToString(ch) {
|
|
768
|
+
if (ch <= 0xffff) return String.fromCharCode(ch);
|
|
769
|
+
ch -= 0x10000;
|
|
770
|
+
return String.fromCharCode((ch >> 10) + 0xd800, (ch & 0x03ff) + 0xdc00);
|
|
771
|
+
}
|
|
772
|
+
class Lexer {
|
|
773
|
+
readString(input, pos) {
|
|
774
|
+
if (pos >= input.length) return null;
|
|
775
|
+
this.input = input;
|
|
776
|
+
this.pos = pos;
|
|
777
|
+
const quote = this.input.charCodeAt(pos);
|
|
778
|
+
if (!(quote === 34 || quote === 39)) return null;
|
|
779
|
+
let out = '';
|
|
780
|
+
let chunkStart = ++this.pos;
|
|
781
|
+
//eslint-disable-next-line no-constant-condition
|
|
782
|
+
while(true){
|
|
783
|
+
if (this.pos >= this.input.length) return null;
|
|
784
|
+
const ch = this.input.charCodeAt(this.pos);
|
|
785
|
+
if (ch === quote) break;
|
|
786
|
+
if (ch === 92) {
|
|
787
|
+
out += this.input.slice(chunkStart, this.pos);
|
|
788
|
+
const escaped = this.readEscapedChar();
|
|
789
|
+
if (escaped === null) return null;
|
|
790
|
+
out += escaped;
|
|
791
|
+
chunkStart = this.pos;
|
|
792
|
+
} else {
|
|
793
|
+
if (isNewLine(ch)) return null;
|
|
794
|
+
++this.pos;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
out += this.input.slice(chunkStart, this.pos++);
|
|
798
|
+
return out;
|
|
799
|
+
}
|
|
800
|
+
readEscapedChar() {
|
|
801
|
+
const ch = this.input.charCodeAt(++this.pos);
|
|
802
|
+
let code;
|
|
803
|
+
++this.pos;
|
|
804
|
+
switch(ch){
|
|
805
|
+
case 110:
|
|
806
|
+
return '\n';
|
|
807
|
+
case 114:
|
|
808
|
+
return '\r';
|
|
809
|
+
case 120:
|
|
810
|
+
code = this.readHexChar(2);
|
|
811
|
+
if (code === null) return null;
|
|
812
|
+
return String.fromCharCode(code);
|
|
813
|
+
case 117:
|
|
814
|
+
code = this.readCodePoint();
|
|
815
|
+
if (code === null) return null;
|
|
816
|
+
return codePointToString(code);
|
|
817
|
+
case 116:
|
|
818
|
+
return '\t';
|
|
819
|
+
case 98:
|
|
820
|
+
return '\b';
|
|
821
|
+
case 118:
|
|
822
|
+
return '\u000b';
|
|
823
|
+
case 102:
|
|
824
|
+
return '\f';
|
|
825
|
+
//@ts-ignore
|
|
826
|
+
case 13:
|
|
827
|
+
if (this.input.charCodeAt(this.pos) === 10) {
|
|
828
|
+
++this.pos;
|
|
829
|
+
}
|
|
830
|
+
// fall through
|
|
831
|
+
case 10:
|
|
832
|
+
return '';
|
|
833
|
+
case 56:
|
|
834
|
+
case 57:
|
|
835
|
+
return null;
|
|
836
|
+
default:
|
|
837
|
+
if (ch >= 48 && ch <= 55) {
|
|
838
|
+
const match = this.input.slice(this.pos - 1, this.pos + 2).match(/^[0-7]+/);
|
|
839
|
+
if (match === null) return null;
|
|
840
|
+
let octalStr = match[0];
|
|
841
|
+
let octal = parseInt(octalStr, 8);
|
|
842
|
+
if (octal > 255) {
|
|
843
|
+
octalStr = octalStr.slice(0, -1);
|
|
844
|
+
octal = parseInt(octalStr, 8);
|
|
845
|
+
}
|
|
846
|
+
this.pos += octalStr.length - 1;
|
|
847
|
+
const nextCh = this.input.charCodeAt(this.pos);
|
|
848
|
+
if (octalStr !== '0' || nextCh === 56 || nextCh === 57) return null;
|
|
849
|
+
return String.fromCharCode(octal);
|
|
850
|
+
}
|
|
851
|
+
if (isNewLine(ch)) return '';
|
|
852
|
+
return String.fromCharCode(ch);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
readInt(radix, len) {
|
|
856
|
+
const start = this.pos;
|
|
857
|
+
let total = 0;
|
|
858
|
+
for(let i = 0; i < len; ++i, ++this.pos){
|
|
859
|
+
const code = this.input.charCodeAt(this.pos);
|
|
860
|
+
let val;
|
|
861
|
+
if (code >= 97) {
|
|
862
|
+
val = code - 97 + 10;
|
|
863
|
+
} else if (code >= 65) {
|
|
864
|
+
val = code - 65 + 10;
|
|
865
|
+
} else if (code >= 48 && code <= 57) {
|
|
866
|
+
val = code - 48;
|
|
867
|
+
} else {
|
|
868
|
+
val = Infinity;
|
|
869
|
+
}
|
|
870
|
+
if (val >= radix) break;
|
|
871
|
+
total = total * radix + val;
|
|
872
|
+
}
|
|
873
|
+
if (this.pos === start || len != null && this.pos - start !== len) return null;
|
|
874
|
+
return total;
|
|
875
|
+
}
|
|
876
|
+
readHexChar(len) {
|
|
877
|
+
return this.readInt(16, len);
|
|
878
|
+
}
|
|
879
|
+
readCodePoint() {
|
|
880
|
+
const ch = this.input.charCodeAt(this.pos);
|
|
881
|
+
let code;
|
|
882
|
+
if (ch === 123) {
|
|
883
|
+
++this.pos;
|
|
884
|
+
code = this.readHexChar(this.input.indexOf('}', this.pos) - this.pos);
|
|
885
|
+
++this.pos;
|
|
886
|
+
if (code && code > 0x10ffff) return null;
|
|
887
|
+
} else {
|
|
888
|
+
code = this.readHexChar(4);
|
|
889
|
+
}
|
|
890
|
+
return code;
|
|
891
|
+
}
|
|
892
|
+
constructor(){
|
|
893
|
+
this.input = '';
|
|
894
|
+
this.pos = 0;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
function orderedUniq(array) {
|
|
899
|
+
// prettier-ignore
|
|
900
|
+
const ret = [], visited = new Set();
|
|
901
|
+
for (const val of array)if (!visited.has(val)) visited.add(val), ret.push(val);
|
|
902
|
+
return ret;
|
|
903
|
+
}
|
|
904
|
+
function cachedReduce(array, reducer, s) {
|
|
905
|
+
// prettier-ignore
|
|
906
|
+
const cache = [
|
|
907
|
+
s
|
|
908
|
+
];
|
|
909
|
+
let cacheLen = 1, last = s;
|
|
910
|
+
return (len)=>{
|
|
911
|
+
while(cacheLen <= len)cacheLen = cache.push(last = reducer(last, array[cacheLen - 1]));
|
|
912
|
+
return cache[len];
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
// from @rollup/pluginutils
|
|
916
|
+
const reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public';
|
|
917
|
+
const builtin = 'arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl';
|
|
918
|
+
const forbiddenIdentifiers = new Set(`${reservedWords} ${builtin}`.split(' '));
|
|
919
|
+
forbiddenIdentifiers.add('');
|
|
920
|
+
const makeLegalIdentifier = function makeLegalIdentifier(str) {
|
|
921
|
+
let identifier = str.replace(/-(\w)/g, (_, letter)=>letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, '_');
|
|
922
|
+
if (/\d/.test(identifier[0]) || forbiddenIdentifiers.has(identifier)) {
|
|
923
|
+
identifier = `_${identifier}`;
|
|
924
|
+
}
|
|
925
|
+
return identifier || '_';
|
|
926
|
+
};
|
|
927
|
+
|
|
928
|
+
function commonjs({ filter = /\.c?js$/, transform = true, transformConfig, requireReturnsDefault = true, ignore } = {}) {
|
|
929
|
+
const init_cjs_module_lexer = transform ? import('cjs-module-lexer') : undefined;
|
|
930
|
+
const use_default_export = typeof requireReturnsDefault === 'function' ? requireReturnsDefault : (_path)=>requireReturnsDefault;
|
|
931
|
+
const is_ignored = typeof ignore === 'function' ? ignore : Array.isArray(ignore) ? (path)=>ignore.includes(path) : ()=>false;
|
|
932
|
+
return {
|
|
933
|
+
name: 'commonjs',
|
|
934
|
+
setup ({ onLoad, esbuild, initialOptions }) {
|
|
935
|
+
let esbuild_shim;
|
|
936
|
+
const require_esbuild = ()=>esbuild || esbuild_shim || (esbuild_shim = require('esbuild'));
|
|
937
|
+
const read = promises.readFile;
|
|
938
|
+
const lexer = new Lexer();
|
|
939
|
+
//@ts-ignore
|
|
940
|
+
onLoad({
|
|
941
|
+
filter: filter
|
|
942
|
+
}, async (args)=>{
|
|
943
|
+
let parseCJS;
|
|
944
|
+
if (init_cjs_module_lexer) {
|
|
945
|
+
const { init, parse } = await init_cjs_module_lexer;
|
|
946
|
+
await init();
|
|
947
|
+
parseCJS = parse;
|
|
948
|
+
}
|
|
949
|
+
let contents;
|
|
950
|
+
try {
|
|
951
|
+
//@ts-ignore
|
|
952
|
+
contents = await read(args.path, 'utf8');
|
|
953
|
+
} catch (e) {
|
|
954
|
+
return null;
|
|
955
|
+
}
|
|
956
|
+
const willTransform = transform === true || typeof transform === 'function' && transform(args.path);
|
|
957
|
+
let cjsExports;
|
|
958
|
+
try {
|
|
959
|
+
if (parseCJS && willTransform) {
|
|
960
|
+
// move sourcemap to the end of the transformed file
|
|
961
|
+
const sourcemapIndex = contents.lastIndexOf('//# sourceMappingURL=');
|
|
962
|
+
let sourcemap;
|
|
963
|
+
if (sourcemapIndex !== -1) {
|
|
964
|
+
sourcemap = contents.slice(sourcemapIndex);
|
|
965
|
+
const sourcemapEnd = sourcemap.indexOf('\n');
|
|
966
|
+
if (sourcemapEnd !== -1 && sourcemap.slice(sourcemapEnd + 1).trimStart().length > 0) {
|
|
967
|
+
// if there's code after sourcemap, it is invalid, don't do this.
|
|
968
|
+
sourcemap = undefined;
|
|
969
|
+
} else {
|
|
970
|
+
contents = contents.slice(0, sourcemapIndex);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
// transform commonjs to es modules, easy mode
|
|
974
|
+
cjsExports = parseCJS(contents);
|
|
975
|
+
let { behavior, exports, sideEffects } = typeof willTransform === 'object' ? willTransform : {};
|
|
976
|
+
var _transformConfig_behavior;
|
|
977
|
+
behavior != null ? behavior : behavior = (_transformConfig_behavior = transformConfig == null ? void 0 : transformConfig.behavior) != null ? _transformConfig_behavior : 'node';
|
|
978
|
+
exports = orderedUniq(cjsExports.exports.concat(exports != null ? exports : []));
|
|
979
|
+
var _transformConfig_sideEffects;
|
|
980
|
+
sideEffects != null ? sideEffects : sideEffects = (_transformConfig_sideEffects = transformConfig == null ? void 0 : transformConfig.sideEffects) != null ? _transformConfig_sideEffects : true;
|
|
981
|
+
let exportDefault = behavior === 'node' ? 'export default exports;' : 'export default exports.__esModule ? exports.default : exports;';
|
|
982
|
+
let exportsMap = exports.map((e)=>[
|
|
983
|
+
e,
|
|
984
|
+
makeLegalIdentifier(e)
|
|
985
|
+
]);
|
|
986
|
+
if (exportsMap.some(([e])=>e === 'default')) {
|
|
987
|
+
if (behavior === 'node') {
|
|
988
|
+
exportsMap = exportsMap.filter(([e])=>e !== 'default');
|
|
989
|
+
} else {
|
|
990
|
+
exportDefault = '';
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
const reexports = cjsExports.reexports.map((e)=>`export * from ${JSON.stringify(e)};`).join('');
|
|
994
|
+
let transformed;
|
|
995
|
+
if (sideEffects === false) {
|
|
996
|
+
transformed = [
|
|
997
|
+
// make sure we don't manipulate the first line so that sourcemap is fine
|
|
998
|
+
reexports + 'var mod, exports = /* @__PURE__ */ ((exports, module) => {' + contents,
|
|
999
|
+
'return module.exports})((mod = { exports: {} }).exports, mod); ' + exportDefault
|
|
1000
|
+
];
|
|
1001
|
+
if (exportsMap.length > 0) {
|
|
1002
|
+
for (const [e, name] of exportsMap){
|
|
1003
|
+
transformed.push(`var ${name} = /* @__PURE__ */ (() => exports[${JSON.stringify(e)}])();`);
|
|
1004
|
+
}
|
|
1005
|
+
transformed.push(`export { ${exportsMap.map(([e, name])=>e === name ? e : `${name} as ${JSON.stringify(e)}`).join(', ')} };`);
|
|
1006
|
+
}
|
|
1007
|
+
} else {
|
|
1008
|
+
transformed = [
|
|
1009
|
+
reexports + 'var exports = {}, module = { exports }; {' + contents,
|
|
1010
|
+
'}; exports = module.exports; ' + exportDefault
|
|
1011
|
+
];
|
|
1012
|
+
if (exportsMap.length > 0) {
|
|
1013
|
+
transformed.push(`var { ${exportsMap.map(([e, name])=>e === name ? e : `${JSON.stringify(e)}: ${name}`).join(', ')} } = exports;`, `export { ${exportsMap.map(([e, name])=>e === name ? e : `${name} as ${JSON.stringify(e)}`).join(', ')} };`);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
contents = transformed.join('\n') + (sourcemap ? '\n' + sourcemap : '');
|
|
1017
|
+
}
|
|
1018
|
+
} catch (e) {
|
|
1019
|
+
return null;
|
|
1020
|
+
}
|
|
1021
|
+
function makeName(path) {
|
|
1022
|
+
let name = `__import_${makeLegalIdentifier(path)}`;
|
|
1023
|
+
if (contents.includes(name)) {
|
|
1024
|
+
let suffix = 2;
|
|
1025
|
+
while(contents.includes(`${name}${suffix}`))suffix++;
|
|
1026
|
+
name = `${name}${suffix}`;
|
|
1027
|
+
}
|
|
1028
|
+
return name;
|
|
1029
|
+
}
|
|
1030
|
+
let warnings;
|
|
1031
|
+
try {
|
|
1032
|
+
({ warnings } = await require_esbuild().transform(contents, {
|
|
1033
|
+
format: 'esm',
|
|
1034
|
+
logLevel: 'silent'
|
|
1035
|
+
}));
|
|
1036
|
+
} catch (err) {
|
|
1037
|
+
({ warnings } = err);
|
|
1038
|
+
}
|
|
1039
|
+
const lines = contents.split('\n');
|
|
1040
|
+
const getOffset = cachedReduce(lines, (a, b)=>a + 1 + b.length, 0);
|
|
1041
|
+
if (warnings && (warnings = warnings.filter((e)=>e.text.includes('"require" to "esm"'))).length) {
|
|
1042
|
+
const edits = [];
|
|
1043
|
+
let imports = [];
|
|
1044
|
+
for (const { location } of warnings){
|
|
1045
|
+
if (location === null) continue;
|
|
1046
|
+
const { line, lineText, column, length } = location;
|
|
1047
|
+
const leftBrace = column + length + 1;
|
|
1048
|
+
const path = lexer.readString(lineText, leftBrace);
|
|
1049
|
+
if (path === null || is_ignored(path)) continue;
|
|
1050
|
+
const rightBrace = lineText.indexOf(')', leftBrace + 2 + path.length) + 1;
|
|
1051
|
+
const name = makeName(path);
|
|
1052
|
+
let import_statement;
|
|
1053
|
+
if (use_default_export(path)) {
|
|
1054
|
+
import_statement = `import ${name} from ${JSON.stringify(path)};`;
|
|
1055
|
+
} else {
|
|
1056
|
+
import_statement = `import * as ${name} from ${JSON.stringify(path)};`;
|
|
1057
|
+
}
|
|
1058
|
+
const offset = getOffset(line - 1);
|
|
1059
|
+
edits.push([
|
|
1060
|
+
offset + column,
|
|
1061
|
+
offset + rightBrace,
|
|
1062
|
+
name
|
|
1063
|
+
]);
|
|
1064
|
+
imports.push(import_statement);
|
|
1065
|
+
}
|
|
1066
|
+
if (imports.length === 0) return null;
|
|
1067
|
+
imports = orderedUniq(imports);
|
|
1068
|
+
let offset = 0;
|
|
1069
|
+
for (const [start, end, name] of edits){
|
|
1070
|
+
contents = contents.slice(0, start + offset) + name + contents.slice(end + offset);
|
|
1071
|
+
offset += name.length - (end - start);
|
|
1072
|
+
}
|
|
1073
|
+
// if we have transformed this module (i.e. having `cjsExports`), don't make the file commonjs
|
|
1074
|
+
contents = [
|
|
1075
|
+
...imports,
|
|
1076
|
+
cjsExports ? 'exports;' : '',
|
|
1077
|
+
contents
|
|
1078
|
+
].join('');
|
|
1079
|
+
return {
|
|
1080
|
+
contents
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
|
|
759
1088
|
function _extends() {
|
|
760
1089
|
_extends = Object.assign || function(target) {
|
|
761
1090
|
for(var i = 1; i < arguments.length; i++){
|
|
@@ -789,36 +1118,32 @@ const cjsToEsmPlugin = {
|
|
|
789
1118
|
filter: /.*/,
|
|
790
1119
|
namespace: 'esm-shares'
|
|
791
1120
|
}, async (args)=>{
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
};
|
|
810
|
-
}
|
|
811
|
-
} catch (e) {
|
|
812
|
-
return {
|
|
813
|
-
contents: fileContent,
|
|
814
|
-
loader: 'js',
|
|
815
|
-
resolveDir: path__default.dirname(resolver),
|
|
816
|
-
pluginData: _extends({}, args.pluginData, {
|
|
817
|
-
path: resolver
|
|
1121
|
+
var _build_initialOptions_external;
|
|
1122
|
+
let esbuild_shim;
|
|
1123
|
+
const require_esbuild = ()=>build.esbuild || esbuild_shim || (esbuild_shim = require('esbuild'));
|
|
1124
|
+
const packageBuilder = await require_esbuild().build(_extends({}, build.initialOptions, {
|
|
1125
|
+
external: (_build_initialOptions_external = build.initialOptions.external) == null ? void 0 : _build_initialOptions_external.filter((e)=>{
|
|
1126
|
+
if (e.includes('*')) {
|
|
1127
|
+
const prefix = e.split('*')[0];
|
|
1128
|
+
return !args.path.startsWith(prefix);
|
|
1129
|
+
}
|
|
1130
|
+
return e !== args.path;
|
|
1131
|
+
}),
|
|
1132
|
+
entryPoints: [
|
|
1133
|
+
args.path
|
|
1134
|
+
],
|
|
1135
|
+
plugins: [
|
|
1136
|
+
commonjs({
|
|
1137
|
+
filter: /.*/
|
|
818
1138
|
})
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
1139
|
+
],
|
|
1140
|
+
write: false
|
|
1141
|
+
}));
|
|
1142
|
+
return {
|
|
1143
|
+
contents: packageBuilder.outputFiles[0].text,
|
|
1144
|
+
loader: 'js',
|
|
1145
|
+
resolveDir: args.pluginData.resolveDir
|
|
1146
|
+
};
|
|
822
1147
|
});
|
|
823
1148
|
}
|
|
824
1149
|
};
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export declare const resolve: (arg1: string, arg2: string) => Promise<string | false | undefined>;
|
|
2
|
+
export declare const resolvePackageJson: (packageName: string, callback: (err: Error | null, result?: string) => void) => Promise<void>;
|
|
2
3
|
export declare function getExports(modulePath: string): Promise<string[]>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { Plugin } from 'esbuild';
|
|
2
|
+
export interface CommonJSOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The regexp passed to onLoad() to match commonjs files.
|
|
5
|
+
*
|
|
6
|
+
* @default /\.c?js$/
|
|
7
|
+
*/
|
|
8
|
+
filter?: RegExp;
|
|
9
|
+
/**
|
|
10
|
+
* _Experimental_: Transform commonjs to es modules. You have to install
|
|
11
|
+
* `cjs-module-lexer` to let it work.
|
|
12
|
+
*
|
|
13
|
+
* When `true`, the plugin tries to wrap the commonjs module into:
|
|
14
|
+
*
|
|
15
|
+
* ```js
|
|
16
|
+
* var exports = {}, module = { exports };
|
|
17
|
+
* {
|
|
18
|
+
* // ... original content ...
|
|
19
|
+
* }
|
|
20
|
+
* exports = module.exports;
|
|
21
|
+
* // the exported names are extracted by cjs-module-lexer
|
|
22
|
+
* export default exports;
|
|
23
|
+
* var { something, "a-b" as a_b } = exports;
|
|
24
|
+
* export { something, a_b as "a-b" };
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @default false
|
|
28
|
+
*/
|
|
29
|
+
transform?: boolean | ((path: string) => boolean | TransformConfig | null | void);
|
|
30
|
+
/**
|
|
31
|
+
* _Experimental_: This options acts as a fallback of the `transform` option above.
|
|
32
|
+
*/
|
|
33
|
+
transformConfig?: Pick<TransformConfig, 'behavior' | 'sideEffects'>;
|
|
34
|
+
/**
|
|
35
|
+
* Controls which style of import should be used. By default, it transforms:
|
|
36
|
+
*
|
|
37
|
+
* ```js
|
|
38
|
+
* // input
|
|
39
|
+
* const foo = require("foo")
|
|
40
|
+
* // output
|
|
41
|
+
* import foo from "foo"
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* The above case is often correct when 'foo' is also a commonjs module.
|
|
45
|
+
* But if 'foo' has es module exports, it is better to use:
|
|
46
|
+
*
|
|
47
|
+
* ```js
|
|
48
|
+
* // output
|
|
49
|
+
* import * as foo from "foo"
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* In which case you can set `requireReturnsDefault` to `false` to get the above output.
|
|
53
|
+
* Or use the callback style to control the behavior for each module.
|
|
54
|
+
*
|
|
55
|
+
* @default true
|
|
56
|
+
*/
|
|
57
|
+
requireReturnsDefault?: boolean | ((path: string) => boolean);
|
|
58
|
+
/**
|
|
59
|
+
* Don't replace require("ignored-modules"). Note that this will cause
|
|
60
|
+
* esbuild generates the __require() wrapper which throw error at runtime.
|
|
61
|
+
*/
|
|
62
|
+
ignore?: string[] | ((path: string) => boolean);
|
|
63
|
+
}
|
|
64
|
+
export interface TransformConfig {
|
|
65
|
+
/**
|
|
66
|
+
* If `"babel"`, it will check if there be `exports.__esModule`,
|
|
67
|
+
* then export `exports.default`. i.e. The wrapper code becomes:
|
|
68
|
+
*
|
|
69
|
+
* ```js
|
|
70
|
+
* export default exports.__esModule ? exports.default : exports;
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @default "node"
|
|
74
|
+
*/
|
|
75
|
+
behavior?: 'babel' | 'node';
|
|
76
|
+
/**
|
|
77
|
+
* Also include these named exports if they aren't recognized automatically.
|
|
78
|
+
*
|
|
79
|
+
* @example ["something"]
|
|
80
|
+
*/
|
|
81
|
+
exports?: string[];
|
|
82
|
+
/**
|
|
83
|
+
* If `false`, slightly change the result to make it side-effect free.
|
|
84
|
+
* But it doesn't actually remove many code. So you maybe not need this.
|
|
85
|
+
*
|
|
86
|
+
* ```js
|
|
87
|
+
* var mod;
|
|
88
|
+
* var exports = /\*#__PURE__*\/ ((exports, module) => {
|
|
89
|
+
* // ... original content ...
|
|
90
|
+
* return module.exports;
|
|
91
|
+
* })((mod = { exports: {} }).exports, mod);
|
|
92
|
+
* export default exports;
|
|
93
|
+
* var a_b = /\*#__PURE__*\/ (() => exports['a-b'])();
|
|
94
|
+
* var something = /\*#__PURE__*\/ (() => exports.something)();
|
|
95
|
+
* export { a_b as "a-b", something };
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
sideEffects?: boolean;
|
|
99
|
+
}
|
|
100
|
+
export declare function commonjs({ filter, transform, transformConfig, requireReturnsDefault, ignore, }?: CommonJSOptions): Plugin;
|
|
101
|
+
export default commonjs;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare class Lexer {
|
|
2
|
+
input: string;
|
|
3
|
+
pos: number;
|
|
4
|
+
readString(input: string, pos: number): string | null;
|
|
5
|
+
readEscapedChar(): string | null;
|
|
6
|
+
readInt(radix: number, len: number): number | null;
|
|
7
|
+
readHexChar(len: number): number | null;
|
|
8
|
+
readCodePoint(): number | null;
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/esbuild",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"author": "Zack Jackson (@ScriptedAlchemy)",
|
|
5
5
|
"main": "./dist/index.cjs.js",
|
|
6
6
|
"module": "./dist/index.esm.js",
|
|
@@ -51,20 +51,18 @@
|
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"enhanced-resolve": "^5.16.1",
|
|
54
|
-
"esm-cjs-lexer": "^0.10.0",
|
|
55
54
|
"cjs-module-lexer": "^1.3.1",
|
|
56
|
-
"@chialab/cjs-to-esm": "^0.18.0",
|
|
57
55
|
"es-module-lexer": "^1.5.3",
|
|
58
|
-
"@chialab/estransform": "^0.18.1",
|
|
59
56
|
"json5": "^2.2.3",
|
|
60
57
|
"@rollup/plugin-commonjs": "^22.0.2",
|
|
61
58
|
"@rollup/plugin-node-resolve": "^13.3.0",
|
|
62
59
|
"@rollup/plugin-replace": "^4.0.0",
|
|
63
60
|
"rollup": "^2.79.0",
|
|
61
|
+
"@chialab/esbuild-plugin-commonjs": "^0.18.0",
|
|
62
|
+
"@hyrious/esbuild-plugin-commonjs": "^0.2.4",
|
|
64
63
|
"rollup-plugin-node-externals": "^4.1.1",
|
|
65
64
|
"esbuild": "^0.18.12",
|
|
66
65
|
"npmlog": "^6.0.2",
|
|
67
|
-
"
|
|
68
|
-
"@module-federation/sdk": "0.1.16"
|
|
66
|
+
"@module-federation/sdk": "0.2.3"
|
|
69
67
|
}
|
|
70
68
|
}
|