-
Notifications
You must be signed in to change notification settings - Fork 222
/
webpack.config.js
155 lines (145 loc) · 4.04 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const path = require('path');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
const fs = require('fs');
const version = require('./package.json').version;
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
// Create a list of Class names to preserve when minifying
const namespace = fs.readFileSync('./src/namespace/cloudinary-jquery-file-upload.js').toString();
const reserved = namespace.match(/(\w+)(?= from)/g);
/**
* Determine the build mode.
* @param {object} env - webpack environment
* @param {boolean} [env.production] - when true, set mode to production
* @param {boolean} [env.development] - when true, set mode to development
* @param {object} argv - webpack options
* @param {string} [argv.mode] - the build mode
*/
function getMode(env, argv) {
// When running from parallel-webpack, grab the cli parameters
argv = Object.keys(argv).length ? argv : require('minimist')(process.argv.slice(2));
var isProd = (argv.mode || env.mode) === 'production' || env === 'prod' || env.prod;
return isProd ? 'production' : 'development';
}
/**
* This function is used by webpack to resolve individual lodash modules
* @param context
* @param request
* @param callback
*/
function resolveLodash(context, request, callback) {
if (/^lodash\//.test(request)) {
callback(null, {
commonjs: request,
commonjs2: request,
amd: request,
root: ['_', request.split('/')[1]]
});
} else {
callback();
}
}
/**
* Generate a webpack configuration
* @param {string} name the name of the package
* @param {string} mode either development or production
* @return {object} webpack configuration
*/
function baseConfig(name, mode) {
const config = {
name: `${name}-${mode}`,
mode,
output: {
library: 'cloudinary',
libraryTarget: 'umd',
globalObject: "this",
pathinfo: false
},
optimization: {
concatenateModules: true,
moduleIds: 'named',
usedExports: true,
minimizer: [new TerserPlugin({
terserOptions: {
mangle: {
keep_classnames: true,
reserved: reserved,
ie8: true
}
},
})]
},
resolve: {
extensions: ['.js']
},
externals: [
{
jquery: 'jQuery'
}
],
node: {
Buffer: false,
process: false
},
devtool: "source-map",
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {loader: 'babel-loader'}
}
]
},
plugins: [
new webpack.BannerPlugin({
banner: `/**
* cloudinary-${name}.js
* Cloudinary's JavaScript library - Version ${version}
* Copyright Cloudinary
* see https://github.com/cloudinary/cloudinary_js
*
*/`, // the banner as string or function, it will be wrapped in a comment
raw: true, // if true, banner will not be wrapped in a comment
entryOnly: true, // if true, the banner will only be added to the entry chunks
})
]
};
let filename = `cloudinary-${name}`;
if(mode === 'production') { filename += '.min';}
const util = name.startsWith('jquery') ? 'jquery' : 'lodash';
const utilPath = path.resolve(__dirname, `src/util/${util}`);
config.output.filename = `./${filename}.js`;
config.entry = `./src/namespace/cloudinary-${name}.js`;
config.resolve.alias = {
"../util$": utilPath,
"./util$": utilPath
};
// Add reference to each lodash function as a separate module.
if (name === 'core') {
config.externals.push(resolveLodash);
}
config.plugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
reportFilename: `./${filename}-visualizer.html`,
openAnalyzer: false
})
);
return config;
}
const names = [
"core",
"jquery",
"jquery-file-upload",
"core-shrinkwrap"
];
const modes = [
'production',
'development'
];
module.exports = names.reduce(
(configs, name)=> configs.concat(
modes.map(mode=> baseConfig(name, mode))
), []
);