-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack-server-prod.config.js
80 lines (73 loc) · 2.54 KB
/
webpack-server-prod.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
// https://github.com/nrwl/nx/issues/1518
const CopyPlugin = require('copy-webpack-plugin');
const GeneratePackageJsonPlugin = require('generate-package-json-webpack-plugin');
const path = require('path');
const packageJson = require('./package.json');
/**
* Extend the default Webpack configuration from nx / ng.
*/
module.exports = (config, context) => {
// Extract output path from context
const {
options: { outputPath }
} = context;
// Install additional plugins
config.plugins = config.plugins || [];
config.plugins.push(...extractRelevantNodeModules(outputPath));
return config;
};
/**
* This repository only contains one single package.json file that lists the dependencies
* of all its frontend and backend applications. When a frontend application is built,
* its external dependencies (aka Node modules) are bundled in the resulting artifact.
* However, it is not the case for a backend application (for various valid reasons).
* Installing all the production dependencies would dramatically increase the size of the
* artifact. Instead, we need to extract the dependencies which are actually used by the
* backend application. We have implemented this behavior by complementing the default
* Webpack configuration with additional plugins.
*
* @param {String} outputPath The path to the bundle being built
* @returns {Array} An array of Webpack plugins
*/
function extractRelevantNodeModules(outputPath) {
return [copyPackageLockFile(outputPath), generatePackageJson()];
}
/**
* Copy the NPM package lock file to the bundle to make sure that the right dependencies are
* installed when running `npm install`.
*
* @param {String} outputPath The path to the bundle being built
* @returns {*} A Webpack plugin
*/
function copyPackageLockFile(outputPath) {
return new CopyPlugin([
{
from: 'package-lock.json',
to: path.join(outputPath, 'package-lock.json')
}
]);
}
/**
* Generate a package.json file that contains only the dependencies which are actually
* used in the code.
*
* @returns {*} A Webpack plugin
*/
function generatePackageJson() {
const implicitDeps = [
'@nestjs/platform-express',
'reflect-metadata',
'@nestjs/platform-socket.io',
'mqtt',
'rxjs'
];
const dependencies = implicitDeps.reduce((acc, dep) => {
acc[dep] = packageJson.dependencies[dep];
return acc;
}, {});
const basePackageJson = {
dependencies
};
const pathToPackageJson = path.join(__dirname, 'package.json');
return new GeneratePackageJsonPlugin(basePackageJson, pathToPackageJson);
}