-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathserver.ts
74 lines (67 loc) · 2.01 KB
/
server.ts
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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { isAbsolute } from 'path';
import { Configuration, ContextReplacementPlugin } from 'webpack';
import { WebpackConfigOptions } from '../build-options';
import { getSourceMapDevTool } from './utils';
/**
* Returns a partial specific to creating a bundle for node
* @param wco Options which are include the build options and app config
*/
export function getServerConfig(wco: WebpackConfigOptions): Configuration {
const {
sourceMap,
bundleDependencies,
externalDependencies = [],
} = wco.buildOptions;
const extraPlugins = [];
if (sourceMap) {
const { scripts, styles, hidden } = sourceMap;
if (scripts || styles) {
extraPlugins.push(getSourceMapDevTool(scripts, styles, hidden));
}
}
const config: Configuration = {
resolve: {
mainFields: [...(wco.supportES2015 ? ['es2015'] : []), 'main', 'module'],
},
target: 'node',
output: {
libraryTarget: 'commonjs',
},
plugins: [
// Fixes Critical dependency: the request of a dependency is an expression
new ContextReplacementPlugin(/@?hapi(\\|\/)/),
new ContextReplacementPlugin(/express(\\|\/)/),
...extraPlugins,
],
node: false,
};
if (bundleDependencies) {
config.externals = [...externalDependencies];
} else {
config.externals = [
...externalDependencies,
(context: string, request: string, callback: (error?: null, result?: string) => void) => {
// Absolute & Relative paths are not externals
if (request.startsWith('./') || isAbsolute(request)) {
callback();
return;
}
try {
require.resolve(request);
callback(null, request);
} catch {
// Node couldn't find it, so it must be user-aliased
callback();
}
},
];
}
return config;
}