@angular-devkit/build-angular
Version:
Angular Webpack Build Facade
59 lines (58 loc) • 2.46 kB
JavaScript
/**
* @license
* Copyright Google LLC 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.dev/license
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FindTestsPlugin = void 0;
const private_1 = require("@angular/build/private");
const mini_css_extract_plugin_1 = require("mini-css-extract-plugin");
const node_assert_1 = __importDefault(require("node:assert"));
/**
* The name of the plugin provided to Webpack when tapping Webpack compiler hooks.
*/
const PLUGIN_NAME = 'angular-find-tests-plugin';
class FindTestsPlugin {
options;
compilation;
constructor(options) {
this.options = options;
}
apply(compiler) {
const { include = ['**/*.spec.ts'], exclude = [], projectSourceRoot, workspaceRoot, } = this.options;
const webpackOptions = compiler.options;
const entry = typeof webpackOptions.entry === 'function' ? webpackOptions.entry() : webpackOptions.entry;
let originalImport;
// Add tests files are part of the entry-point.
webpackOptions.entry = async () => {
const specFiles = await (0, private_1.findTests)(include, exclude, workspaceRoot, projectSourceRoot);
const entrypoints = await entry;
const entrypoint = entrypoints['main'];
if (!entrypoint.import) {
throw new Error(`Cannot find 'main' entrypoint.`);
}
if (specFiles.length) {
originalImport ??= entrypoint.import;
entrypoint.import = [...originalImport, ...specFiles];
}
else {
(0, node_assert_1.default)(this.compilation, 'Compilation cannot be undefined.');
this.compilation
.getLogger(mini_css_extract_plugin_1.pluginName)
.error(`Specified patterns: "${include.join(', ')}" did not match any spec files.`);
}
return entrypoints;
};
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
this.compilation = compilation;
compilation.contextDependencies.add(projectSourceRoot);
});
}
}
exports.FindTestsPlugin = FindTestsPlugin;
;