Files
Alan Agius 29f9dd5e77 refactor(devtools): update angular optimization plugin for JavaScriptTransformer API
Update createEsbuildAngularOptimizePlugin to use the new options object signature for JavaScriptTransformer.transformFile, and update the @angular/build dependency to 22.2.0-next.7.
2026-09-10 15:06:43 -07:00

48 lines
1.4 KiB
JavaScript

/**
* @license
* Copyright Google LLC
*
* 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
*/
/**
* Creates an ESBuild plugin that configures various Angular optimization.
* The optimization plugins usually run in the Angular CLI compilation pipeline.
*
* @param {import('./esbuild-plugin').OptimizationOptions} opts Options
*/
export function createEsbuildAngularOptimizePlugin(opts) {
return {
name: 'ng-optimize-esbuild',
setup: async (build) => {
const {JavaScriptTransformer} = (await import('@angular/build/private')).default;
const javascriptTransformer = new JavaScriptTransformer(
{
jit: false,
advancedOptimizations: !!opts.optimize,
sourcemap: !!build.initialOptions.sourcemap,
},
/** maxWorkers */ 2,
);
build.onLoad({filter: /\.[cm]?js$/}, async (args) => {
const contents = await javascriptTransformer.transformFile(args.path, {
skipLinker: !opts.enableLinker,
sideEffects: () => {
const sideEffects = opts.optimize?.isSideEffectFree?.(args.path) !== true;
return Promise.resolve(sideEffects);
},
});
return {contents};
});
build.onDispose(() => {
void javascriptTransformer.close();
});
},
};
}