๐Ÿ“ฆ YMFE / ykit

๐Ÿ“„ ConfigProcessCircle.js ยท 70 lines
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
69
70const ConfigConverter = require('./ConfigConverter.js');

module.exports = {
    runTasksBeforeCompiling(hooks, webpackConfig) {

        webpackConfig = ConfigConverter(webpackConfig);

        return new Promise ((resolve, reject) => {
            async.series(
                hooks.beforeCompiling.map((beforeTask) => {
                    return (callback) => {
                        let isAsync = false;
                        beforeTask.bind({
                            async: () => {
                                isAsync = true;
                                return callback;
                            }
                        })(webpackConfig);

                        if(!isAsync) {
                            callback(null);
                        }
                    };
                }),
                (err) => {
                    if(err) {
                        logError(err);
                        process.exit(1);
                    }

                    const results = removeDuplicateBabelLoader(
                        webpackConfig.module.rules,
                        webpackConfig.plugins
                    );
                    webpackConfig.module.rules = results.rules;
                    webpackConfig.plugins = results.plugins;

                    resolve(webpackConfig);
                }
            );
        });

        function removeDuplicateBabelLoader(rules, plugins) {
            const babelExists = rules.some((rule) => {
                const isFromYkit = rule.test.toString().match(/__ykit__/);

                if(isFromYkit) {
                    return false;
                } else {
                    const isRuleForJS = rule.test.toString().match(/js/);
                    const ruleUse = typeof rule.use === 'string' ? rule.use : rule.use.join();
                    const isUsingBabel = ruleUse.includes('babel') || ruleUse.includes('happypack');
                    return isRuleForJS && isUsingBabel;
                }
            });

            if(babelExists) {
                rules = rules.filter((rule) => {
                    return !rule.test.toString().match(/__ykit__/);
                });
                plugins = plugins.filter((plugin) => {
                    return !plugin.__ykit__;
                });
            }

            return {rules, plugins};
        }
    }
};