This repository was archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.config.js
More file actions
149 lines (134 loc) · 3.88 KB
/
webpack.config.js
File metadata and controls
149 lines (134 loc) · 3.88 KB
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
const path = require('path');
const webpack = require('webpack');
const slsw = require('serverless-webpack');
const TerserPlugin = require('terser-webpack-plugin');
const nodeExternals = require('webpack-node-externals');
const Visualizer = require('webpack-visualizer-plugin2');
const { StatsWriterPlugin } = require('webpack-stats-plugin');
const aliases = require('./aliases');
const ENABLE_DEBUGGING = false;
const IS_LOCAL = ENABLE_DEBUGGING ? false : slsw.lib.webpack.isLocal;
const servicePath = slsw.lib.serverless && slsw.lib.serverless.config && slsw.lib.serverless.config.servicePath || '';
// configurable settings
const ENABLE_STATS = ENABLE_DEBUGGING || IS_LOCAL; // only want stats locally
const ENABLE_SOURCE_MAPS = !IS_LOCAL; // only want source maps in prod
const ENABLE_CACHING = IS_LOCAL;
function entries() {
const entries = slsw.lib.entries;
for (let key in entries) {
entries[key] = path.join(servicePath, entries[key]);
}
return entries;
}
function babelLoader() {
const plugins = [
'@babel/plugin-transform-runtime',
'@babel/plugin-proposal-class-properties',
];
if (ENABLE_SOURCE_MAPS) {
plugins.push('babel-plugin-source-map-support');
}
return {
loader: 'babel-loader',
options: {
// Enable caching
cacheDirectory: ENABLE_CACHING,
// Disable compressing cache files to speed up caching
cacheCompression: false,
plugins: plugins,
presets: ['@babel/preset-env'],
}
};
}
function loaders() {
const loaders = {
rules: [
// process JS files
{
test: /\.js$/,
exclude: /node_modules/,
use: [babelLoader()]
},
// removed css related loaders b/c we don't use them here _yet_
// ignore image files
{
test: /\.gif|\.svg|\.png|\.jpg|\.jpeg$/,
loader: 'ignore-loader'
}
]
};
return loaders;
}
function plugins() {
const plugins = [];
if (ENABLE_DEBUGGING) {
plugins.push(new StatsWriterPlugin({
filename: "stats.json",
stats: {
all: true
}
}));
plugins.push(new Visualizer());
}
return plugins;
}
function optimization() {
const optimizationConfig = {
concatenateModules: true,
minimize: true,
minimizer: [
new TerserPlugin({
parallel: IS_LOCAL, // https://github.com/webpack-contrib/terser-webpack-plugin#parallel
terserOptions: {
mangle: false, // need to disable for graphql
output: {
comments: false,
},
},
extractComments: false
}),
],
usedExports: true, // enables tree shaking
};
if (IS_LOCAL) {
optimizationConfig.removeEmptyChunks = false;
optimizationConfig.removeAvailableModules = false;
}
return optimizationConfig;
}
module.exports = {
entry: entries(),
target: 'node',
externalsPresets: { node: true }, // in order to ignore built-in modules like path, fs, etc.
context: path.resolve(__dirname),
// Verbose Logging
stats: ENABLE_STATS ? (ENABLE_DEBUGGING ? 'verbose' : 'normal') : 'errors-only',
devtool: ENABLE_SOURCE_MAPS ? 'source-map' : false,
// Exclude aws-sdk b/c it's a built-in on lambda and nodeExternals b/c
// we're in a node runtime and not on the browser
externals: ['aws-sdk', 'bufferutil', 'utf-8-validate', nodeExternals()],
mode: IS_LOCAL ? 'development' : 'production',
performance: {
// Turn off size warnings for entry points
hints: false,
},
resolve: {
// Performance
symlinks: false,
// import aliases
alias: {
...aliases,
graphql$: path.resolve(__dirname, 'node_modules/graphql/index.js'),
},
// We don't package services individually so only look
// inside the project's node_modules
modules: ['node_modules'],
},
module: loaders(),
// PERFORMANCE ONLY FOR DEVELOPMENT
optimization: optimization(),
plugins: plugins(),
node: {
__dirname: false,
}
};