UNPKG

4.07 kBJavaScriptView Raw
1/*eslint-disable*/
2var CleanWebpackPlugin = require('clean-webpack-plugin');
3var HTMLWebpackPlugin = require('html-webpack-plugin');
4const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
5var OfflinePlugin = require('offline-plugin');
6var webpack = require('webpack');
7
8module.exports = function(cx, umdConf) {
9 return {
10 enableEntryHTML: false,
11
12 installEntryHTML: function(entry, options) {
13 var mergeOptions = Object.assign({}, {
14 filename: (entry || 'index') + '.html',
15 template: cx.getCwdPath('./etc/umd.template.html'),
16 minify: {
17 preserveLineBreaks: false,
18 collapseWhitespace: true,
19 collapseInlineTagWhitespace: true,
20 minifyCSS: true,
21 minifyJS: true,
22 quoteCharacter: '"',
23 removeComments: true,
24 },
25 chunks: [(entry || 'index')],
26 hash: false
27 }, options);
28 if (umdConf.__vendorAlias) mergeOptions.chunks.unshift(umdConf.__vendorAlias);
29 umdConf.addPlugin(new HTMLWebpackPlugin(mergeOptions));
30 },
31
32 enableClean: function(absPaths) {
33 var mergeAbsPaths = absPaths || [cx.__builddir];
34 umdConf.addPlugin(new CleanWebpackPlugin(mergeAbsPaths, {
35 root: cx.__cwd
36 }))
37 },
38
39 enableHistoryfallback: true,
40
41 enableASAR: false,
42
43 enableCSSModule: false,
44
45 enableEntryHot: function(entryName) {
46 var webpackHotClient = require.resolve('webpack-hot-middleware/client') + '?reload=true';
47 var entryBundle = umdConf.webpackOptions.entry[entryName || 'main'];
48 if (entryBundle) {
49 entryBundle.unshift(webpackHotClient);
50 }
51 },
52
53 enableBabelPolyfill: function(entryName) {
54 var entryBundle = umdConf.webpackOptions.entry[entryName || 'main'];
55 if (entryBundle)
56 entryBundle.unshift(require.resolve('babel-polyfill'));
57 },
58
59 enableUglifyJs: function(options) {
60 var mergeOptions = Object.assign({
61 parallel: true,
62 sourceMap: false,
63 uglifyOptions: {
64 output: {
65 comments: false,
66 beautify: false
67 },
68 warnings: false
69 }
70 }, options);
71
72 umdConf.addPlugin(new UglifyJSPlugin(mergeOptions));
73 },
74
75 enableVendors: function(options) {
76 var mergeOptions = Object.assign({}, {
77 name: "vendor",
78 minChunks: Infinity,
79 }, options)
80 umdConf.webpackOptions.entry[mergeOptions.name] = [];
81 umdConf.__vendorAlias = mergeOptions.name;
82 umdConf.addPlugin(new webpack.optimize.CommonsChunkPlugin(mergeOptions));
83 },
84
85 enableCommons: function(options) {
86 var mergeOptions = Object.assign({}, {
87 name: "commons"
88 }, options)
89 umdConf.webpackOptions.entry[mergeOptions.name] = [];
90 umdConf.addPlugin(new webpack.optimize.CommonsChunkPlugin(mergeOptions));
91 },
92
93 enableOffline: false,
94
95 installOffline: function() {
96 umdConf.addPlugin(new OfflinePlugin());
97 //install offapp
98 for (var key in umdConf.pkg.wbp.entries) {
99 var entryModules = umdConf.webpackOptions.entry[key];
100 if (entryModules) {
101 entryModules.push(require.resolve('../lib/offapp.js'));
102 }
103 }
104 },
105
106 enableChuckHash: false,
107
108 installChuckHash: function() {
109 const chunkname = umdConf.webpackOptions.output.filename;
110 if (chunkname.indexOf('[name]') !== -1) {
111 umdConf.webpackOptions.output.filename = chunkname.replace('[name]', '[name]' + (umdConf.devMode ? '_[hash:7]' : '_[chunkhash:7]'));
112 }
113 },
114
115 enableNode: function(options, target) {
116 umdConf.webpackOptions.target = target || 'node';
117 umdConf.addExternalNodeModules(options);
118 },
119
120 enableDevtool: function(devtoolType = 'eval') {
121 umdConf.webpackOptions.devtool = devtoolType;
122 },
123
124 enableHits: function(hitsobj) {
125 umdConf.webpackOptions.performance = Object.assign({
126 hints: 'warning',
127 assetFilter: function(assetFilename) {
128 return assetFilename.endsWith('.js');
129 }
130 }, hitsobj);
131 }
132 };
133}
\No newline at end of file