Paul Gschwendtner fd3cfbb678 build: create bazel marco to test for circular dependencies (#34774)
Creates a Bazel macro that can be used to test packages for
circular dependencies. We face one limitation with Bazel:

 * Built packages use module imports, and not relative source file
 paths. This means we need custom resolution.

Fortunately, tools like `madge` support custom resolution.

Also removes the outdated `check-cycles` gulp task that
didn't catch circular dependencies. It seems like the test
became broken when we switched the packages-dist output to Bazel. It
breaks because the Bazel output doesn't use relative paths, but uses
the module imports. This will be handled in the new Bazel macro/rule.

PR Close #34774
2020-01-23 11:36:40 -08:00

39 lines
1.5 KiB
JavaScript

/**
* @license
* Copyright Google Inc. 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.io/license
*/
/**
* Custom resolution plugin for Webpack's `resolve-enhanced` package that is used by
* Madge for resolving imports. The plugin extends the resolution by leveraging the
* runfile resolution and module mappings handled in the module info aspect.
*/
class BazelRunfileResolutionPlugin {
apply(resolver) {
resolver.plugin('module', (request, callback) => {
try {
// Resolve the module through the `require.resolve` method which has been patched
// in the Bazel NodeJS loader to respect runfiles and module mappings. This allows
// Madge to handle module mappings specified in `ts_library` and `ng_module` targets.
const resolvedPath = require.resolve(request.request);
// Update the request to refer to the runfile resolved file path.
resolver.doResolve('resolve', {...request, request: resolvedPath}, null, callback, true);
return;
} catch {
}
// If the file could not be resolved through Bazel's runfile resolution, proceed
// with the default module resolvers.
callback();
});
}
}
// Configures a plugin which ensures that Madge can properly resolve specified
// dependencies through their configured module names.
module.exports = {
resolve: {plugins: [new BazelRunfileResolutionPlugin()]}
};