UNPKG

4.3 kBJavaScriptView Raw
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 */
9var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12}) : (function(o, m, k, k2) {
13 if (k2 === undefined) k2 = k;
14 o[k2] = m[k];
15}));
16var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17 Object.defineProperty(o, "default", { enumerable: true, value: v });
18}) : function(o, v) {
19 o["default"] = v;
20});
21var __importStar = (this && this.__importStar) || function (mod) {
22 if (mod && mod.__esModule) return mod;
23 var result = {};
24 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25 __setModuleDefault(result, mod);
26 return result;
27};
28var __importDefault = (this && this.__importDefault) || function (mod) {
29 return (mod && mod.__esModule) ? mod : { "default": mod };
30};
31Object.defineProperty(exports, "__esModule", { value: true });
32exports.runWebpack = void 0;
33const architect_1 = require("@angular-devkit/architect");
34const path_1 = require("path");
35const rxjs_1 = require("rxjs");
36const operators_1 = require("rxjs/operators");
37const webpack_1 = __importDefault(require("webpack"));
38const utils_1 = require("../utils");
39function runWebpack(config, context, options = {}) {
40 const { logging: log = (stats, config) => context.logger.info(stats.toString(config.stats)), shouldProvideStats = true, } = options;
41 const createWebpack = (c) => {
42 if (options.webpackFactory) {
43 const result = options.webpackFactory(c);
44 if (rxjs_1.isObservable(result)) {
45 return result;
46 }
47 else {
48 return rxjs_1.of(result);
49 }
50 }
51 else {
52 return rxjs_1.of(webpack_1.default(c));
53 }
54 };
55 return createWebpack({ ...config, watch: false }).pipe(operators_1.switchMap((webpackCompiler) => new rxjs_1.Observable((obs) => {
56 const callback = (err, stats) => {
57 if (err) {
58 return obs.error(err);
59 }
60 if (!stats) {
61 return;
62 }
63 // Log stats.
64 log(stats, config);
65 const statsOptions = typeof config.stats === 'boolean' ? undefined : config.stats;
66 const result = {
67 success: !stats.hasErrors(),
68 webpackStats: shouldProvideStats ? stats.toJson(statsOptions) : undefined,
69 emittedFiles: utils_1.getEmittedFiles(stats.compilation),
70 outputPath: stats.compilation.outputOptions.path,
71 };
72 if (config.watch) {
73 obs.next(result);
74 }
75 else {
76 webpackCompiler.close(() => {
77 obs.next(result);
78 obs.complete();
79 });
80 }
81 };
82 try {
83 if (config.watch) {
84 const watchOptions = config.watchOptions || {};
85 const watching = webpackCompiler.watch(watchOptions, callback);
86 // Teardown logic. Close the watcher when unsubscribed from.
87 return () => {
88 watching.close(() => { });
89 webpackCompiler.close(() => { });
90 };
91 }
92 else {
93 webpackCompiler.run(callback);
94 }
95 }
96 catch (err) {
97 if (err) {
98 context.logger.error(`\nAn error occurred during the build:\n${(err && err.stack) || err}`);
99 }
100 throw err;
101 }
102 })));
103}
104exports.runWebpack = runWebpack;
105exports.default = architect_1.createBuilder((options, context) => {
106 const configPath = path_1.resolve(context.workspaceRoot, options.webpackConfig);
107 return rxjs_1.from(Promise.resolve().then(() => __importStar(require(configPath)))).pipe(operators_1.switchMap(({ default: config }) => runWebpack(config, context)));
108});