-
Notifications
You must be signed in to change notification settings - Fork 320
/
webpack.common.config.js
219 lines (196 loc) · 6.4 KB
/
webpack.common.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
/* eslint-disable camelcase */
const { resolve, join } = require('path');
const { readFileSync } = require('fs');
const TerserPlugin = require('terser-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const terserOptions = require('./scripts/buildtools/terser/config');
var SRC = resolve(__dirname, 'src');
var TARGET_JS = resolve(__dirname, 'target/js/');
var LOCAL_PACKAGES = resolve(__dirname, 'packages/');
var COURAGE_DIST = `${LOCAL_PACKAGES}/@okta/courage-dist/esm`;
var QTIP2_DIST = `${LOCAL_PACKAGES}/@okta/qtip2/dist`;
// Return a function so that all consumers get a new copy of the config
module.exports = function({
entry,
outputFilename,
mode = 'development',
engine = '',
outputLibrary = 'OktaSignIn',
outputLibraryTarget = 'umd',
cdn = true,
useBuiltIns = false,
}) {
/* eslint complexity: [2, 11] */
// normalize entry so it is always an array
entry = Array.isArray(entry) ? entry : [entry];
const babelOptions = {
configFile: false, // do not load from babel.config.js
babelrc: false, // do not load from .babelrc
sourceType: 'unambiguous',
presets: [
'@babel/preset-typescript',
],
plugins: [
'./packages/@okta/babel-plugin-handlebars-inline-precompile',
'@babel/plugin-transform-modules-commonjs',
],
assumptions: {
setPublicClassFields: true
}
};
if (mode === 'production') {
// preset-env must run before preset-typescript https://github.com/babel/babel/issues/12066
babelOptions.presets.unshift([
'@babel/preset-env',
useBuiltIns ? {
useBuiltIns: 'entry',
corejs: '3.9',
} : {}
]);
} else if (process.env.IE11_COMPAT_MODE === 'true') {
babelOptions.presets.unshift([
'@babel/preset-env',
{
targets: {
ie: '11'
},
}
]);
} else {
// In local development, we would prefer not to include any babel transforms as they make debugging more difficult
// However, there is an issue with testcafe which requires us to include the optional chaining transform
// https://github.com/DevExpress/testcafe-hammerhead/issues/2714
babelOptions.plugins.unshift('@babel/plugin-proposal-optional-chaining');
// acorn-hammerhead (testcafe) is currently unable to parse class fields, and will return the script unmodified
// this will cause URLs to load outside the proxy and cause browser disconnect error
babelOptions.plugins.unshift('@babel/plugin-proposal-class-properties');
}
if (cdn) {
babelOptions.plugins.push('add-module-exports');
}
const webpackConfig = {
entry,
mode,
devtool: 'source-map',
output: {
path: TARGET_JS,
filename: outputFilename,
libraryTarget: outputLibraryTarget
},
resolve: {
// conditionNames: ['import', 'browser'],
extensions: ['.js', '.ts'],
modules: [SRC, 'packages', 'node_modules'],
alias: {
// General remapping
'nls': '@okta/i18n/src/json',
// '@okta/courage': `${LOCAL_PACKAGES}/@okta/courage-dist`,
'@okta/okta-i18n-bundles': 'util/Bundles',
// Vendor files from courage that
'handlebars/runtime': `${COURAGE_DIST}/lib/handlebars/dist/cjs/handlebars.runtime`,
'handlebars$': `${COURAGE_DIST}/lib/handlebars/dist/cjs/handlebars.runtime`,
'@okta/qtip': '@okta/qtip2/dist/jquery.qtip.js',
'widgets/jquery.qtip': `${QTIP2_DIST}/jquery.qtip.css`,
'@okta/duo': `${LOCAL_PACKAGES}/vendor/duo_web_sdk/index.js`,
'@okta/typingdna': `${LOCAL_PACKAGES}/vendor/TypingDnaRecorder-JavaScript/typingdna`,
}
},
module: {
rules: [
// Babel
{
test: /\.[jt]s$/,
exclude: function(filePath) {
const filePathContains = (f) => filePath.indexOf(f) > 0;
const npmRequiresTransform = [
'/node_modules/parse-ms',
'/node_modules/@sindresorhus/to-milliseconds'
].some(filePathContains);
const shallBeExcluded = [
'/node_modules/',
'packages/@okta/qtip2',
'okta-auth-js'
].some(filePathContains);
return shallBeExcluded && !npmRequiresTransform;
},
loader: 'babel-loader',
options: babelOptions
},
// load external source maps
{
test: /\.js$/,
use: ['source-map-loader'],
enforce: 'pre'
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false,
}
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
map: true,
plugins: [
['autoprefixer', { remove: false }],
...(mode === 'production' ? [
['cssnano', { save: true }]
] : [])
]
}
}
},
{
loader: 'sass-loader',
options: {
sassOptions: {
includePaths: ['target/sass'],
outputStyle: 'expanded',
}
}
}
],
},
]
},
// Webpack attempts to add a polyfill for process
// and setImmediate, because q uses process to see
// if it's in a Node.js environment
node: false,
optimization: {}
};
if (outputLibrary) {
webpackConfig.output.library = outputLibrary;
}
if (mode !== 'development') {
const optimization = {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions,
extractComments: {
// `banner` config option is intended for a message pointing to file containing license info
// we use it to place single Okta license banner
banner: readFileSync(join(__dirname, './src/widget/copyright.txt'), 'utf8')
}
})
],
};
webpackConfig.optimization = optimization;
}
switch (engine) {
case 'classic':
webpackConfig.resolve.alias['@okta/okta-auth-js'] = '@okta/okta-auth-js/authn';
break;
case 'oie':
webpackConfig.resolve.alias['@okta/okta-auth-js'] = '@okta/okta-auth-js/idx';
break;
}
return webpackConfig;
};