UNPKG

5.53 kBJavaScriptView Raw
1'use strict';
2const path = require('path');
3const fs = require('fs');
4const chalk = require('chalk');
5const WebpackTool = require('webpack-tool');
6const merge = WebpackTool.merge;
7const WebpackDllBuilder = require('./target/dll');
8const utils = require('../utils/utils');
9const BASE_SOLUTION = 'easywebpack';
10const BASE_FRAMEWORKS = ['vue', 'react', 'weex', 'html', 'js'];
11
12exports.getFramework = baseDir => {
13 const pkgFile = path.join(baseDir, 'package.json');
14 const pkg = require(pkgFile);
15 return BASE_FRAMEWORKS.find(framework => {
16 const key = `${BASE_SOLUTION}-${framework}`;
17 return pkg.dependencies[key] || pkg.devDependencies[key];
18 });
19};
20
21exports.getTarget = config => {
22 if (config.target) {
23 return config.target;
24 }
25 if (config.type === 'client' || config.framework === 'html' || config.template) {
26 return 'web';
27 }
28 if (config.target === null || utils.isEgg(config)) {
29 return undefined;
30 }
31 return 'web';
32};
33
34exports.getPackageConfig = baseDir => {
35 const pkgFile = path.join(baseDir, 'package.json');
36 if (fs.existsSync(pkgFile)) {
37 const pkg = require(pkgFile);
38 return merge({ baseDir }, pkg.webpack);
39 }
40 return { baseDir };
41};
42
43exports.mergeConfig = (a = {}, b = {}) => {
44 if (Array.isArray(a.plugins) && utils.isObject(b.plugins || {})) {
45 b.plugins = Object.keys(b.plugins || {}).map(key => {
46 return {
47 [key]: b.plugins[key]
48 };
49 });
50 }
51 if (Array.isArray(b.plugins) && utils.isObject(a.plugins || {})) {
52 a.plugins = Object.keys(a.plugins || {}).map(key => {
53 return {
54 [key]: a.plugins[key]
55 };
56 });
57 }
58 return merge(a, b);
59};
60
61exports.getBuilderConfig = (config = {}, option = {}) => {
62 const { baseDir = process.cwd() } = option;
63 const pkgConfig = exports.getPackageConfig(baseDir);
64 const baseConfig = exports.mergeConfig(pkgConfig, option);
65 if (utils.isObject(config)) {
66 const defaultWebpackFilepath = path.join(config.baseDir || baseDir, 'webpack.config.js');
67 if (fs.existsSync(defaultWebpackFilepath)) {
68 const fileConfig = require(defaultWebpackFilepath);
69 const normalizeFileConfig = exports.mergeConfig(baseConfig, fileConfig);
70 return exports.mergeConfig(normalizeFileConfig, config);
71 }
72 } else if (utils.isString(config)) {
73 const filepath = path.isAbsolute(config) ? config : path.resolve(baseDir, config);
74 if (fs.existsSync(filepath)) {
75 const customFileConfig = require(filepath);
76 return exports.mergeConfig(baseConfig, customFileConfig);
77 }
78 }
79 return exports.mergeConfig(baseConfig, config);
80};
81
82// create dll manifest
83exports.getDllWebpackConfig = (config, option = {}) => {
84 config.baseDir = config.baseDir || process.cwd();
85 if (config.dll) {
86 const dll = WebpackDllBuilder.getDllConfig(config.dll);
87 const cli = utils.isObject(config.cli) ? config.cli : {};
88 const webpackConfigList = [];
89 const plugins = [];
90 if (cli.size) {
91 plugins.push(cli.size === true ? { analyzer: true } : { stats: true });
92 }
93 // need dll customize uglifyJs config
94 const uglifyJs = utils.getConfigPlugin(config.plugins, 'uglifyJs');
95 if (uglifyJs) {
96 plugins.push({ uglifyJs });
97 }
98 dll.forEach(item => {
99 const tmpConfig = utils.cloneDeep(config);
100 const dllConfig = merge(tmpConfig, { entry: null, dll: null, plugins }, { dll: item }, item.webpackConfig);
101 if (option.onlyView || cli.dll || utils.checkDllUpdate(config, item)) {
102 webpackConfigList.push(new WebpackDllBuilder(dllConfig).create());
103 }
104 });
105 if (webpackConfigList.length) {
106 return webpackConfigList.length === 1 ? webpackConfigList[0] : webpackConfigList;
107 }
108 } else {
109 console.warn(`${chalk.red('[easywebpack] webpack config no dll config, please check webpack.config.js file config')}`);
110 }
111 return null;
112};
113
114exports.getConfig = (config, option) => {
115 const easyConfig = exports.getBuilderConfig(config, option);
116 // 自动检测动态设置 framework, 支持 vue,react,html,weex,js
117 easyConfig.framework = easyConfig.framework || exports.getFramework(easyConfig.baseDir);
118 easyConfig.target = exports.getTarget(easyConfig);
119 return easyConfig;
120};
121
122exports.getWebpackConfig = (config, builders, option = {}) => {
123 let configured = utils.isObject(config) && config.configured;
124 if (configured === undefined) {
125 configured = utils.isObject(config) && utils.isObject(config.cli);
126 }
127 const easyConfig = configured ? config : exports.getConfig(config, option);
128 const type = easyConfig.type;
129 const target = easyConfig.target || option && option.target;
130 let webpackConfigList = [];
131 if (option.onlyDll) {
132 return exports.getDllWebpackConfig(easyConfig, option);
133 }
134 if (option.dll) {
135 const dllWebpackConfig = exports.getDllWebpackConfig(easyConfig);
136 if (dllWebpackConfig) {
137 webpackConfigList = webpackConfigList.concat(dllWebpackConfig);
138 }
139 }
140 builders = Array.isArray(builders) ? builders : [builders];
141 builders.forEach(builder => {
142 const WebpackBuilder = builder;
143 // console.log('easywebpack.getWebpackConfig:',WebpackBuilder.TYPE, WebpackBuilder.TARGET, target, type);
144 if ((type === undefined && (target === undefined || target === null)) || WebpackBuilder.TYPE === type ||
145 WebpackBuilder.TARGET === target || (Array.isArray(type) && type.includes(WebpackBuilder.TYPE))) {
146 webpackConfigList.push(new WebpackBuilder(easyConfig).create());
147 }
148 });
149 return webpackConfigList.length === 1 ? webpackConfigList[0] : webpackConfigList;
150};