UNPKG

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