• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MicroAppJS / plugin-webpack / 4647175977

pending completion
4647175977

push

github

Zyao89
bookmark: release v0.1.3

222 of 595 branches covered (37.31%)

Branch coverage included in aggregate %.

565 of 897 relevant lines covered (62.99%)

11.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.68
/src/extends/webpack/configParser.js
1
'use strict';
2

3
const { _, hash, tryRequire, path } = require('@micro-app/shared-utils');
4✔
4

5
const DEFAULT_KEY = 'main';
4✔
6

7
module.exports = function configParser(selfConfig, extraConfig = {}) {
4✔
8
    const originalConfig = selfConfig.originalConfig || {};
15!
9
    const webpackConfig = originalConfig.webpack || {};
15!
10

11
    function staticPaths() {
12
        // String | Array
13
        const staticPath = originalConfig.staticPath || [];
15✔
14
        const staticPaths = [];
15✔
15
        if (staticPath && typeof staticPath === 'string') {
15!
16
            staticPaths.push(staticPath);
×
17
        } else if (Array.isArray(staticPath)) {
15!
18
            staticPaths.push(...staticPath);
15✔
19
        }
20
        return staticPaths.filter(item => {
15✔
21
            return !!item;
×
22
        }).map(item => {
23
            if (!tryRequire.resolve(item)) {
×
24
                return path.resolve(selfConfig.root, item);
×
25
            }
26
            return item;
×
27
        });
28
    }
29

30
    // @Deprecated
31
    function htmls() {
32
        if (extraConfig.disabled) {
15!
33
            return [];
×
34
        }
35
        // 支持 array
36
        const htmls = originalConfig.htmls || (!originalConfig.html && webpackConfig.plugins && Array.isArray(webpackConfig.plugins) && webpackConfig.plugins.filter(item => {
15!
37
            const constru = item.constructor;
×
38
            if (constru && constru.name) {
×
39
                const constructorName = constru.name;
×
40
                if (constructorName === 'HtmlWebpackPlugin') {
×
41
                    return true;
×
42
                }
43
            }
44
            return false;
×
45
        }).map(item => item.options)) || []; // 兼容
×
46
        const _html = originalConfig.html; // 兼容
15✔
47
        if (_html && typeof _html === 'object') {
15!
48
            htmls.unshift(_html);
×
49
        }
50
        htmls.forEach(item => {
15✔
51
            if (item && item.template) {
15!
52
                const template = item.template;
15✔
53
                if (!tryRequire.resolve(template)) {
15✔
54
                    item.template = path.resolve(selfConfig.root, template);
4✔
55
                }
56
            }
57
        });
58
        return htmls;
15✔
59
    }
60

61
    function html() {
62
        if (extraConfig.disabled) {
15!
63
            return {};
×
64
        }
65
        // 支持 array
66
        const html = originalConfig.html || {};
15✔
67
        if (typeof html === 'object') {
15!
68
            Object.keys(html).forEach(key => {
15✔
69
                const _htmls = html[key];
×
70
                if (Array.isArray(_htmls)) {
×
71
                    html[key] = _htmls.map(item => {
×
72
                        if (!tryRequire.resolve(item.template)) {
×
73
                            item.template = path.resolve(selfConfig.root, item.template);
×
74
                        }
75
                        return item;
×
76
                    });
77
                } else if (typeof _htmls === 'string') {
×
78
                    if (!tryRequire.resolve(_htmls)) {
×
79
                        html[key] = {
×
80
                            template: path.resolve(selfConfig.root, _htmls),
81
                        };
82
                    }
83
                }
84
            });
85
        } else if (Array.isArray(html)) {
×
86
            return {
×
87
                [DEFAULT_KEY]: html.map(item => {
88
                    if (!tryRequire.resolve(item.template)) {
×
89
                        item.template = path.resolve(selfConfig.root, item.template);
×
90
                    }
91
                    return item;
×
92
                }),
93
            };
94

95
        } else if (typeof html === 'string') {
×
96
            return {
×
97
                [DEFAULT_KEY]: {
98
                    template: path.resolve(selfConfig.root, html),
99
                },
100
            };
101
        }
102
        return html;
15✔
103
    }
104

105
    function entry() {
106
        if (extraConfig.disabled) {
15!
107
            return {};
×
108
        }
109
        const entry = originalConfig.entry || webpackConfig.entry || {}; // 兼容
15!
110
        // fix entry path
111
        if (typeof entry === 'object') {
15!
112
            Object.keys(entry).forEach(key => {
15✔
113
                const _entrys = entry[key];
15✔
114
                if (Array.isArray(_entrys)) {
15✔
115
                    entry[key] = _entrys.map(item => {
11✔
116
                        if (!tryRequire.resolve(item)) {
11!
117
                            return path.resolve(selfConfig.root, item);
×
118
                        }
119
                        return item;
11✔
120
                    });
121
                } else if (typeof _entrys === 'string') {
4!
122
                    if (!tryRequire.resolve(_entrys)) {
4!
123
                        entry[key] = [ path.resolve(selfConfig.root, _entrys) ];
4✔
124
                    }
125
                }
126
            });
127
        } else if (Array.isArray(entry)) {
×
128
            return {
×
129
                [DEFAULT_KEY]: entry.map(item => {
130
                    if (!tryRequire.resolve(item)) {
×
131
                        return path.resolve(selfConfig.root, item);
×
132
                    }
133
                    return item;
×
134
                }),
135
            };
136
        } else if (typeof entry === 'string') {
×
137
            return {
×
138
                [DEFAULT_KEY]: [ path.resolve(selfConfig.root, entry) ],
139
            };
140
        }
141
        return entry;
15✔
142
    }
143

144
    function namespace() {
145
        const _namespace = selfConfig.namespace || hash(selfConfig.key);
×
146
        return _namespace;
×
147
    }
148

149
    return {
15✔
150
        staticPaths,
151
        htmls,
152
        html,
153
        entry,
154
        namespace,
155
    };
156
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc