forked from paypal/paypal-checkout-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
234 lines (206 loc) · 6.67 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* @flow */
/* eslint import/unambiguous: 0 */
/* eslint import/no-nodejs-modules: 0 */
import fs from 'fs';
import qs from 'querystring';
import path from 'path';
import webpack from 'webpack';
import CircularDependencyPlugin from 'circular-dependency-plugin';
import UglifyJSPlugin from 'uglifyjs-webpack-plugin';
import { WebpackPromiseShimPlugin } from 'webpack-promise-shim-plugin';
let babelConfig = JSON.parse(fs.readFileSync('./.babelrc').toString()); // eslint-disable-line no-sync
babelConfig.babelrc = false;
babelConfig.cacheDirectory = true;
babelConfig.presets[0][1].modules = false;
const FILE_NAME = 'checkout';
function getNextVersion() : string {
let version = require('./package.json').version;
version = version.split('.');
version[2] = (parseInt(version[2], 10) + 1).toString();
version = version.join('.');
return version;
}
function getNextMajorVersion() : string {
return getNextVersion().split('.')[0];
}
function getNextMinorVersion() : string {
return getNextVersion();
}
function getVersionVars() : { [string] : string } {
return {
__MAJOR_VERSION__: JSON.stringify(getNextMajorVersion()),
__MINOR_VERSION__: JSON.stringify(getNextMinorVersion())
};
}
type WebPackConfig = {
src : string,
filename : string,
modulename? : string,
target? : string,
major? : boolean,
minify? : boolean,
vars? : { [string] : (string | number | boolean) },
chunkname? : string
};
function getWebpackConfig({ src, filename, modulename, target = 'window', major = true, minify = false, vars = {}, chunkname } : WebPackConfig) : Object {
vars = {
__TEST__: false,
__IE_POPUP_SUPPORT__: true,
__POPUP_SUPPORT__: true,
__LEGACY_SUPPORT__: true,
__FILE_NAME__: JSON.stringify(filename),
__DEFAULT_LOG_LEVEL__: JSON.stringify('warn'),
__MAJOR__: major,
__MINIFIED__: minify,
__CHILD_WINDOW_ENFORCE_LOG_LEVEL__: true,
__SEND_POPUP_LOGS_TO_OPENER__: false,
__ALLOW_POSTMESSAGE_POPUP__: true,
...getVersionVars(),
...vars
};
const PREPROCESSOR_OPTS = {
'ifdef-triple-slash': 'false',
...vars
};
let config = {
entry: path.resolve(src),
module: {
rules: [
{
test: /\.js$/,
loader: `ifdef-loader?${ qs.stringify(PREPROCESSOR_OPTS) }`
},
{
test: /sinon\.js$/,
loader: 'imports?define=>false,require=>false'
},
{
test: /\.jsx?$/,
exclude: /(sinon|chai)/,
loader: 'babel-loader',
options: babelConfig
},
{
test: /\.(html?|css|json|svg)$/,
loader: 'raw-loader'
}
]
},
output: {
path: path.resolve('./dist'),
filename,
libraryTarget: target,
umdNamedDefine: true,
library: modulename,
chunkFilename: chunkname,
pathinfo: false,
jsonpFunction: '__paypal_checkout_jsonp__',
publicPath: 'https://www.paypalobjects.com/api/'
},
bail: true,
resolve: {
extensions: [ '.js', '.jsx' ]
},
plugins: [
new webpack.SourceMapDevToolPlugin({
filename: '[file].map'
}),
new WebpackPromiseShimPlugin({
module: 'zalgo-promise/src',
key: 'ZalgoPromise'
}),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: (chunkname ? 2 : 1)
}),
new webpack.DefinePlugin(vars),
new webpack.NamedModulesPlugin(),
new UglifyJSPlugin({
test: /\.js$/,
beautify: !minify,
minimize: minify,
compress: {
warnings: false,
sequences: minify
},
mangle: minify,
sourceMap: true
}),
new CircularDependencyPlugin({
exclude: /node_modules/,
failOnError: true
})
// new webpack.optimize.ModuleConcatenationPlugin()
]
};
return config;
}
let nextMajorVersion = getNextMajorVersion();
let nextMinorVersion = getNextMinorVersion();
export let BASE = getWebpackConfig({
src: './src/load.js',
filename: `${ FILE_NAME }.js`,
major: true
});
export let MAJOR = getWebpackConfig({
src: './src/load.js',
filename: `${ FILE_NAME }.v${ nextMajorVersion }.js`,
major: true,
vars: {
__IE_POPUP_SUPPORT__: false,
__LEGACY_SUPPORT__: true
}
});
export let MINOR = getWebpackConfig({
src: './src/load.js',
filename: `${ FILE_NAME }.${ nextMinorVersion }.js`
});
export let MAJOR_MIN = getWebpackConfig({
src: './src/load.js',
filename: `${ FILE_NAME }.min.js`,
minify: true,
major: true
});
export let MINOR_MIN = getWebpackConfig({
src: './src/load.js',
filename: `${ FILE_NAME }.${ nextMinorVersion }.min.js`,
minify: true
});
export let LIB = getWebpackConfig({
src: './src/index.js',
filename: `${ FILE_NAME }.lib.js`,
target: `umd`,
modulename: `paypal`,
major: true
});
export let BUTTON_RENDER = getWebpackConfig({
src: './src/button/template/componentTemplate.jsx',
filename: `${ FILE_NAME }.button.render.js`,
target: `commonjs`
});
export let CHILD_LOADER = getWebpackConfig({
src: './src/loader/index.js',
filename: `checkout.child.loader.js`
});
export let CHILD_LOADER_MIN = getWebpackConfig({
src: './src/loader/index.js',
filename: `checkout.child.loader.min.js`,
minify: true
});
export let BUTTON = getWebpackConfig({
src: './src/load.js',
filename: `${ FILE_NAME }.button.v${ nextMajorVersion }.js`,
chunkname: `${ FILE_NAME }.button.v${ nextMajorVersion }.chunk.js`,
major: true,
vars: {
__LEGACY_SUPPORT__: false
}
});
// eslint-disable-next-line import/no-anonymous-default-export
export default [
BASE,
MAJOR, MINOR,
MAJOR_MIN, MINOR_MIN,
LIB,
BUTTON_RENDER,
CHILD_LOADER, CHILD_LOADER_MIN
];