UNPKG

6.88 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 env: ['test', 'prod'],
172 args() {
173 return {
174 filename: this.webpackInfo.cssName,
175 chunkFilename: this.webpackInfo.cssChunkName
176 };
177 }
178};
179
180
181exports.modulereplacement = {
182 enable() {
183 return this.isUsePlugin('extract', false);
184 },
185 type: 'server',
186 name: webpack.NormalModuleReplacementPlugin,
187 args: [/\.(css|less|scss|sass|styl|stylus)$/, require.resolve('node-noop')]
188};
189
190exports.ignore = {
191 enable() {
192 return this.isUsePlugin('extract', false);
193 },
194 type: 'server',
195 name: webpack.IgnorePlugin,
196 args: /\.(css|less|scss|sass|styl|stylus)$/
197};
198
199exports.html = {
200 enable: true,
201 type: 'client',
202 name: 'html-webpack-plugin',
203 args: {
204 inject: true
205 }
206};
207
208exports.serviceworker = {
209 enable: false,
210 type: 'client',
211 name: 'service-worker-precache-webpack-plugin',
212 args() {
213 return {
214 env: this.env,
215 hash: this.test || this.prod,
216 minify: this.prod,
217 localPublicPath: this.config.publicPath
218 };
219 }
220};
221
222exports.tschecker = {
223 enable: false,
224 type: 'client',
225 name: 'fork-ts-checker-webpack-plugin',
226 args() {
227 const filepath = path.resolve(this.baseDir, this.egg ? 'app/web/tsconfig.json' : 'tsconfig.json');
228 const arg = {
229 silent: true,
230 memoryLimit: 512,
231 checkSyntacticErrors: true
232 };
233 if (fs.existsSync(filepath)) {
234 arg.tsconfig = filepath;
235 }
236 return arg;
237 }
238};
239
240exports.clean = {
241 enable: true,
242 env: ['test', 'prod'],
243 type: 'client',
244 name: 'clean-webpack-plugin',
245 args() {
246 const dist = path.relative(this.baseDir, this.buildPath);
247 const dirs = [dist];
248 const options = { root: this.baseDir };
249 return [dirs, options];
250 }
251};
252
253exports.cssmini = {
254 enable: true,
255 env: 'prod',
256 type: 'client',
257 name: 'optimize-css-assets-webpack-plugin'
258};
259
260exports.case = {
261 enable: true,
262 name: 'case-sensitive-paths-webpack-plugin',
263 args: {}
264};
265
266exports.copy = {
267 enable: false,
268 type: 'client',
269 name: 'copy-webpack-plugin'
270};
271
272exports.write = {
273 enable: true,
274 env: ['dev'],
275 type: 'client',
276 name: 'write-file-webpack-plugin',
277 args: {
278 test: /\.(tpl|html)$/
279 }
280};
281
282exports.filter = {
283 enable: true,
284 name: 'webpack-filter-warnings-plugin',
285 args: {
286 exclude: /\[mini-css-extract-plugin]\nConflicting order/
287 }
288};
289
290exports.vconsole = {
291 enable: false,
292 type: 'client',
293 name: 'vconsole-webpack-plugin',
294 args() {
295 const enable = ['dev', 'test'].indexOf(this.env) > -1;
296 return { enable };
297 }
298};
\No newline at end of file