UNPKG

6.36 kBJavaScriptView Raw
1/** WEBPACK-CONFIG-UMD */
2const externalNodeModules = require('webpack-node-externals');
3const webpack = require('webpack');
4const path = require('path');
5
6const 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 // noParse: []
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 // host: '0.0.0.0',
42 // port: 8080
43 }
44};
45
46module.exports = {
47 webpackOptions,
48
49 /**
50 * setContext
51 * @param {string} contextPath *MUST be a absolute path*
52 */
53 setContext: function setContext(contextPath) {
54 this.webpackOptions.context = contextPath;
55 },
56
57 /**
58 * addBundleEntry
59 * @param {string} bundleName
60 * @param {string/array} bundleEntry
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 * @method setUMDName
78 * @param {[type]} libraryName [description]
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 * setBuildPath
90 * @param {string} buildPath
91 */
92 setBuildPath: function setBuildPath(buildPath) {
93 this.webpackOptions.output.path = buildPath;
94 },
95 /**
96 * @method setPublicPath
97 * @param {string} publicPath
98 */
99 setPublicPath: function setPublicPath(publicPath) {
100 this.webpackOptions.output.publicPath = publicPath || '';
101 },
102
103 /**
104 * addModuleLoader
105 * @param {object} loader
106 */
107 addModuleLoader: function(loader) {
108 this.webpackOptions.module.rules.push(loader);
109 },
110
111 /**
112 * addModuleNoParse
113 * @param {Regex} matchRegex , to match the resolved request.
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 * addModuleAlias
126 * @param {string} source name
127 * @param {string} alias name
128 * @param {string} noparse
129 * @param {string} isvendor
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 * addModuleSearchPath
145 * @param {string} *MUST* be absolute path
146 */
147 addModuleSearchPath: function(path) {
148 this.webpackOptions.resolve.modules.push(path);
149 },
150
151 /**
152 * addPlugin
153 * @param {[object]} plugin
154 */
155 addPlugin: function(plugin) {
156 this.webpackOptions.plugins.push(plugin);
157 },
158
159 /**
160 * @method addVendor
161 * @param vendor module name or absolute path
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 * addLoaderSearchPath
183 * @param {string} *MUST* be absolute path
184 */
185 addLoaderSearchPath: function(path) {
186 this.webpackOptions.resolveLoader.modules.push(path);
187 },
188 /*
189 "web" Compile for usage in a browser-like environment (default)
190 "webworker" Compile as WebWorker
191 "node" Compile for usage in a node.js-like environment (use require to load chunks)
192 "async-node" Compile for usage in a node.js-like environment (use fs and vm to load chunks async)
193 "node-webkit" Compile for usage in webkit, uses jsonp chunk loading but also supports build in node.js modules plus require(“nw.gui”) (experimental)
194 "electron" Compile for usage in Electron – supports require-ing Electron-specific modules.
195 "electron-renderer" Compile for electron renderer process, provide a target using JsonpTemplatePlugin, FunctionModulePlugin for browser environment and NodeTargetPlugin and ExternalsPlugin for commonjs and electron bulit-in modules. Note: need webpack >= 1.12.15.
196 */
197
198 /**
199 * @method addExternal
200 * @param external // string,object,function,RegExp,array
201 * http://webpack.github.io/docs/configuration.html#externals
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 * setDevServer local modification support
234 * @type {string} host
235 * @type {number} port
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