1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.getServerConfig = void 0;
|
---|
11 | const path_1 = require("path");
|
---|
12 | const webpack_1 = require("webpack");
|
---|
13 | const helpers_1 = require("../utils/helpers");
|
---|
14 | /**
|
---|
15 | * Returns a partial Webpack configuration specific to creating a bundle for node
|
---|
16 | * @param wco Options which include the build options and app config
|
---|
17 | */
|
---|
18 | function getServerConfig(wco) {
|
---|
19 | const { sourceMap, bundleDependencies, externalDependencies = [] } = wco.buildOptions;
|
---|
20 | const extraPlugins = [];
|
---|
21 | const { scripts, styles, hidden } = sourceMap;
|
---|
22 | if (scripts || styles) {
|
---|
23 | extraPlugins.push(helpers_1.getSourceMapDevTool(scripts, styles, hidden));
|
---|
24 | }
|
---|
25 | const externals = [...externalDependencies];
|
---|
26 | if (!bundleDependencies) {
|
---|
27 | externals.push(({ context, request }, callback) => externalizePackages(context !== null && context !== void 0 ? context : wco.projectRoot, request, callback));
|
---|
28 | }
|
---|
29 | const config = {
|
---|
30 | resolve: {
|
---|
31 | mainFields: ['es2015', 'main', 'module'],
|
---|
32 | },
|
---|
33 | output: {
|
---|
34 | libraryTarget: 'commonjs',
|
---|
35 | },
|
---|
36 | module: {
|
---|
37 | parser: {
|
---|
38 | javascript: {
|
---|
39 | worker: false,
|
---|
40 | url: false,
|
---|
41 | },
|
---|
42 | },
|
---|
43 | },
|
---|
44 | plugins: [
|
---|
45 | // Fixes Critical dependency: the request of a dependency is an expression
|
---|
46 | new webpack_1.ContextReplacementPlugin(/@?hapi(\\|\/)/),
|
---|
47 | new webpack_1.ContextReplacementPlugin(/express(\\|\/)/),
|
---|
48 | ...extraPlugins,
|
---|
49 | ],
|
---|
50 | node: false,
|
---|
51 | externals,
|
---|
52 | };
|
---|
53 | return config;
|
---|
54 | }
|
---|
55 | exports.getServerConfig = getServerConfig;
|
---|
56 | function externalizePackages(context, request, callback) {
|
---|
57 | if (!request) {
|
---|
58 | return;
|
---|
59 | }
|
---|
60 | // Absolute & Relative paths are not externals
|
---|
61 | if (request.startsWith('.') || path_1.isAbsolute(request)) {
|
---|
62 | callback();
|
---|
63 | return;
|
---|
64 | }
|
---|
65 | try {
|
---|
66 | require.resolve(request, { paths: [context] });
|
---|
67 | callback(undefined, request);
|
---|
68 | }
|
---|
69 | catch {
|
---|
70 | // Node couldn't find it, so it must be user-aliased
|
---|
71 | callback();
|
---|
72 | }
|
---|
73 | }
|
---|