UNPKG

6.16 kBJavaScriptView Raw
1const _ = require('lodash');
2const path = require('path');
3const chalk = require('chalk');
4const fs = require('fs');
5const resolveApp = require('../tools').resolveApp;
6
7
8
9const ENV = process.env.NODE_ENV === 'development' ? 'dev' : 'prod';
10const webpackConfig = (function getCustomConfig() {
11 const customConfigPath = path.resolve(`./config/webpack.config.${ENV}.js`);
12 return fs.existsSync(customConfigPath) ? require(customConfigPath) : {}
13})();
14
15
16/**
17 * get filenames from config.output.filenames or config/filenames.js
18 *
19 * @param webpackConfig
20 * {
21 * ...,
22 * output: {
23 * filenames: {
24 * js: 'static/js/[name].js',
25 * jsChunk: 'static/js/[name].chunk.js',
26 * css: '', // 在<style>中,无需配置
27 * img: 'static/img/[name].[hash:8].[ext]',
28 * media: 'static/media/[name].[hash:8].[ext]'
29 * }
30 *
31 * },
32 * ...
33 * }
34 *
35 * @return {object} filenames
36 */
37function getFilenames(webpackConfig) {
38
39 let filenames = _.get(webpackConfig, ['output', 'filenames']);
40
41 if (filenames && !_.isEmpty(filenames)) {
42 return filenames
43 }
44
45 // deprecated
46 log(chalk.yellow('\n warning: config/filenames.js is deprecated, please use webpackconfig.output.filenames instead \n'));
47
48 try {
49 const env = process.env.NODE_ENV === 'development' ? 'dev' : 'prod';
50 return require(path.resolve('config/filenames'))[env]
51 } catch (e){
52
53 }
54
55 // show missing error
56 if (_.isEmpty(filenames)) {
57 console.log(chalk.red('\n warning: webpackconfig.output.filenames is missing!'));
58 process.exit(1);
59 }
60
61}
62
63/**
64 * get publicPath from config.output.publicPath or config/cndPath.js
65 *
66 * @param webpackConfig
67 * {
68 * ...,
69 * output: {
70 * // string
71 * publicPath: 'path'
72 * // or obj (only prod)
73 * publicPath: {
74 * js: 'path',
75 * css: 'path',
76 * img: 'path',
77 * media: 'path'
78 * }
79 *
80 * },
81 * ...
82 * }
83 *
84 *
85 * @return {*} publicPath [object | null]
86 */
87function getCdnPath(webpackConfig) {
88
89 const defaultPublicPath = '';
90 const publicPath = _.get(webpackConfig, ['output', 'publicPath']);
91
92 let cdnConfig = {};
93
94
95 if (_.isString(publicPath)) {
96 return null
97 }
98
99
100 if (ENV === 'prod') {
101
102 if (_.isPlainObject(publicPath)) {
103
104 cdnConfig = publicPath;
105
106 return {
107 js: cdnConfig.js || defaultPublicPath,
108 css: cdnConfig.css || defaultPublicPath,
109 img: cdnConfig.img || defaultPublicPath,
110 media: cdnConfig.media || defaultPublicPath
111 }
112 }
113
114 // deprecated
115 try{
116 cdnConfig = require(path.resolve('config/cdnPath'));
117 cdnConfig && log(chalk.yellow('\n warning: config/cdnPath.js is deprecated, please use webpackconfig.output.publicPath instead \n'));
118 return {
119 js: cdnConfig.prodJsCDN || defaultPublicPath,
120 css: cdnConfig.prodCssCDN || defaultPublicPath,
121 img: cdnConfig.prodImgCDN || defaultPublicPath,
122 media: cdnConfig.prodMediaCDN || defaultPublicPath
123 }
124 }catch(e){
125
126 // show missing error
127 if (!_.isEmpty(webpackConfig) && _.isEmpty(publicPath)) {
128 log(chalk.yellow('\n warning: webpackconfig.output.publicPath is missing!'));
129 return null
130 }
131
132 }
133
134
135 }
136
137
138
139
140
141}
142
143/**
144 * get alias from config.resolve.alias or config/alias.js
145 *
146 * @param webpackConfig
147 * {
148 * ...,
149 * resolve: {
150 * alias: {
151 * // custom config
152 * }
153 * },
154 * ...
155 * }
156 *
157 * @return {object} alias
158 */
159function getAliasConfig(webpackConfig) {
160 let defaultConfig = {
161 commons: path.resolve('src/components_common/'),
162 tools: path.resolve('src/tools/'),
163 api: path.resolve('src/api/'),
164 config: path.resolve('src/config'),
165 public: path.resolve('public/'),
166 scss: path.resolve('src/scss_mixin/scss/'),
167 scss_mixin: path.resolve('src/scss_mixin/'),
168 };
169
170 let alias = _.get(webpackConfig, ['resolve', 'alias']);
171
172 if (_.isEmpty(alias)) {
173 alias = defaultConfig;
174
175 log(chalk.yellow('\n warning: ' + "config/alias.js is deprecated. You can use alias in webpackconfig.resolve.alias." + '\n'));
176
177 try{
178 alias = require(path.resolve('config/alias'));
179 }catch(e){
180 }
181 }
182
183
184 return alias
185
186}
187
188/**
189 * get cssModule from config.cssModule
190 *
191 * @param webpackConfig
192 * {
193 * ...,
194 * cssModule: {
195 exclude: 'src/static',
196 name: '[name]__[local]-[hash:base64:5]'
197 },
198 ...
199 * }
200 *
201 * @return {object}
202 * {
203 * exclude: 'path',
204 * name: '',
205 * enable: false / true
206 * }
207 */
208function getCssModuleConfig(webpackConfig) {
209 let cssModule = _.get(webpackConfig, 'cssModule');
210
211 if (_.isEmpty(cssModule)) {
212 return {
213 exclude: '',
214 name: '',
215 enable: false
216 }
217 }
218
219
220 let excludePath;
221
222 if (_.isArray(cssModule.exclude)) {
223 excludePath = cssModule.exclude.map((path) => resolveApp(path))
224 // fs.existsSync(resolveApp(cssModule.exclude)) ? resolveApp(cssModule.exclude) : ''
225 } else {
226 excludePath = resolveApp(cssModule.exclude)
227 }
228
229 const getLocalIdent = cssModule.getLocalIdent ? {getLocalIdent: cssModule.getLocalIdent} : {};
230
231
232 return {
233 exclude: excludePath,
234 enable: true,
235 config: Object.assign(
236 getLocalIdent,
237 {
238 localIdentName: cssModule.localIdentName || cssModule.name || '[folder]__[local]-[hash:base64:5]',
239 }
240 )
241 }
242}
243
244
245
246
247function log(msg) {
248 console.log(msg)
249}
250
251module.exports = {
252 webpackConfig: webpackConfig,
253
254 filenames: getFilenames(webpackConfig),
255 cdnPath: getCdnPath(webpackConfig),
256 alias: getAliasConfig(webpackConfig),
257 cssModule: getCssModuleConfig(webpackConfig),
258 externals: webpackConfig.externals
259};
\No newline at end of file