1 |
|
2 | const externalNodeModules = require('webpack-node-externals');
|
3 | const webpack = require('webpack');
|
4 | const path = require('path');
|
5 |
|
6 | const webpackOptions = {
|
7 | context: process.cwd(),
|
8 |
|
9 | entry: {},
|
10 |
|
11 | output: {
|
12 | path: path.resolve(process.cwd(), 'dist'),
|
13 | filename: '[name].js',
|
14 | publicPath: '',
|
15 | libraryTarget: 'umd',
|
16 | library: ''
|
17 | },
|
18 |
|
19 | module: {
|
20 | rules: [],
|
21 |
|
22 | },
|
23 |
|
24 | resolve: {
|
25 | extensions: ['.js', '.jsx', '.css', '.scss', '.less'],
|
26 | modules: [],
|
27 | alias: {}
|
28 | },
|
29 |
|
30 | plugins: [],
|
31 |
|
32 | resolveLoader: {
|
33 | modules: []
|
34 | },
|
35 |
|
36 | target: 'web',
|
37 |
|
38 | externals: [],
|
39 |
|
40 | devServer: {
|
41 |
|
42 |
|
43 | }
|
44 | };
|
45 |
|
46 | module.exports = {
|
47 | webpackOptions,
|
48 |
|
49 | |
50 |
|
51 |
|
52 |
|
53 | setContext: function setContext(contextPath) {
|
54 | this.webpackOptions.context = contextPath;
|
55 | },
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 | addBundleEntry: function addBundleEntry(bundleName, bundleEntry) {
|
63 | var entryType = bundleEntry.constructor.name.toLowerCase();
|
64 | switch (entryType) {
|
65 | case 'string':
|
66 | bundleEntry = [bundleEntry];
|
67 | break;
|
68 | case 'array':
|
69 | break;
|
70 | default:
|
71 | cx.warning('The type of bundleEntry is not supported yet.');
|
72 | }
|
73 | this.webpackOptions.entry[bundleName] = bundleEntry;
|
74 | },
|
75 |
|
76 | |
77 |
|
78 |
|
79 |
|
80 | setExportedName: function(libraryName) {
|
81 | this.webpackOptions.output.library = libraryName;
|
82 | },
|
83 |
|
84 | setChuckFileName: function(chunkname) {
|
85 | this.webpackOptions.output.filename = chunkname;
|
86 | },
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
92 | setBuildPath: function setBuildPath(buildPath) {
|
93 | this.webpackOptions.output.path = buildPath;
|
94 | },
|
95 | |
96 |
|
97 |
|
98 |
|
99 | setPublicPath: function setPublicPath(publicPath) {
|
100 | this.webpackOptions.output.publicPath = publicPath || '';
|
101 | },
|
102 |
|
103 | |
104 |
|
105 |
|
106 |
|
107 | addModuleLoader: function(loader) {
|
108 | this.webpackOptions.module.rules.push(loader);
|
109 | },
|
110 |
|
111 | |
112 |
|
113 |
|
114 |
|
115 | addModuleNoParse: function(matchRegex) {
|
116 | if (!this.webpackOptions.module.noParse) this.webpackOptions.module.noParse = [];
|
117 | this.webpackOptions.module.noParse.push(matchRegex);
|
118 | },
|
119 |
|
120 | addParseInclude(abspath) {
|
121 | this.webpackOptions.module.rules[0].include.push(abspath);
|
122 | },
|
123 |
|
124 | |
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | addModuleAlias: function(alias, source, noParse, isVendor) {
|
132 | this.webpackOptions.resolve.alias[alias] = source;
|
133 |
|
134 | if (noParse) {
|
135 | this.addModuleNoParse(source);
|
136 | }
|
137 |
|
138 | if (isVendor) {
|
139 | this.addVendor(source);
|
140 | }
|
141 | },
|
142 |
|
143 | |
144 |
|
145 |
|
146 |
|
147 | addModuleSearchPath: function(path) {
|
148 | this.webpackOptions.resolve.modules.push(path);
|
149 | },
|
150 |
|
151 | |
152 |
|
153 |
|
154 |
|
155 | addPlugin: function(plugin) {
|
156 | this.webpackOptions.plugins.push(plugin);
|
157 | },
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 | addVendor: function(vendor) {
|
164 | if (this.__vendorAlias) {
|
165 | if (vendor) {
|
166 | var vendorChunk = this.webpackOptions.entry[this.__vendorAlias];
|
167 | var valueType = vendor.constructor.name.toLowerCase();
|
168 |
|
169 | if (valueType == 'string') {
|
170 | vendorChunk.push(vendor);
|
171 | }
|
172 | if (valueType == 'array') {
|
173 | vendorChunk = vendorChunk.concat(vendor);
|
174 | }
|
175 | }
|
176 | } else {
|
177 | console.warn('Please enable vendors configuration.');
|
178 | }
|
179 | },
|
180 |
|
181 | |
182 |
|
183 |
|
184 |
|
185 | addLoaderSearchPath: function(path) {
|
186 | this.webpackOptions.resolveLoader.modules.push(path);
|
187 | },
|
188 | |
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | |
199 |
|
200 |
|
201 |
|
202 |
|
203 | addExternal: function(external) {
|
204 | this.webpackOptions.externals.push(external);
|
205 | },
|
206 |
|
207 | addExternalRequire: function(moduleName) {
|
208 | this.webpackOptions.externals.push({
|
209 | [moduleName]: 'commonjs ' + moduleName
|
210 | })
|
211 | },
|
212 |
|
213 | addRequireIgnore: function(requestRegExp, contextRegExp) {
|
214 | this.webpackOptions.addPlugin(new webpack.IgnorePlugin(requestRegExp, contextRegExp));
|
215 | },
|
216 |
|
217 | addExternalNodeModules: function(options) {
|
218 | this.webpackOptions.externals.push(externalNodeModules(Object.assign({
|
219 | whitelist: [],
|
220 | importType: 'commonjs',
|
221 | modulesDir: 'node_modules',
|
222 | modulesFromFile: false
|
223 | }, options)))
|
224 | },
|
225 |
|
226 | addExternalGlobal: function(objName) {
|
227 | this.webpackOptions.externals.push({
|
228 | [objName]: true
|
229 | })
|
230 | },
|
231 |
|
232 | |
233 |
|
234 |
|
235 |
|
236 |
|
237 | setDevServer: function(host, port, contentBase) {
|
238 | this.webpackOptions.devServer.host = host || 'localhost';
|
239 | this.webpackOptions.devServer.port = port || 8080;
|
240 | this.webpackOptions.devServer.contentBase = contentBase || this.webpackOptions.output.path;
|
241 | },
|
242 |
|
243 | postcss: function() {
|
244 | return [
|
245 | require('precss'),
|
246 | require('autoprefixer'),
|
247 | ]
|
248 | }
|
249 | }; |
\ | No newline at end of file |