angular/packages/compiler-cli/linker/babel/src/babel_plugin.ts
Alan Agius e9fcbb8af1 fix(compiler): remove TypeScript from linker (#61618)
This commit removes the direct dependency on TypeScript within the linker, addressing a performance overhead that was adding between 500ms to 1s to compilation times for applications.

The primary cause of this overhead was the linker's direct reliance on TypeScript's which was caused by importing from barrel files. While convenient, barrel files are detrimental to code splitting and code motion. They force the bundling of all exported modules, even if only a subset is actually used.

By removing the usage of this barrel file and restructuring the imports to be more granular, we can avoid unnecessary TypeScript imports.
 Furthermore, TypeScript has now been changed to an optional peer dependency as using only the linker does not require TypeScript.

PR Close #61618
2025-05-26 08:46:00 +00:00

39 lines
1.6 KiB
TypeScript

/**
* @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
*/
import {ConfigAPI, PluginObj} from '@babel/core';
import {ConsoleLogger, LogLevel} from '../../../src/ngtsc/logging';
import {LinkerOptions} from '../../src/file_linker/linker_options';
import {createEs2015LinkerPlugin} from './es2015_linker_plugin';
import {NodeJSFileSystem} from '../../../src/ngtsc/file_system/src/node_js_file_system';
/**
* This is the Babel plugin definition that is provided as a default export from the package, such
* that the plugin can be used using the module specifier of the package. This is the recommended
* way of integrating the Angular Linker into a build pipeline other than the Angular CLI.
*
* When the module specifier `@angular/compiler-cli/linker/babel` is used as a plugin in a Babel
* configuration, Babel invokes this function (by means of the default export) to create the plugin
* instance according to the provided options.
*
* The linker plugin that is created uses the native NodeJS filesystem APIs to interact with the
* filesystem. Any logging output is printed to the console.
*
* @param api Provides access to the Babel environment that is configuring this plugin.
* @param options The plugin options that have been configured.
*/
export function defaultLinkerPlugin(api: ConfigAPI, options: Partial<LinkerOptions>): PluginObj {
api.assertVersion(7);
return createEs2015LinkerPlugin({
...options,
fileSystem: new NodeJSFileSystem(),
logger: new ConsoleLogger(LogLevel.info),
});
}