-
Notifications
You must be signed in to change notification settings - Fork 63
/
webpack.config.js
171 lines (166 loc) · 3.84 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
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const TerserWebpackPlugin = require('terser-webpack-plugin');
const externalModules = [
'async_hooks',
'sqlite3',
'mysql2',
'pg',
'mariasql',
'mssql',
'mysql',
'strong-oracle',
'oracle',
'oracledb',
'pg-query-stream',
'tedious',
'better-sqlite3',
/mssql\/.*/,
'osx-temperature-sensor',
'@balena/systemd',
];
let requiredModules = [];
let maybeOptionalModules = [];
const lookForOptionalDeps = function (sourceDir) {
// We iterate over the node modules and mark all optional dependencies as external
const dirs = fs.readdirSync(sourceDir);
for (const dir of dirs) {
let packageJson = {};
const internalNodeModules = path.join(sourceDir, dir, 'node_modules');
if (fs.existsSync(internalNodeModules)) {
lookForOptionalDeps(internalNodeModules);
}
try {
packageJson = JSON.parse(
fs.readFileSync(path.join(sourceDir, dir, '/package.json'), 'utf8'),
);
} catch {
continue;
}
if (packageJson.optionalDependencies != null) {
maybeOptionalModules = maybeOptionalModules.concat(
_.keys(packageJson.optionalDependencies),
);
}
if (packageJson.dependencies != null) {
requiredModules = requiredModules.concat(
_.keys(packageJson.dependencies),
);
}
}
};
lookForOptionalDeps('./node_modules');
externalModules.push(
new RegExp(
'^(' +
_.reject(maybeOptionalModules, requiredModules)
.map(_.escapeRegExp)
.join('|') +
')(/.*)?$',
),
);
console.log('Using the following dependencies as external:', externalModules);
module.exports = function (env) {
return {
mode: env == null || !env.noOptimize ? 'production' : 'development',
entry: './src/app.ts',
output: {
filename: 'app.js',
path: path.resolve(__dirname, 'dist'),
},
resolve: {
extensions: ['.js', '.ts', '.json'],
alias: {
// Use the es2018 build instead of the default es2015 build
'pinejs-client-core': 'pinejs-client-core/es2018',
},
},
target: 'node',
node: {
__dirname: false,
},
optimization: {
minimize: true,
minimizer: [
new TerserWebpackPlugin({
terserOptions: {
mangle: false,
keep_classnames: true,
},
}),
],
},
module: {
rules: [
{
include: [
new RegExp(
_.escapeRegExp(
// this is the path as of [email protected]
path.join('knex', 'lib', 'migrations', 'common'),
),
),
],
use: require.resolve('./build-utils/hardcode-migrations'),
},
{
test: new RegExp(
_.escapeRegExp(path.join('JSONStream', 'index.js')) + '$',
),
use: require.resolve('./build-utils/fix-jsonstream'),
},
{
test: /\.ts$|\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
configFile: 'tsconfig.release.json',
},
},
],
},
{
test: /\.node$/,
loader: 'node-loader',
},
],
},
externals: (_context, request, callback) => {
for (const m of externalModules) {
if (
(typeof m === 'string' && m === request) ||
(m instanceof RegExp && m.test(request))
) {
return callback(null, 'commonjs ' + request);
} else if (typeof m !== 'string' && !(m instanceof RegExp)) {
throw new Error('Invalid entry in external modules: ' + m);
}
}
return callback();
},
plugins: [
new ForkTsCheckerWebpackPlugin({
async: false,
}),
new CopyWebpackPlugin({
patterns: [
{
from: './build/migrations',
to: 'migrations',
},
],
}),
new webpack.ContextReplacementPlugin(
/\.\/migrations/,
path.resolve(__dirname, 'build/migrations'),
),
],
};
};