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 | var __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 | }));
|
---|
16 | var __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 | });
|
---|
21 | var __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 | };
|
---|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
29 | exports.execute = void 0;
|
---|
30 | const architect_1 = require("@angular-devkit/architect");
|
---|
31 | const core_1 = require("@angular-devkit/core");
|
---|
32 | const path_1 = require("path");
|
---|
33 | const rxjs_1 = require("rxjs");
|
---|
34 | const operators_1 = require("rxjs/operators");
|
---|
35 | const schema_1 = require("../browser/schema");
|
---|
36 | const version_1 = require("../utils/version");
|
---|
37 | const webpack_browser_config_1 = require("../utils/webpack-browser-config");
|
---|
38 | const configs_1 = require("../webpack/configs");
|
---|
39 | const single_test_transform_1 = require("../webpack/plugins/single-test-transform");
|
---|
40 | const find_tests_1 = require("./find-tests");
|
---|
41 | async function initialize(options, context, webpackConfigurationTransformer) {
|
---|
42 | const { config } = await webpack_browser_config_1.generateBrowserWebpackConfigFromContext(
|
---|
43 | // only two properties are missing:
|
---|
44 | // * `outputPath` which is fixed for tests
|
---|
45 | // * `budgets` which might be incorrect due to extra dev libs
|
---|
46 | {
|
---|
47 | ...options,
|
---|
48 | outputPath: '',
|
---|
49 | budgets: undefined,
|
---|
50 | optimization: false,
|
---|
51 | buildOptimizer: false,
|
---|
52 | aot: false,
|
---|
53 | vendorChunk: true,
|
---|
54 | namedChunks: true,
|
---|
55 | extractLicenses: false,
|
---|
56 | outputHashing: schema_1.OutputHashing.None,
|
---|
57 | // The webpack tier owns the watch behavior so we want to force it in the config.
|
---|
58 | // When not in watch mode, webpack-dev-middleware will call `compiler.watch` anyway.
|
---|
59 | // https://github.com/webpack/webpack-dev-middleware/blob/698c9ae5e9bb9a013985add6189ff21c1a1ec185/src/index.js#L65
|
---|
60 | // https://github.com/webpack/webpack/blob/cde1b73e12eb8a77eb9ba42e7920c9ec5d29c2c9/lib/Compiler.js#L379-L388
|
---|
61 | watch: true,
|
---|
62 | extractCss: true,
|
---|
63 | }, context, (wco) => [
|
---|
64 | configs_1.getCommonConfig(wco),
|
---|
65 | configs_1.getStylesConfig(wco),
|
---|
66 | configs_1.getTypeScriptConfig(wco),
|
---|
67 | configs_1.getTestConfig(wco),
|
---|
68 | configs_1.getWorkerConfig(wco),
|
---|
69 | ]);
|
---|
70 | const karma = await Promise.resolve().then(() => __importStar(require('karma')));
|
---|
71 | return [
|
---|
72 | karma,
|
---|
73 | webpackConfigurationTransformer ? await webpackConfigurationTransformer(config) : config,
|
---|
74 | ];
|
---|
75 | }
|
---|
76 | /**
|
---|
77 | * @experimental Direct usage of this function is considered experimental.
|
---|
78 | */
|
---|
79 | function execute(options, context, transforms = {}) {
|
---|
80 | // Check Angular version.
|
---|
81 | version_1.assertCompatibleAngularVersion(context.workspaceRoot);
|
---|
82 | let singleRun;
|
---|
83 | if (options.watch !== undefined) {
|
---|
84 | singleRun = !options.watch;
|
---|
85 | }
|
---|
86 | return rxjs_1.from(initialize(options, context, transforms.webpackConfiguration)).pipe(operators_1.switchMap(async ([karma, webpackConfig]) => {
|
---|
87 | var _a;
|
---|
88 | const karmaOptions = {
|
---|
89 | singleRun,
|
---|
90 | };
|
---|
91 | // Convert browsers from a string to an array
|
---|
92 | if (options.browsers) {
|
---|
93 | karmaOptions.browsers = options.browsers.split(',');
|
---|
94 | }
|
---|
95 | if (options.reporters) {
|
---|
96 | // Split along commas to make it more natural, and remove empty strings.
|
---|
97 | const reporters = options.reporters
|
---|
98 | .reduce((acc, curr) => acc.concat(curr.split(',')), [])
|
---|
99 | .filter((x) => !!x);
|
---|
100 | if (reporters.length > 0) {
|
---|
101 | karmaOptions.reporters = reporters;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | // prepend special webpack loader that will transform test.ts
|
---|
105 | if (options.include && options.include.length > 0) {
|
---|
106 | const mainFilePath = core_1.getSystemPath(core_1.join(core_1.normalize(context.workspaceRoot), options.main));
|
---|
107 | const files = find_tests_1.findTests(options.include, path_1.dirname(mainFilePath), context.workspaceRoot);
|
---|
108 | // early exit, no reason to start karma
|
---|
109 | if (!files.length) {
|
---|
110 | throw new Error(`Specified patterns: "${options.include.join(', ')}" did not match any spec files.`);
|
---|
111 | }
|
---|
112 | // Get the rules and ensure the Webpack configuration is setup properly
|
---|
113 | const rules = ((_a = webpackConfig.module) === null || _a === void 0 ? void 0 : _a.rules) || [];
|
---|
114 | if (!webpackConfig.module) {
|
---|
115 | webpackConfig.module = { rules };
|
---|
116 | }
|
---|
117 | else if (!webpackConfig.module.rules) {
|
---|
118 | webpackConfig.module.rules = rules;
|
---|
119 | }
|
---|
120 | rules.unshift({
|
---|
121 | test: mainFilePath,
|
---|
122 | use: {
|
---|
123 | // cannot be a simple path as it differs between environments
|
---|
124 | loader: single_test_transform_1.SingleTestTransformLoader,
|
---|
125 | options: {
|
---|
126 | files,
|
---|
127 | logger: context.logger,
|
---|
128 | },
|
---|
129 | },
|
---|
130 | });
|
---|
131 | }
|
---|
132 | karmaOptions.buildWebpack = {
|
---|
133 | options,
|
---|
134 | webpackConfig,
|
---|
135 | logger: context.logger,
|
---|
136 | };
|
---|
137 | const config = await karma.config.parseConfig(path_1.resolve(context.workspaceRoot, options.karmaConfig), transforms.karmaOptions ? transforms.karmaOptions(karmaOptions) : karmaOptions, { promiseConfig: true, throwErrors: true });
|
---|
138 | return [karma, config];
|
---|
139 | }), operators_1.switchMap(([karma, karmaConfig]) => new rxjs_1.Observable((subscriber) => {
|
---|
140 | var _a, _b, _c;
|
---|
141 | var _d, _e;
|
---|
142 | // Pass onto Karma to emit BuildEvents.
|
---|
143 | (_a = karmaConfig.buildWebpack) !== null && _a !== void 0 ? _a : (karmaConfig.buildWebpack = {});
|
---|
144 | if (typeof karmaConfig.buildWebpack === 'object') {
|
---|
145 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
146 | (_b = (_d = karmaConfig.buildWebpack).failureCb) !== null && _b !== void 0 ? _b : (_d.failureCb = () => subscriber.next({ success: false }));
|
---|
147 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
148 | (_c = (_e = karmaConfig.buildWebpack).successCb) !== null && _c !== void 0 ? _c : (_e.successCb = () => subscriber.next({ success: true }));
|
---|
149 | }
|
---|
150 | // Complete the observable once the Karma server returns.
|
---|
151 | const karmaServer = new karma.Server(karmaConfig, (exitCode) => {
|
---|
152 | subscriber.next({ success: exitCode === 0 });
|
---|
153 | subscriber.complete();
|
---|
154 | });
|
---|
155 | const karmaStart = karmaServer.start();
|
---|
156 | // Cleanup, signal Karma to exit.
|
---|
157 | return () => karmaStart.then(() => karmaServer.stop());
|
---|
158 | })), operators_1.defaultIfEmpty({ success: false }));
|
---|
159 | }
|
---|
160 | exports.execute = execute;
|
---|
161 | exports.default = architect_1.createBuilder(execute);
|
---|