summaryrefslogtreecommitdiff
path: root/support/build/compile-modules.js
blob: 583f83d066d325c63e745ef3acba5a8eb42568aa (plain)
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
const async = require('async');
const {transformFile} = require('babel-core');
const _ = require('lodash');
const readdirR = require('recursive-readdir');
const pluginCJS = require('babel-plugin-transform-es2015-modules-commonjs');
const pluginModuleExports = require('babel-plugin-add-module-exports');
const pluginLodashImportRename = require('./plugin-lodash-import-rename');
const joinPath = require('path').join;
const fs = require('fs-extra');

module.exports = function(options, cb) {
    options = _.defaults({}, options, {
        path: 'lib/',
        outpath: 'build',
        es6: false,
        lodashRename: false
    });
    const plugins = [];
    const { path, outpath } = options;

    if (options.lodashRename) {
        plugins.push(pluginLodashImportRename);
    }
    if (!options.es6) {
        plugins.push(pluginModuleExports);
        plugins.push(pluginCJS);
    }

    readdirR(path, [], (err, files) => {
        fs.emptyDirSync(outpath);
        fs.emptyDirSync(joinPath(outpath, 'internal'));
        async.each(files, (file, callback) => {

            transformFile(file, {
                babelrc: false,
                plugins: plugins
            }, (err, content) => {
                if (err) { return callback(err); }
                const outfile = file.startsWith(path) ?
                    file.slice(path.length) :
                    file;
                const finalPath = joinPath(outpath, outfile);
                fs.writeFile(finalPath, content.code, callback);
            });
        }, cb);
    });
}