Start sharing components as a team!Share components as a team!Join Bit to build your applications faster.Get Started Free

babel-base-compiler

v1.0.4arrow_drop_down
v1.0.4
v1.0.3
v1.0.2
v1.0.1
v1.0.0
v0.0.2
v0.0.1
STATUS
Passing
DOWNLOADS
7,536
LICENSE
MIT
VISIBILITY
Public
PUBLISHED
4 months ago
SIZE
237 KB
6 contributors
Install babel-base-compiler as a package?
Copied
npm i @bit/bit.envs.internal.babel-base-compiler
Set Bit as a scoped registryLearn more
npm config set '@bit:registry' https://node.bit.dev
Files
babelBaseCompiler/index.js
68 Lines(50 sloc)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
import {
    transform
} from '@babel/core';
import Vinyl from 'vinyl';
import path from 'path';
import groupBy from '@bit/bit.utils.object.group-by';
import getBabelOptions from '../getBabelOptions';

const _getDistFile = (file, distPath, content) => {
    let distFile = file.clone();
    distFile.base = distPath;
    distFile.path = path.join(distPath, file.relative);

    if (content) {
        distFile.contents = content;
    }

    return distFile;
}

const _runBabel = (file, options, distPath) => {
    options.configFile = false
    const {
        code,
        map
    } = transform(file.contents.toString(), options);

    const mappings = new Vinyl({
        contents: new Buffer(map.mappings),
        base: distPath,
        path: path.join(distPath, file.relative),
    });
    mappings.basename = file.basename + '.map';
    file.basename = path.parse(file.basename).name + '.js';

    const fileContent = code ?
        new Buffer(`${code}\n\n//# sourceMappingURL=${mappings.basename}`) :
        new Buffer(code);
    const distFile = _getDistFile(file, distPath, fileContent);

    return [mappings, distFile];
}

const baseCompile = (files, distPath, compilerPath, compiledFileTypes) => {
    // Get babel options from .babelrc file in actual compiler dir
    const options = getBabelOptions(compilerPath);

    // Divide files by whether we should compile them, according to file type.
    const filesByToCompile = groupBy(files, (file) => {
        return compiledFileTypes.indexOf(file.extname.replace('.', '')) > -1;
    });

    // Compile the ones we should
    const compiled = (!filesByToCompile.true || filesByToCompile.true.length === 0) ?
        [] :
        filesByToCompile.true.map(file =>
            _runBabel(file, options, distPath)).reduce((a, b) => a.concat(b));

    // We also need the non-compiled files as dist files
    const nonCompiled = !filesByToCompile.false ?
        [] :
        filesByToCompile.false.map(file => _getDistFile(file, distPath));

    // Compiler returns compiled AND non compiled dist files together
    return compiled.concat(nonCompiled);;
}

export default baseCompile;