UNPKG

6.72 kBJavaScriptView Raw
1'use strict';
2const path = require('path');
3const fs = require('fs');
4const WebpackTool = require('webpack-tool');
5const webpack = WebpackTool.webpack;
6const chalk = require('chalk');
7const utils = require('../utils/utils');
8
9exports.hot = {
10 enable: true,
11 type: 'client',
12 env: ['dev'],
13 name: webpack.HotModuleReplacementPlugin
14};
15
16exports.npm = {
17 enable: false,
18 name: 'npm-install-webpack-plugin',
19 args: {
20 dev: true
21 }
22};
23
24exports.provide = {
25 enable: true,
26 name: webpack.ProvidePlugin,
27 args: {}
28};
29
30exports.define = {
31 enable: true,
32 name: webpack.DefinePlugin,
33 args() {
34 const NODE_ENV = process.env.NODE_ENV ? process.env.NODE_ENV : (this.prod ? 'production' : 'development');
35 return {
36 'process.env.NODE_ENV': JSON.stringify(NODE_ENV),
37 EASY_ENV: JSON.stringify(this.env),
38 EASY_ENV_IS_DEV: !!this.dev,
39 EASY_ENV_IS_TEST: !!this.test,
40 EASY_ENV_IS_PROD: !!this.prod,
41 EASY_ENV_IS_BROWSER: !(!!this.ssr),
42 EASY_ENV_IS_NODE: !!this.ssr,
43 EASY_ENV_LOCAL_PUBLIC_PATH: JSON.stringify(this.config.publicPath),
44 EASY_ENV_PUBLIC_PATH: JSON.stringify(this.publicPath),
45 EASY_ENV_HOST_URL: JSON.stringify(`${this.host}`)
46 };
47 }
48};
49
50exports.manifest = {
51 enable: true,
52 type: 'client',
53 name: 'webpack-manifest-resource-plugin',
54 args() {
55 const args = {
56 baseDir: this.baseDir,
57 host: this.host,
58 proxy: this.proxy,
59 buildPath: this.buildPath,
60 publicPath: this.publicPath,
61 localPublicPath: this.config.publicPath,
62 assets: false,
63 writeToFileEmit: true
64 };
65 const manifestConfig = this.getConfigPlugin('manifest') || {};
66 const configManifestFileName = manifestConfig.fileName || (manifestConfig.args && manifestConfig.args.fileName);
67 const manifestName = configManifestFileName || (this.egg ? 'config/manifest.json' : 'manifest.json');
68 const manifestDir = this.egg ? this.baseDir : this.buildPath;
69 const filepath = path.join(manifestDir, manifestName);
70 // 兼容旧 manifest 配置
71 const fileName = path.relative(this.config.buildPath, filepath);
72 const dllConfig = utils.getDllConfig(this.config.dll);
73 const dllDir = utils.getDllManifestDir(this.env);
74 const dllChunk = this.getDLLChunk();
75 const commonsChunk = this.getCommonsChunk();
76 // 如果开启了dll 功能, 则读取 dll manifest 配置, 然后与项目 manifest 合并
77 if (dllConfig.length) {
78 return this.merge(args, {
79 dllDir,
80 filepath,
81 fileName,
82 dllConfig,
83 dllChunk,
84 commonsChunk
85 });
86 }
87 return this.merge(args, {
88 filepath,
89 fileName,
90 commonsChunk
91 });
92 }
93};
94
95exports.manifestDll = {
96 enable: false,
97 type: 'client',
98 name: 'webpack-manifest-resource-plugin',
99 args() {
100 const dllConfig = this.config.dll || {};
101 const filepath = this.utils.getDllManifestPath(dllConfig.name, this.env);
102 return {
103 baseDir: this.baseDir,
104 proxy: this.proxy,
105 host: this.host,
106 buildPath: this.buildPath,
107 publicPath: this.publicPath,
108 localPublicPath: this.config.publicPath,
109 assets: false,
110 manifestDll: true,
111 writeToFileEmit: true,
112 dllConfig,
113 filepath
114 };
115 }
116};
117
118exports.progress = {
119 enable: true,
120 name: 'progress-bar-webpack-plugin',
121 args: {
122 width: 100,
123 format: `webpack build [:bar] ${chalk.green.bold(':percent')} (:elapsed seconds)`,
124 clear: false
125 }
126};
127
128exports.imagemini = {
129 enable: true,
130 env: ['prod'],
131 type: 'client',
132 name: 'imagemin-webpack-plugin',
133 entry: 'default'
134};
135
136exports.analyzer = {
137 enable: false,
138 name: 'webpack-bundle-analyzer',
139 entry: 'BundleAnalyzerPlugin',
140 args() {
141 const prefix = this.dll ? 'dll' : this.type;
142 return {
143 analyzerPort: this.dll ? 9997 : this.ssr ? 9998 : 9999,
144 statsFilename: prefix ? prefix + '_analyzer_stats.json' : 'analyzer_stats.json'
145 };
146 }
147};
148
149exports.stats = {
150 enable: false,
151 name: 'stats-webpack-plugin',
152 args() {
153 const prefix = this.dll ? 'dll' : this.type;
154 const args = [{
155 chunkModules: true,
156 exclude: [/node_modules[\\\/]/]
157 }];
158 args.unshift(prefix ? prefix + '_stats.json' : 'stats.json');
159 return args;
160 }
161};
162
163exports.directoryname = {
164 enable: false,
165 name: 'directory-named-webpack-plugin'
166};
167
168exports.extract = {
169 type: 'client',
170 name: 'mini-css-extract-plugin',
171 enable() {
172 return this.config.cssExtract;
173 },
174 args() {
175 return {
176 filename: this.webpackInfo.cssName,
177 chunkFilename: this.webpackInfo.cssChunkName
178 };
179 }
180};
181
182
183exports.modulereplacement = {
184 enable() {
185 return this.config.cssExtract;
186 },
187 type: 'server',
188 name: webpack.NormalModuleReplacementPlugin,
189 args: [/\.(css|less|scss|sass)$/, require.resolve('node-noop')]
190};
191
192exports.ignore = {
193 enable() {
194 return this.config.cssExtract;
195 },
196 type: 'server',
197 name: webpack.IgnorePlugin,
198 args: /\.(css|less|scss|sass)$/
199};
200
201exports.html = {
202 enable: true,
203 type: 'client',
204 name: 'html-webpack-plugin',
205 args: {
206 inject: true
207 }
208};
209
210exports.serviceworker = {
211 enable: false,
212 type: 'client',
213 name: 'service-worker-precache-webpack-plugin',
214 args() {
215 return {
216 env: this.env,
217 hash: this.test || this.prod,
218 minify: this.prod,
219 localPublicPath: this.config.publicPath
220 };
221 }
222};
223
224exports.tschecker = {
225 enable: false,
226 type: 'client',
227 name: 'fork-ts-checker-webpack-plugin',
228 args() {
229 const filepath = path.resolve(this.baseDir, this.egg ? 'app/web/tsconfig.json' : 'tsconfig.json');
230 const arg = {
231 silent: true,
232 memoryLimit: 512,
233 checkSyntacticErrors: true
234 };
235 if (fs.existsSync(filepath)) {
236 arg.tsconfig = filepath;
237 }
238 return arg;
239 }
240};
241
242exports.clean = {
243 enable: true,
244 env: ['test', 'prod'],
245 type: 'client',
246 name: 'clean-webpack-plugin',
247 args() {
248 const dist = path.relative(this.baseDir, this.buildPath);
249 const dirs = [dist];
250 const options = { root: this.baseDir };
251 return [dirs, options];
252 }
253};
254
255exports.cssmini = {
256 enable: true,
257 env: 'prod',
258 type: 'client',
259 name: 'optimize-css-assets-webpack-plugin'
260};
261
262exports.case = {
263 enable: true,
264 name: 'case-sensitive-paths-webpack-plugin',
265 args: {}
266};
267
268exports.copy = {
269 enable: false,
270 type: 'client',
271 name: 'copy-webpack-plugin'
272};
273
274exports.filter = {
275 enable: true,
276 name: 'webpack-filter-warnings-plugin',
277 args: {
278 exclude: /\[mini-css-extract-plugin]\nConflicting order between:/
279 }
280};
281
282exports.vconsole = {
283 enable: false,
284 type: 'client',
285 name: 'vconsole-webpack-plugin',
286 args() {
287 const enable = ['dev', 'test'].indexOf(this.env) > -1;
288 return { enable };
289 }
290};
\No newline at end of file