summaryrefslogtreecommitdiff
path: root/support/build/plugin-lodash-import-rename.js
blob: 6fe893bea26374b6d8505eb88eec7953a0d2172e (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
import _ from 'lodash';
import {dirname, sep} from 'path';

export default function() {
    return {
        visitor: {

            ImportDeclaration(path, mapping) {
                let {node} = path;
                let {value} = node.source;

                if (/\blodash\b/.test(value)) {
                    let f = mapping.file.opts.filename;
                    let dir = dirname(f).split(sep);
                    let relative = _.repeat('../', dir.length + 1);

                    node.source.value = value.replace(
                        /\blodash\b/,
                        relative + 'node_modules/lodash-es');
                }
            }
        }
    };
}