-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
111 lines (95 loc) · 3.25 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
const path = require('path');
const webpack = require('webpack');
const NodemonPlugin = require('nodemon-webpack-plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const packageJson = require('./package.json');
const isDev = !process.env.NODE_ENV || process.env.NODE_ENV === 'development';
const useNodemon = process.env.USE_NODEMON === 'true';
const WEBPACK_DEV_SERVER_PORT = 3003;
const resolveExtensions = ['*', '.js', '.ts'];
const stats = {
preset: 'normal',
errorDetails: true,
warnings: true,
modules: false,
};
console.log({ 'process.env.NODE_ENV': process.env.NODE_ENV, useNodemon });
module.exports = {
mode: isDev ? 'development' : 'production',
devtool: isDev && 'eval-cheap-module-source-map',
cache: {
type: 'filesystem', // https://webpack.js.org/configuration/cache/#cachetype
},
entry: path.resolve(__dirname, './src/index.ts'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'spresso.sdk.tracking.web.js',
library: {
name: 'SpressoSdk',
type: 'umd', // enable browser or node import
export: 'default',
},
globalObject: 'this',
},
module: {
rules: [
{
// Use Babel to handle browser compatibility
test: /\.(ts|js)?$/,
exclude: /node_modules/,
use: [
{
// Use Babel to handle browser compatibility
loader: 'babel-loader',
options: {
envName: isDev ? 'development' : 'production', // transpile code in prod mode for staging/production builds
cacheDirectory: true,
cacheCompression: !isDev, // https://javascript.plainenglish.io/how-to-improve-webpack-performance-7637db26fa5f
configFile: path.resolve(__dirname, 'babel.config.js'),
},
},
'ts-loader',
],
},
],
},
resolve: {
extensions: resolveExtensions,
plugins: [
new TsconfigPathsPlugin({
configFile: `tsconfig.json`,
extensions: resolveExtensions, // https://github.com/dividab/tsconfig-paths-webpack-plugin#extensions-string-defaultts-tsx
}),
],
},
plugins: [
// new webpack.ProgressPlugin({
// profile: true,
// }),
new webpack.DefinePlugin({
__SDK_VERSION__: JSON.stringify(packageJson.version),
}),
useNodemon &&
new NodemonPlugin({
// only runs on `dev:prod-mode`
exec: 'serve',
script: 'dist',
args: [`--listen=${WEBPACK_DEV_SERVER_PORT}`],
watch: ['./dist'],
ext: 'js',
env: {
NODE_ENV: 'production',
},
}),
].filter(Boolean),
stats,
devServer: isDev
? {
hot: true,
port: WEBPACK_DEV_SERVER_PORT,
devMiddleware: {
stats,
},
}
: {},
};