|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import babel from '@babel/core'; |
| 10 | +import {createEs2015LinkerPlugin} from '@angular/compiler-cli/linker/babel'; |
| 11 | +import {ConsoleLogger, NodeJSFileSystem, LogLevel} from '@angular/compiler-cli'; |
| 12 | +import {GLOBAL_DEFS_FOR_TERSER_WITH_AOT} from '@angular/compiler-cli/private/tooling'; |
| 13 | +import adjustStaticClassMembersPlugin from '@angular-devkit/build-angular/src/babel/plugins/adjust-static-class-members.js'; |
| 14 | +import elideAngularMetadataPlugin from '@angular-devkit/build-angular/src/babel/plugins/elide-angular-metadata.js'; |
| 15 | +import adjustTypeScriptEnumsPlugin from '@angular-devkit/build-angular/src/babel/plugins/adjust-typescript-enums.js'; |
| 16 | +import pureToplevelFunctionsPlugin from '@angular-devkit/build-angular/src/babel/plugins/pure-toplevel-functions.js'; |
| 17 | +import fs from 'fs'; |
| 18 | + |
| 19 | +/** Babel plugin running the Angular linker. */ |
| 20 | +const linkerBabelPlugin = createEs2015LinkerPlugin({ |
| 21 | + fileSystem: new NodeJSFileSystem(), |
| 22 | + logger: new ConsoleLogger(LogLevel.warn), |
| 23 | + linkerJitMode: false, |
| 24 | +}); |
| 25 | + |
| 26 | +/** |
| 27 | + * ESBuild plugin configuring various optimization Babel plugins. The Babel plugins |
| 28 | + * configured as part of this plugin run in the Angular CLI compilation pipeline as well. |
| 29 | + */ |
| 30 | +const esbuildBabelOptimizePlugin = { |
| 31 | + name: 'ng-babel-optimize-esbuild', |
| 32 | + setup: build => { |
| 33 | + build.onLoad({filter: /.*/}, async args => { |
| 34 | + const filePath = args.path; |
| 35 | + const content = await fs.promises.readFile(filePath, 'utf8'); |
| 36 | + const plugins = [ |
| 37 | + linkerBabelPlugin, |
| 38 | + adjustStaticClassMembersPlugin, |
| 39 | + elideAngularMetadataPlugin, |
| 40 | + adjustTypeScriptEnumsPlugin, |
| 41 | + ]; |
| 42 | + |
| 43 | + // All files except for the auto-generated module entry-point are considered side-effect |
| 44 | + // free. For these we can add the pure-top level Babel plugin. This matches conceptually |
| 45 | + // with what is done in the Angular CLI compilation pipeline, with respect to everything |
| 46 | + // in this repo being an official side-effect free APF package. |
| 47 | + if (!args.path.includes('autogenerated_module_index.mjs')) { |
| 48 | + plugins.push(pureToplevelFunctionsPlugin); |
| 49 | + } |
| 50 | + |
| 51 | + const {code} = await babel.transformAsync(content, { |
| 52 | + filename: filePath, |
| 53 | + filenameRelative: filePath, |
| 54 | + plugins: plugins, |
| 55 | + // Sourcemaps are generated inline so that ESBuild can process them. |
| 56 | + sourceMaps: 'inline', |
| 57 | + compact: false, |
| 58 | + }); |
| 59 | + |
| 60 | + return {contents: code}; |
| 61 | + }); |
| 62 | + }, |
| 63 | +}; |
| 64 | + |
| 65 | +export default { |
| 66 | + // Note: We prefer `.mjs` here as this is the extension used by Angular APF packages. |
| 67 | + resolveExtensions: ['.mjs', '.js'], |
| 68 | + conditions: ['es2020', 'es2015'], |
| 69 | + mainFields: ['fesm2020', 'es2020', 'es2015', 'module'], |
| 70 | + format: 'iife', |
| 71 | + // The majority of these options match with the ones the CLI sets: |
| 72 | + // https://github.com/angular/angular-cli/blob/0d76bf04bca6e083865972b5398a32bbe9396e14/packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-worker.ts#L133. |
| 73 | + treeShaking: true, |
| 74 | + minifyIdentifiers: true, |
| 75 | + minifySyntax: true, |
| 76 | + minifyWhitespace: false, |
| 77 | + pure: ['forwardRef'], |
| 78 | + legalComments: 'none', |
| 79 | + // ESBuild requires the `define` option to take a string-based dictionary. |
| 80 | + define: convertObjectToStringDictionary(GLOBAL_DEFS_FOR_TERSER_WITH_AOT), |
| 81 | + plugins: [esbuildBabelOptimizePlugin], |
| 82 | +}; |
| 83 | + |
| 84 | +/** Converts an object to a string dictionary. */ |
| 85 | +function convertObjectToStringDictionary(value) { |
| 86 | + return Object.entries(value).reduce((result, [propName, value]) => { |
| 87 | + result[propName] = String(value); |
| 88 | + return result; |
| 89 | + }, {}); |
| 90 | +} |
0 commit comments