Skip to content

Commit 8d7fc10

Browse files
authored
cjs-lexer: Handle webpack 3 minified umd (#841)
1 parent 7125ee2 commit 8d7fc10

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

packages/esm-cjslexer/src/cjs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ impl CJSLexer {
12121212
..
12131213
} = function.as_ref()
12141214
{
1215-
if let Some(Stmt::Decl(Decl::Fn(FnDecl { function, .. }))) = stmts.get(1) {
1215+
let mut check_function = |function: &Box<Function>| {
12161216
if let Function {
12171217
body: Some(BlockStmt { stmts, .. }),
12181218
..
@@ -1294,6 +1294,11 @@ impl CJSLexer {
12941294
}
12951295
}
12961296
}
1297+
};
1298+
if let Some(Stmt::Decl(Decl::Fn(FnDecl { function, .. }))) = stmts.get(0) {
1299+
check_function(function);
1300+
} else if let Some(Stmt::Decl(Decl::Fn(FnDecl { function, .. }))) = stmts.get(1) {
1301+
check_function(function);
12971302
}
12981303
}
12991304
}
@@ -1382,7 +1387,6 @@ impl CJSLexer {
13821387
},
13831388
..
13841389
}))) = stmts.get(first_stmt_index + 1)
1385-
// }))) = stmts.get(2)
13861390
{
13871391
let webpack_require_props =
13881392
self.get_webpack_require_props_from_stmts(stmts, webpack_require_sym);

packages/esm-cjslexer/src/test.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,77 @@ mod tests {
13581358
assert_eq!(exports.join(","), "__esModule,app");
13591359
}
13601360

1361+
#[test]
1362+
fn parse_cjs_exports_case_21_12() {
1363+
// Webpack 3 minified UMD output from https://github.com/highcharts/highcharts-react/blob/08e5ac19b9da5ba7d5b463919691e0b1db0fb34c/dist/highcharts-react.min.js#L1C1-L2C49
1364+
// with irrelevant parts manually removed.
1365+
// Formatted with Deno to avoid ast changes from prettier.
1366+
let source = r#"
1367+
!function (e, t) {
1368+
"object" == typeof exports && "object" == typeof module
1369+
? module.exports = t(require("react"))
1370+
: "function" == typeof define && define.amd
1371+
? define(["react"], t)
1372+
: "object" == typeof exports
1373+
? exports.HighchartsReact = t(require("react"))
1374+
: e.HighchartsReact = t(e.React);
1375+
}("undefined" != typeof self ? self : this, function (e) {
1376+
return function (e) {
1377+
function t(n) {
1378+
if (r[n]) return r[n].exports;
1379+
var o = r[n] = { i: n, l: !1, exports: {} };
1380+
return e[n].call(o.exports, o, o.exports, t), o.l = !0, o.exports;
1381+
}
1382+
var r = {};
1383+
return t.m = e,
1384+
t.c = r,
1385+
t.d = function (e, r, n) {
1386+
t.o(e, r) ||
1387+
Object.defineProperty(e, r, {
1388+
configurable: !1,
1389+
enumerable: !0,
1390+
get: n,
1391+
});
1392+
},
1393+
t.n = function (e) {
1394+
var r = e && e.__esModule
1395+
? function () {
1396+
return e.default;
1397+
}
1398+
: function () {
1399+
return e;
1400+
};
1401+
return t.d(r, "a", r), r;
1402+
},
1403+
t.o = function (e, t) {
1404+
return Object.prototype.hasOwnProperty.call(e, t);
1405+
},
1406+
t.p = "",
1407+
t(t.s = 0);
1408+
}([function (e, t, r) {
1409+
"use strict";
1410+
Object.defineProperty(t, "__esModule", { value: !0 }),
1411+
r.d(t, "HighchartsReact", function () {
1412+
return c;
1413+
});
1414+
var n = r(1),
1415+
o = r.n(n),
1416+
c = function (e) {
1417+
return o.a.createElement("div", e.containerProps);
1418+
};
1419+
t.default = c;
1420+
}, function (t, r) {
1421+
t.exports = e;
1422+
}]);
1423+
});
1424+
"#;
1425+
let swc = SWC::parse("index.cjs", source).expect("could not parse module");
1426+
let (exports, _) = swc
1427+
.parse_cjs_exports("production", true)
1428+
.expect("could not parse exports");
1429+
assert_eq!(exports.join(","), "HighchartsReact,default");
1430+
}
1431+
13611432
#[test]
13621433
fn parse_cjs_exports_case_22() {
13631434
let source = r#"

0 commit comments

Comments
 (0)