summaryrefslogtreecommitdiff
path: root/support/build/compile-modules.js
blob: 17d34ecd766f8131d964f56c8a7f2165c2282b6f (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
import async from '../../lib';
import {transformFile} from 'babel-core';
import _ from 'lodash';
import readdirR from 'recursive-readdir';
import pluginCJS from 'babel-plugin-transform-es2015-modules-commonjs';
import pluginLodashImportRename from './plugin-lodash-import-rename';
import {join as joinPath} from 'path';
import fs from 'fs-extra';

export default function(cb, options) {
    options = _.defaults({}, options, {path:'lib/', outpath:'build/modules', es6: false, lodashRename: false});
    let plugins = [];

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

    readdirR(options.path, [], function(err, files) {
        fs.emptyDirSync(options.outpath);
        fs.emptyDirSync(joinPath(options.outpath, 'internal'));
        async.each(files, (file, callback) => {
            var filename = file.startsWith(options.path) ? file.slice(options.path.length) : file;
            transformFile(file, {
                babelrc: false,
                plugins: plugins
            }, function(err, content) {
                fs.writeFile(joinPath(options.outpath, filename), content.code, callback);
            });
        }, cb);
    });
}