Skip to content

Commit

Permalink
fix(mfsu): webpack export support (umijs#6894)
Browse files Browse the repository at this point in the history
* fix(mfsu): webpack export support

* fix(mfsu): get all export of webpack export

* fix: support webpack export object

* fix: static path

* fix: regexp

Co-authored-by: xiefengnian <[email protected]>
  • Loading branch information
xiefengnian and xiefengnian authored Jul 6, 2021
1 parent f32f969 commit 67cada7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ test('cjs mode esm parser', () => {
exports {Love};
exportsILoveYou = "1";
exports["something"] = undefined;
var index_0 = __webpack_exports__["dddfault"] = (createExtensibleEditor(editor_BraftEditor));
__webpack_require__.d( __webpack_exports__ , "getDecorators", function() { return getDecorators; });
__webpack_require__.d(__webpack_exports__, {
"qqqqqq": function() { return /* binding */ clipboard; },
"wwwwww": function() { return /* binding */ clipboard; },
"eeeeee" : ()=>{ {"ddd":123} }
});
`;
expect(cjsModeEsmParser(file)).toEqual([
'__esModule',
Expand All @@ -165,5 +176,10 @@ test('cjs mode esm parser', () => {
'Foo',
'default',
'something',
'dddfault',
'getDecorators',
'qqqqqq',
'wwwwww',
'eeeeee',
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,60 @@ export const cjsModeEsmParser = (code: string) => {
// ref: https://unpkg.alibaba-inc.com/browse/[email protected]/lib/core.js
matchAll(/exports(?:\.|\[(?:\'|\"))(\w+)(?:\s*|(?:\'|\")\])\s*\=/g, code),
)
.concat(
// Support __webpack_exports__["default"]
// ref: https://github.com/margox/braft-editor/blob/master/dist/index.js#L8429
matchAll(/__webpack_exports__\[(?:\"|\')(\w+)(?:\"|\')\]\s*\=/g, code),
)
.concat(
// Support __webpack_require__.d(__webpack_exports, "EditorState")
// ref: https://github.com/margox/braft-editor/blob/master/dist/index.js#L8347
matchAll(
/__webpack_require__\.d\(\s*__webpack_exports__\s*,\s*(?:\"|\')(\w+)(?:\"|\')\s*,/g,
code,
),
)
.concat(
// Support __webpack_require__.d(__webpack_exports__, {"default": function() { return /* binding */ clipboard; }});
// ref: https://unpkg.alibaba-inc.com/browse/[email protected]/dist/clipboard.js L26
...matchAll(
/__webpack_require__\.d\(\s*__webpack_exports__\s*,\s*(\{)/g,
code,
).map((matchResult) => {
const { index } = matchResult;

let idx = index;
let deep = 0;
let isMeetSymbol = false;
let symbolBeginIndex = index;

while (idx < code.length) {
if (!deep && isMeetSymbol) {
break;
}
if (code[idx] === '{') {
if (!isMeetSymbol) {
isMeetSymbol = true;
symbolBeginIndex = idx;
}
deep++;
}
if (code[idx] === '}') {
deep--;
}
idx++;
}

let result = code.slice(symbolBeginIndex, idx);

return [
...matchAll(
/(?:\"|\')(\w+)(?:\"|\')\s*\:\s*(?:function|\()/g,
result,
),
];
}),
)
.map((result) => result[1]);
};

Expand Down

0 comments on commit 67cada7

Please sign in to comment.