UNPKG

4.16 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.CustomWebpackBuilder = exports.defaultWebpackConfigPath = void 0;
13const util_1 = require("util");
14const core_1 = require("@angular-devkit/core");
15const lodash_1 = require("lodash");
16const utils_1 = require("./utils");
17const webpack_config_merger_1 = require("./webpack-config-merger");
18exports.defaultWebpackConfigPath = 'webpack.config.js';
19class CustomWebpackBuilder {
20 static buildWebpackConfig(root, config, baseWebpackConfig, buildOptions, targetOptions, logger) {
21 return __awaiter(this, void 0, void 0, function* () {
22 if (!config) {
23 return baseWebpackConfig;
24 }
25 const webpackConfigPath = config.path || exports.defaultWebpackConfigPath;
26 const path = `${(0, core_1.getSystemPath)(root)}/${webpackConfigPath}`;
27 const tsConfig = `${(0, core_1.getSystemPath)(root)}/${buildOptions.tsConfig}`;
28 const configOrFactoryOrPromise = yield resolveCustomWebpackConfig(path, tsConfig, logger);
29 if (typeof configOrFactoryOrPromise === 'function') {
30 // The exported function can return a new configuration synchronously
31 // or return a promise that resolves to a new configuration.
32 const finalWebpackConfig = yield configOrFactoryOrPromise(baseWebpackConfig, buildOptions, targetOptions);
33 logConfigProperties(config, finalWebpackConfig, logger);
34 return finalWebpackConfig;
35 }
36 // The user can also export a promise that resolves to a `Configuration` object.
37 // Suppose the following example:
38 // `module.exports = new Promise(resolve => resolve({ ... }))`
39 // This is valid both for promise and non-promise cases. If users export
40 // a plain object, for instance, `module.exports = { ... }`, then it will
41 // be wrapped into a promise and also `awaited`.
42 const resolvedConfig = yield configOrFactoryOrPromise;
43 const finalWebpackConfig = (0, webpack_config_merger_1.mergeConfigs)(baseWebpackConfig, resolvedConfig, config.mergeRules, config.replaceDuplicatePlugins);
44 logConfigProperties(config, finalWebpackConfig, logger);
45 return finalWebpackConfig;
46 });
47 }
48}
49exports.CustomWebpackBuilder = CustomWebpackBuilder;
50function resolveCustomWebpackConfig(path, tsConfig, logger) {
51 return __awaiter(this, void 0, void 0, function* () {
52 (0, utils_1.tsNodeRegister)(path, tsConfig, logger);
53 return (0, utils_1.loadModule)(path);
54 });
55}
56function logConfigProperties(config, webpackConfig, logger) {
57 var _a;
58 // There's no reason to log the entire configuration object
59 // since Angular's Webpack configuration is huge by default
60 // and doesn't bring any meaningful context by being printed
61 // entirely. Users can provide a list of properties they want to be logged.
62 if ((_a = config.verbose) === null || _a === void 0 ? void 0 : _a.properties) {
63 for (const property of config.verbose.properties) {
64 const value = (0, lodash_1.get)(webpackConfig, property);
65 if (value) {
66 const message = (0, util_1.inspect)(value, /* showHidden */ false, config.verbose.serializationDepth);
67 logger.info(message);
68 }
69 }
70 }
71}
72//# sourceMappingURL=custom-webpack-builder.js.map
\No newline at end of file