UNPKG

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