File size: 1,371 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transform = transform;
exports.hasBabel = hasBabel;
const load_module_1 = require("../../../utils/load-module");
const compat_1 = require("../../../utils/compat");
/**
 * Transpile with babel
 */
function transform(node, text, context) {
    const babel = loadBabel(context);
    if (!babel) {
        return null;
    }
    let inputRange;
    if (node.endTag) {
        inputRange = [node.startTag.range[1], node.endTag.range[0]];
    }
    else {
        inputRange = [node.startTag.range[1], node.range[1]];
    }
    const code = text.slice(...inputRange);
    try {
        const output = babel.transformSync(code, {
            sourceType: 'module',
            sourceMaps: true,
            minified: false,
            ast: false,
            code: true,
            cwd: (0, compat_1.getCwd)(context)
        });
        if (!output) {
            return null;
        }
        return {
            inputRange,
            output: output.code,
            mappings: output.map.mappings
        };
    }
    catch (_e) {
        return null;
    }
}
/** Check if project has Babel. */
function hasBabel(context) {
    return Boolean(loadBabel(context));
}
/**
 * Load babel
 */
function loadBabel(context) {
    return (0, load_module_1.loadModule)(context, '@babel/core');
}