2020-03-30 13:08:42 +00:00
|
|
|
const webpack = require('webpack');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const _ = require('lodash');
|
2017-11-29 21:32:57 +00:00
|
|
|
const CopyWebpackPlugin = require('copy-webpack-plugin');
|
2020-03-30 13:08:42 +00:00
|
|
|
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
|
|
|
|
const TerserWebpackPlugin = require('terser-webpack-plugin');
|
2017-07-01 04:07:02 +00:00
|
|
|
|
2024-02-29 22:00:39 +00:00
|
|
|
const externalModules = [
|
2020-03-13 13:23:30 +00:00
|
|
|
'async_hooks',
|
2017-07-01 04:07:02 +00:00
|
|
|
'sqlite3',
|
|
|
|
'mysql2',
|
|
|
|
'pg',
|
|
|
|
'mariasql',
|
|
|
|
'mssql',
|
|
|
|
'mysql',
|
|
|
|
'strong-oracle',
|
|
|
|
'oracle',
|
|
|
|
'oracledb',
|
2018-12-03 16:57:24 +00:00
|
|
|
'pg-query-stream',
|
|
|
|
'tedious',
|
2023-08-03 22:55:46 +00:00
|
|
|
'better-sqlite3',
|
2019-06-13 09:54:39 +00:00
|
|
|
/mssql\/.*/,
|
2022-09-20 13:32:37 +00:00
|
|
|
'osx-temperature-sensor',
|
2023-08-10 19:18:12 +00:00
|
|
|
'@balena/systemd',
|
2019-06-13 09:54:39 +00:00
|
|
|
];
|
2017-07-01 04:07:02 +00:00
|
|
|
|
2020-06-16 12:04:12 +00:00
|
|
|
let requiredModules = [];
|
|
|
|
let maybeOptionalModules = [];
|
|
|
|
const lookForOptionalDeps = function (sourceDir) {
|
2017-07-01 04:07:02 +00:00
|
|
|
// We iterate over the node modules and mark all optional dependencies as external
|
2024-02-29 22:00:39 +00:00
|
|
|
const dirs = fs.readdirSync(sourceDir);
|
|
|
|
for (const dir of dirs) {
|
2017-07-01 04:07:02 +00:00
|
|
|
let packageJson = {};
|
2024-02-29 22:00:39 +00:00
|
|
|
const internalNodeModules = path.join(sourceDir, dir, 'node_modules');
|
2017-07-01 04:07:02 +00:00
|
|
|
if (fs.existsSync(internalNodeModules)) {
|
|
|
|
lookForOptionalDeps(internalNodeModules);
|
|
|
|
}
|
|
|
|
try {
|
2019-06-13 09:54:39 +00:00
|
|
|
packageJson = JSON.parse(
|
2020-06-16 12:04:12 +00:00
|
|
|
fs.readFileSync(path.join(sourceDir, dir, '/package.json'), 'utf8'),
|
2019-06-13 09:54:39 +00:00
|
|
|
);
|
2022-09-19 15:08:16 +00:00
|
|
|
} catch {
|
2017-07-01 04:07:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-06-13 09:54:39 +00:00
|
|
|
if (packageJson.optionalDependencies != null) {
|
|
|
|
maybeOptionalModules = maybeOptionalModules.concat(
|
2020-04-09 10:41:48 +00:00
|
|
|
_.keys(packageJson.optionalDependencies),
|
2019-06-13 09:54:39 +00:00
|
|
|
);
|
2017-07-01 04:07:02 +00:00
|
|
|
}
|
2019-06-13 09:54:39 +00:00
|
|
|
if (packageJson.dependencies != null) {
|
|
|
|
requiredModules = requiredModules.concat(
|
2020-04-09 10:41:48 +00:00
|
|
|
_.keys(packageJson.dependencies),
|
2019-06-13 09:54:39 +00:00
|
|
|
);
|
2017-07-01 04:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-13 09:54:39 +00:00
|
|
|
};
|
2017-07-01 04:07:02 +00:00
|
|
|
|
2019-06-13 09:54:39 +00:00
|
|
|
lookForOptionalDeps('./node_modules');
|
|
|
|
externalModules.push(
|
|
|
|
new RegExp(
|
|
|
|
'^(' +
|
|
|
|
_.reject(maybeOptionalModules, requiredModules)
|
|
|
|
.map(_.escapeRegExp)
|
|
|
|
.join('|') +
|
2020-04-09 10:41:48 +00:00
|
|
|
')(/.*)?$',
|
|
|
|
),
|
2019-06-13 09:54:39 +00:00
|
|
|
);
|
2017-07-01 04:07:02 +00:00
|
|
|
|
|
|
|
console.log('Using the following dependencies as external:', externalModules);
|
|
|
|
|
2020-06-16 12:04:12 +00:00
|
|
|
module.exports = function (env) {
|
2017-11-09 00:06:28 +00:00
|
|
|
return {
|
2018-11-05 18:33:58 +00:00
|
|
|
mode: env == null || !env.noOptimize ? 'production' : 'development',
|
2019-06-21 11:21:52 +00:00
|
|
|
entry: './src/app.ts',
|
2017-11-09 00:06:28 +00:00
|
|
|
output: {
|
|
|
|
filename: 'app.js',
|
2019-06-13 09:54:39 +00:00
|
|
|
path: path.resolve(__dirname, 'dist'),
|
2017-11-09 00:06:28 +00:00
|
|
|
},
|
|
|
|
resolve: {
|
2020-05-13 19:30:30 +00:00
|
|
|
extensions: ['.js', '.ts', '.json'],
|
2020-06-16 11:16:48 +00:00
|
|
|
alias: {
|
|
|
|
// Use the es2018 build instead of the default es2015 build
|
|
|
|
'pinejs-client-core': 'pinejs-client-core/es2018',
|
|
|
|
},
|
2017-11-09 00:06:28 +00:00
|
|
|
},
|
|
|
|
target: 'node',
|
2017-11-29 21:32:57 +00:00
|
|
|
node: {
|
2019-06-13 09:54:39 +00:00
|
|
|
__dirname: false,
|
2017-11-29 21:32:57 +00:00
|
|
|
},
|
2020-03-30 13:08:42 +00:00
|
|
|
optimization: {
|
|
|
|
minimize: true,
|
|
|
|
minimizer: [
|
|
|
|
new TerserWebpackPlugin({
|
|
|
|
terserOptions: {
|
|
|
|
mangle: false,
|
2020-04-09 10:41:48 +00:00
|
|
|
keep_classnames: true,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2020-03-30 13:08:42 +00:00
|
|
|
},
|
2017-11-09 00:06:28 +00:00
|
|
|
module: {
|
|
|
|
rules: [
|
2017-11-29 21:32:57 +00:00
|
|
|
{
|
2020-04-27 17:33:41 +00:00
|
|
|
include: [
|
|
|
|
new RegExp(
|
2022-09-27 17:43:27 +00:00
|
|
|
_.escapeRegExp(
|
2023-08-03 22:55:46 +00:00
|
|
|
// this is the path as of knex@2.5.1
|
|
|
|
path.join('knex', 'lib', 'migrations', 'common'),
|
2022-09-27 17:43:27 +00:00
|
|
|
),
|
2020-04-27 17:33:41 +00:00
|
|
|
),
|
|
|
|
],
|
2020-04-27 17:36:28 +00:00
|
|
|
use: require.resolve('./build-utils/hardcode-migrations'),
|
2017-11-29 21:32:57 +00:00
|
|
|
},
|
2017-11-09 00:06:28 +00:00
|
|
|
{
|
2019-06-13 09:54:39 +00:00
|
|
|
test: new RegExp(
|
2020-04-09 10:41:48 +00:00
|
|
|
_.escapeRegExp(path.join('JSONStream', 'index.js')) + '$',
|
2019-06-13 09:54:39 +00:00
|
|
|
),
|
2020-04-27 17:36:28 +00:00
|
|
|
use: require.resolve('./build-utils/fix-jsonstream'),
|
2017-11-09 00:06:28 +00:00
|
|
|
},
|
2018-04-27 13:27:42 +00:00
|
|
|
{
|
2020-03-23 18:22:48 +00:00
|
|
|
test: /\.ts$|\.js$/,
|
2020-03-24 12:44:03 +00:00
|
|
|
exclude: /node_modules/,
|
2018-04-27 13:27:42 +00:00
|
|
|
use: [
|
|
|
|
{
|
|
|
|
loader: 'ts-loader',
|
2018-12-20 15:34:30 +00:00
|
|
|
options: {
|
|
|
|
transpileOnly: true,
|
2019-06-13 09:54:39 +00:00
|
|
|
configFile: 'tsconfig.release.json',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2017-11-09 00:06:28 +00:00
|
|
|
},
|
2020-06-16 12:04:12 +00:00
|
|
|
externals: (_context, request, callback) => {
|
2024-02-29 22:00:39 +00:00
|
|
|
for (const m of externalModules) {
|
2019-06-13 09:54:39 +00:00
|
|
|
if (
|
|
|
|
(typeof m === 'string' && m === request) ||
|
|
|
|
(m instanceof RegExp && m.test(request))
|
|
|
|
) {
|
2017-11-09 00:06:28 +00:00
|
|
|
return callback(null, 'commonjs ' + request);
|
2020-03-23 18:42:27 +00:00
|
|
|
} else if (typeof m !== 'string' && !(m instanceof RegExp)) {
|
2017-11-09 00:06:28 +00:00
|
|
|
throw new Error('Invalid entry in external modules: ' + m);
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 09:54:39 +00:00
|
|
|
return callback();
|
2017-11-09 00:06:28 +00:00
|
|
|
},
|
2018-12-20 15:34:30 +00:00
|
|
|
plugins: [
|
2018-12-20 17:28:10 +00:00
|
|
|
new ForkTsCheckerWebpackPlugin({
|
2019-06-13 09:54:39 +00:00
|
|
|
async: false,
|
2018-12-20 17:28:10 +00:00
|
|
|
}),
|
2020-06-16 12:04:12 +00:00
|
|
|
new CopyWebpackPlugin({
|
|
|
|
patterns: [
|
|
|
|
{
|
|
|
|
from: './build/migrations',
|
|
|
|
to: 'migrations',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
2018-12-20 15:34:30 +00:00
|
|
|
new webpack.ContextReplacementPlugin(
|
|
|
|
/\.\/migrations/,
|
2020-04-09 10:41:48 +00:00
|
|
|
path.resolve(__dirname, 'build/migrations'),
|
2018-12-20 15:34:30 +00:00
|
|
|
),
|
2019-06-13 09:54:39 +00:00
|
|
|
],
|
2017-11-09 00:06:28 +00:00
|
|
|
};
|
2019-06-13 09:54:39 +00:00
|
|
|
};
|