forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.base.ts
346 lines (318 loc) · 11.4 KB
/
webpack.config.base.ts
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import chalk from 'chalk'
import { CleanWebpackPlugin } from 'clean-webpack-plugin'
import webpack from 'webpack'
// @ts-ignore
import LiveReloadPlugin from 'webpack-livereload-plugin'
// @ts-ignore
import sassGlobImporter = require('node-sass-glob-importer')
import HtmlWebpackPlugin = require('html-webpack-plugin')
import MiniCSSExtractWebpackPlugin = require('mini-css-extract-plugin')
const env = process.env.NODE_ENV === 'production' ? 'production' : 'development'
const args = process.argv.slice(2)
const liveReloadEnabled = process.env.ENABLE_LIVE_RELOAD === '1'
const watchModeEnabled = args.includes('--watch') || args.includes('-w')
// opt in to livereload with ENABLE_LIVE_RELOAD set to '1'
// eslint-disable-next-line no-console
if (liveReloadEnabled && watchModeEnabled) console.log(chalk.gray(`\nLive Reloading is enabled. Unset ${chalk.bold('ENABLE_LIVE_RELOAD')} to disable`))
process.env.NODE_ENV = env
// @ts-ignore
const evalDevToolPlugin = new webpack.EvalDevToolModulePlugin({
moduleFilenameTemplate: 'cypress://[namespace]/[resource-path]',
fallbackModuleFilenameTemplate: 'cypress://[namespace]/[resourcePath]?[contenthash]',
})
evalDevToolPlugin.evalDevToolPlugin = true
const optimization = {
usedExports: true,
providedExports: true,
sideEffects: true,
removeAvailableModules: true,
mergeDuplicateChunks: true,
flagIncludedChunks: true,
removeEmptyChunks: true,
}
const stats = {
errors: true,
warnings: true,
all: false,
builtAt: true,
colors: true,
modules: true,
excludeModules: /(main|test-entry).scss/,
timings: true,
}
const ignoreWarnings = [{
module: /node_modules\/mocha\/lib\/mocha.js/,
}]
function makeSassLoaders ({ modules }: { modules: boolean }) {
const exclude = [/node_modules/]
if (!modules) exclude.push(/\.modules?\.s[ac]ss$/i)
return {
test: modules ? /\.modules?\.s[ac]ss$/i : /\.s[ac]ss$/i,
exclude,
enforce: 'pre',
use: [
{
loader: require.resolve('css-modules-typescript-loader'),
options: {
modules: true,
mode: process.env.CI ? 'verify' : 'emit',
},
},
{
loader: require.resolve('css-loader'),
options: {
// sourceMap: true,
// esModule: false,
modules,
},
}, // translates CSS into CommonJS
{
loader: require.resolve('postcss-loader'),
options: {
postcssOptions: {
plugins: [
require('autoprefixer')({ overrideBrowserslist: ['last 2 versions'], cascade: false }),
],
},
},
},
{
loader: require.resolve('resolve-url-loader'),
},
{
loader: require.resolve('sass-loader'),
options: {
implementation: require('sass'),
sourceMap: true,
sassOptions: {
importer: sassGlobImporter(),
},
},
}, // compiles Sass to CSS, using Node Sass by default
],
}
}
// the chrome version should be synced with
// npm/webpack-batteries-included-preprocessor/index.js and
// packages/server/lib/browsers/chrome.ts
const babelPresetEnvConfig = [require.resolve('@babel/preset-env'), { targets: { 'chrome': '64' } }]
const babelPresetTypeScriptConfig = [require.resolve('@babel/preset-typescript'), { allowNamespaces: true }]
export const getCommonConfig = () => {
const commonConfig = {
mode: 'none',
ignoreWarnings,
resolve: {
extensions: ['.ts', '.js', '.jsx', '.tsx', '.scss', '.json'],
// see https://gist.github.com/ef4/d2cf5672a93cf241fd47c020b9b3066a for polyfill migration details
fallback: {
buffer: require.resolve('buffer/'),
child_process: false,
fs: false,
module: false,
net: false,
os: require.resolve('os-browserify/browser'),
path: require.resolve('path-browserify'),
process: require.resolve('process/browser.js'),
stream: require.resolve('stream-browserify'),
tls: false,
url: require.resolve('url/'),
},
},
stats,
optimization,
module: {
rules: [
{
test: /\.(ts|js|jsx|tsx)$/,
exclude: /node_modules/,
use: {
loader: require.resolve('babel-loader'),
options: {
plugins: [
// "istanbul",
[require.resolve('@babel/plugin-proposal-decorators'), { legacy: true }],
[require.resolve('@babel/plugin-transform-class-properties'), { loose: true }],
[require.resolve('@babel/plugin-transform-private-methods'), { loose: true }],
[require.resolve('@babel/plugin-transform-private-property-in-object'), { loose: true }],
],
presets: [
babelPresetEnvConfig,
require.resolve('@babel/preset-react'),
babelPresetTypeScriptConfig,
],
babelrc: false,
},
},
},
{
test: /\.s?css$/,
exclude: /node_modules/,
use: [
{ loader: MiniCSSExtractWebpackPlugin.loader },
],
},
makeSassLoaders({ modules: false }),
makeSassLoaders({ modules: true }),
{
test: /\.(eot|ttf|woff|woff2)$/,
type: 'asset/resource',
generator: {
// @see https://webpack.js.org/guides/asset-modules/#custom-output-filename
filename: 'fonts/[name][ext]',
},
},
{
test: /\.(png|gif)$/,
type: 'asset/resource',
generator: {
// @see https://webpack.js.org/guides/asset-modules/#custom-output-filename
filename: 'img/[name][ext]',
},
},
// leveraged for SVGs as components for react components inside the reporter.
// The loader is needed for the reporter under component tests, and the runner
// as it bundles the reporter.
{
test: /\.svg$/i,
issuer: /\.[jt]sx?$/,
use: [{
loader: '@svgr/webpack',
options: {
// we leverage classes in our svgs that correspond to scss classes.
// svgo prefixes these classes be default, which we don't want, so we must disable it
// @see https://react-svgr.com/docs/options/#svgo
svgo: false,
},
}],
},
{
test: /\.wasm$/,
type: 'javascript/auto',
use: [
{
loader: require.resolve('arraybuffer-loader'),
},
],
},
],
},
plugins: [
new MiniCSSExtractWebpackPlugin(),
// Enable source maps / eval maps
// 'EvalDevtoolModulePlugin' is used in development
// because it is fast and maps to filenames while showing compiled source
// 'SourceMapDevToolPlugin' is used in production for the same reasons as 'eval', but it
// shows full source and does not cause crossorigin errors like 'eval' (in Chromium < 63)
// files will be mapped like: `cypress://../driver/cy/commands/click.coffee`
// other sourcemap options:
// [new webpack.SourceMapDevToolPlugin({
// moduleFilenameTemplate: 'cypress://[namespace]/[resource-path]',
// fallbackModuleFilenameTemplate: 'cypress://[namespace]/[resourcePath]?[contenthash]'
// })] :
...[
(env === 'production'
? new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production') })
: evalDevToolPlugin
),
],
// Cypress needs access globally to the buffer and process objects.
// These were polyfilled by webpack in v4 and no longer are in v5.
// To work around this, we provide the process and buffer and globals into the webpack bundle.
// @see https://gist.github.com/ef4/d2cf5672a93cf241fd47c020b9b3066a#polyfilling-globals
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
// As of Webpack 5, a new option called resolve.fullySpecified, was added.
// This option means that a full path, in particular to .mjs / .js files
// in ESM packages must have the full path of an import specified.
// Otherwise, compilation fails as this option defaults to true.
// This means we need to adjust our global injections to always
// resolve to include the full file extension if a file resolution is provided.
// @see https://github.com/cypress-io/cypress/issues/27599
// @see https://webpack.js.org/configuration/module/#resolvefullyspecified
process: 'process/browser.js',
}),
...(liveReloadEnabled ? [new LiveReloadPlugin({ appendScriptTag: true, port: 0, hostname: 'localhost', protocol: 'http' })] : []),
],
cache: true,
}
return commonConfig
}
// eslint-disable-next-line @cypress/dev/arrow-body-multiline-braces
export const getSimpleConfig = () => ({
ignoreWarnings,
resolve: {
extensions: ['.js', '.ts', '.json'],
// see https://gist.github.com/ef4/d2cf5672a93cf241fd47c020b9b3066a for polyfill migration details
fallback: {
buffer: require.resolve('buffer/'),
child_process: false,
fs: false,
module: false,
net: false,
os: require.resolve('os-browserify/browser'),
path: require.resolve('path-browserify'),
process: require.resolve('process/browser.js'),
stream: require.resolve('stream-browserify'),
tls: false,
url: require.resolve('url/'),
},
},
stats,
optimization,
cache: true,
plugins: [
new webpack.DefinePlugin({
// The main & cross-origin injection code in @packages/runner needs
// access to the process.env.NODE_DEBUG variable.
// Since the injection lives inside html, it makes most sense to just use the
// DefinePlugin to push the value into the bundle instead of providing the whole process
'process.env.NODE_DEBUG': JSON.stringify('process.env.NODE_DEBUG'),
}),
],
module: {
rules: [
{
test: /\.(js|ts)$/,
exclude: /node_modules/,
use: {
loader: require.resolve('babel-loader'),
options: {
plugins: [
[require.resolve('@babel/plugin-transform-class-properties'), { loose: true }],
[require.resolve('@babel/plugin-transform-private-methods'), { loose: true }],
[require.resolve('@babel/plugin-transform-private-property-in-object'), { loose: true }],
],
presets: [
babelPresetEnvConfig,
babelPresetTypeScriptConfig,
],
babelrc: false,
},
},
},
// FIXME: we don't actually want or need wasm support in the
// cross origin bundle that uses this config, but we need to refactor
// the driver so that it doesn't load the wasm code in
// packages/driver/src/cypress/source_map_utils.js when creating
// the cross origin bundle. for now, this is necessary so the build
// doesn't fail
// https://github.com/cypress-io/cypress/issues/19888
{
test: /\.wasm$/,
type: 'javascript/auto',
use: [
{
loader: require.resolve('arraybuffer-loader'),
},
],
},
],
},
})
export { HtmlWebpackPlugin }
export function getCopyWebpackPlugin () {
return require('copy-webpack-plugin')
}
export function getCleanWebpackPlugin () {
return CleanWebpackPlugin
}