UNPKG

2.67 kBJavaScriptView Raw
1"use strict";
2var babel = require("@babel/core");
3var acorn = require("acorn");
4var log4js = require("log4js");
5var log;
6var walk;
7var isEs6 = function (ast) {
8 var es6NodeFound = false;
9 if (ast.body) {
10 var visitNode = function (node, state, c) {
11 if (!es6NodeFound) {
12 walk.base[node.type](node, state, c);
13 switch (node.type) {
14 case "ArrowFunctionExpression":
15 case "ClassDeclaration":
16 case "ExportAllDeclaration":
17 case "ExportDefaultDeclaration":
18 case "ExportNamedDeclaration":
19 case "ImportDeclaration":
20 es6NodeFound = true;
21 break;
22 case "VariableDeclaration":
23 if (node.kind === "const" || node.kind === "let") {
24 es6NodeFound = true;
25 break;
26 }
27 default:
28 }
29 }
30 };
31 walk.recursive(ast, null, {
32 Expression: visitNode,
33 Statement: visitNode
34 });
35 }
36 return es6NodeFound;
37};
38var configure = function (options) {
39 options = options || {};
40 if (!options.presets || options.presets.length === 0) {
41 options.presets = [["@babel/preset-env"]];
42 }
43 var transform = function (context, callback) {
44 if (!context.js) {
45 return callback(undefined, false);
46 }
47 if (isEs6(context.js.ast)) {
48 options.filename = context.filename;
49 log.debug("Transforming %s", options.filename);
50 try {
51 context.source = babel.transform(context.source, options).code;
52 context.js.ast = acorn.parse(context.source, { sourceType: "module" });
53 return callback(undefined, true);
54 }
55 catch (error) {
56 return callback(error, false);
57 }
58 }
59 else {
60 return callback(undefined, false);
61 }
62 };
63 var initialize = function (logOptions) {
64 log4js.configure({
65 appenders: logOptions.appenders,
66 categories: {
67 default: {
68 appenders: Object.keys(logOptions.appenders),
69 level: logOptions.level
70 }
71 }
72 });
73 log = log4js.getLogger("es6-transform.karma-typescript");
74 walk = require("acorn-walk");
75 };
76 return Object.assign(transform, { initialize: initialize });
77};
78module.exports = configure;
79//# sourceMappingURL=transform.js.map
\No newline at end of file