From efc2a41d1dc3055cd7b2632f3c653960e83a03c2 Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Wed, 9 Jul 2025 18:21:38 +0000 Subject: [PATCH] build: use rollup directory instead of rollup through @bazel/rollup (#62556) Use rollup directly instead of using the previous toolchain implementation PR Close #62556 --- packages/core/schematics/BUILD.bazel | 130 ++++++++++++------ packages/core/schematics/rollup.config.js | 2 + packages/language-service/bundles/BUILD.bazel | 33 +++-- .../language-service/bundles/rollup.config.js | 9 +- tools/bazel/rollup/BUILD.bazel | 12 ++ tools/bazel/rollup/path-plugin.cjs | 49 +++++++ 6 files changed, 184 insertions(+), 51 deletions(-) create mode 100644 tools/bazel/rollup/BUILD.bazel create mode 100644 tools/bazel/rollup/path-plugin.cjs diff --git a/packages/core/schematics/BUILD.bazel b/packages/core/schematics/BUILD.bazel index ce2cc2492ef..8f739f33f33 100644 --- a/packages/core/schematics/BUILD.bazel +++ b/packages/core/schematics/BUILD.bazel @@ -1,5 +1,5 @@ load("@aspect_rules_js//js:defs.bzl", "js_library") -load("@npm//@bazel/rollup:index.bzl", "rollup_bundle") +load("@npm2//:rollup/package_json.bzl", rollup = "bin") load("//tools:defaults.bzl", "pkg_npm") load("//tools:defaults2.bzl", "ts_config") @@ -61,46 +61,98 @@ pkg_npm( ], ) -rollup_bundle( +bundle_entrypoints = [ + [ + "inject-migration", + "packages/core/schematics/ng-generate/inject-migration/index.js", + ], + [ + "route-lazy-loading", + "packages/core/schematics/ng-generate/route-lazy-loading/index.js", + ], + [ + "standalone-migration", + "packages/core/schematics/ng-generate/standalone-migration/index.js", + ], + [ + "cleanup-unused-imports", + "packages/core/schematics/ng-generate/cleanup-unused-imports/index.js", + ], + [ + "signals", + "packages/core/schematics/ng-generate/signals/index.js", + ], + [ + "signal-input-migration", + "packages/core/schematics/ng-generate/signal-input-migration/index.js", + ], + [ + "signal-queries-migration", + "packages/core/schematics/ng-generate/signal-queries-migration/index.js", + ], + [ + "output-migration", + "packages/core/schematics/ng-generate/output-migration/index.js", + ], + [ + "self-closing-tags-migration", + "packages/core/schematics/ng-generate/self-closing-tags-migration/index.js", + ], + [ + "inject-flags", + "packages/core/schematics/migrations/inject-flags/index.js", + ], + [ + "test-bed-get", + "packages/core/schematics/migrations/test-bed-get/index.js", + ], + [ + "document-core", + "packages/core/schematics/migrations/document-core/index.js", + ], + [ + "control-flow-migration", + "packages/core/schematics/migrations/control-flow-migration/index.js", + ], +] + +rollup.rollup( name = "bundles", - config_file = ":rollup.config.js", - entry_points = { - "//packages/core/schematics/ng-generate/inject-migration:index.ts": "inject-migration", - "//packages/core/schematics/ng-generate/route-lazy-loading:index.ts": "route-lazy-loading", - "//packages/core/schematics/ng-generate/standalone-migration:index.ts": "standalone-migration", - "//packages/core/schematics/ng-generate/cleanup-unused-imports:index.ts": "cleanup-unused-imports", - "//packages/core/schematics/ng-generate/signals:index.ts": "signals", - "//packages/core/schematics/ng-generate/signal-input-migration:index.ts": "signal-input-migration", - "//packages/core/schematics/ng-generate/signal-queries-migration:index.ts": "signal-queries-migration", - "//packages/core/schematics/ng-generate/output-migration:index.ts": "output-migration", - "//packages/core/schematics/ng-generate/self-closing-tags-migration:index.ts": "self-closing-tags-migration", - "//packages/core/schematics/migrations/inject-flags:index.ts": "inject-flags", - "//packages/core/schematics/migrations/test-bed-get:index.ts": "test-bed-get", - "//packages/core/schematics/migrations/document-core:index.ts": "document-core", - "//packages/core/schematics/migrations/control-flow-migration:index.ts": "control-flow-migration", - }, - format = "cjs", - link_workspace_root = True, - output_dir = True, - sourcemap = "false", + srcs = [ + "rollup.config.js", + "//:node_modules/@rollup/plugin-commonjs", + "//:node_modules/@rollup/plugin-node-resolve", + "//:node_modules/magic-string", + "//:node_modules/semver", + "//packages/core/schematics:tsconfig_build", + "//packages/core/schematics/migrations/control-flow-migration:control-flow-migration_rjs", + "//packages/core/schematics/migrations/document-core:document-core_rjs", + "//packages/core/schematics/migrations/inject-flags:inject-flags_rjs", + "//packages/core/schematics/migrations/test-bed-get:test-bed-get_rjs", + "//packages/core/schematics/ng-generate/cleanup-unused-imports:cleanup-unused-imports_rjs", + "//packages/core/schematics/ng-generate/inject-migration:inject-migration_rjs", + "//packages/core/schematics/ng-generate/output-migration:output-migration_rjs", + "//packages/core/schematics/ng-generate/route-lazy-loading:route-lazy-loading_rjs", + "//packages/core/schematics/ng-generate/self-closing-tags-migration:self-closing-tags-migration_rjs", + "//packages/core/schematics/ng-generate/signal-input-migration:signal-input-migration_rjs", + "//packages/core/schematics/ng-generate/signal-queries-migration:signal-queries-migration_rjs", + "//packages/core/schematics/ng-generate/signals:signals_rjs", + "//packages/core/schematics/ng-generate/standalone-migration:standalone-migration_rjs", + "//tools/bazel/rollup:path-plugin", + ], + args = [ + "--format=cjs", + "--config=$(rootpath rollup.config.js)", + "--dir=packages/core/schematics/bundles", + "--no-sourcemap", + ] + ["--input=%s=%s" % (name, path) for [ + name, + path, + ] in bundle_entrypoints], + out_dirs = [ + "bundles", + ], visibility = [ "//packages/core/schematics/test:__pkg__", ], - deps = [ - "//packages/core/schematics/migrations/control-flow-migration", - "//packages/core/schematics/migrations/document-core", - "//packages/core/schematics/migrations/inject-flags", - "//packages/core/schematics/migrations/test-bed-get", - "//packages/core/schematics/ng-generate/cleanup-unused-imports", - "//packages/core/schematics/ng-generate/inject-migration", - "//packages/core/schematics/ng-generate/output-migration", - "//packages/core/schematics/ng-generate/route-lazy-loading", - "//packages/core/schematics/ng-generate/self-closing-tags-migration", - "//packages/core/schematics/ng-generate/signal-input-migration", - "//packages/core/schematics/ng-generate/signal-queries-migration", - "//packages/core/schematics/ng-generate/signals", - "//packages/core/schematics/ng-generate/standalone-migration", - "@npm//@rollup/plugin-commonjs", - "@npm//@rollup/plugin-node-resolve", - ], ) diff --git a/packages/core/schematics/rollup.config.js b/packages/core/schematics/rollup.config.js index bbcf1c0727c..614b3ceed51 100644 --- a/packages/core/schematics/rollup.config.js +++ b/packages/core/schematics/rollup.config.js @@ -7,6 +7,7 @@ */ const {nodeResolve} = require('@rollup/plugin-node-resolve'); const commonjs = require('@rollup/plugin-commonjs'); +const {pathPlugin} = require('../../../tools/bazel/rollup/path-plugin.cjs'); /** Removed license banners from input files. */ const stripBannerPlugin = { @@ -43,6 +44,7 @@ const banner = `'use strict'; */`; const plugins = [ + pathPlugin({tsconfigPath: 'packages/core/schematics/tsconfig.json'}), nodeResolve({ jail: process.cwd(), }), diff --git a/packages/language-service/bundles/BUILD.bazel b/packages/language-service/bundles/BUILD.bazel index b339da77a50..30498a4b496 100644 --- a/packages/language-service/bundles/BUILD.bazel +++ b/packages/language-service/bundles/BUILD.bazel @@ -1,15 +1,26 @@ -load("@npm//@bazel/rollup:index.bzl", "rollup_bundle") +load("@npm2//:rollup/package_json.bzl", rollup = "bin") -rollup_bundle( +rollup.rollup( name = "language-service", - config_file = "rollup.config.js", - entry_point = "//packages/language-service/src:ts_plugin.ts", - format = "amd", - silent = True, - visibility = ["//packages/language-service:__pkg__"], - deps = [ - "//packages/language-service/src", - "@npm//@rollup/plugin-commonjs", - "@npm//@rollup/plugin-node-resolve", + srcs = [ + "rollup.config.js", + "//:node_modules/@angular/compiler-cli", + "//:node_modules/@rollup/plugin-commonjs", + "//:node_modules/@rollup/plugin-node-resolve", + "//packages/language-service:tsconfig_build", + "//packages/language-service/src:src_rjs", + "//packages/language-service/src:ts_plugin.js", + "//tools/bazel/rollup:path-plugin", ], + outs = [ + "language-service.js", + ], + args = [ + "--format=amd", + "--config=$(rootpath rollup.config.js)", + "--input=packages/language-service/src/ts_plugin.js", + "--file=packages/language-service/bundles/language-service.js", + "--sourcemap=inline", + ], + visibility = ["//packages/language-service:__pkg__"], ) diff --git a/packages/language-service/bundles/rollup.config.js b/packages/language-service/bundles/rollup.config.js index c4c08e071bd..30e14b276d2 100644 --- a/packages/language-service/bundles/rollup.config.js +++ b/packages/language-service/bundles/rollup.config.js @@ -8,6 +8,7 @@ const {nodeResolve} = require('@rollup/plugin-node-resolve'); const commonJs = require('@rollup/plugin-commonjs'); +const {pathPlugin} = require('../../../tools/bazel/rollup/path-plugin.cjs'); // This is a custom AMD file header that patches the AMD `define` call generated // by rollup so that the bundle exposes a CJS-exported function which takes an @@ -47,7 +48,13 @@ const external = ['os', 'fs', 'path', 'typescript']; const config = { external, - plugins: [nodeResolve({preferBuiltins: true}), commonJs()], + plugins: [ + pathPlugin({ + tsconfigPath: 'packages/language-service/tsconfig.json', + }), + nodeResolve({preferBuiltins: true}), + commonJs(), + ], output: { banner: amdFileHeader, }, diff --git a/tools/bazel/rollup/BUILD.bazel b/tools/bazel/rollup/BUILD.bazel new file mode 100644 index 00000000000..8d8b251ead9 --- /dev/null +++ b/tools/bazel/rollup/BUILD.bazel @@ -0,0 +1,12 @@ +load("@aspect_rules_js//js:defs.bzl", "js_library") + +js_library( + name = "path-plugin", + srcs = [ + "path-plugin.cjs", + ], + visibility = ["//visibility:public"], + deps = [ + "//:node_modules/get-tsconfig", + ], +) diff --git a/tools/bazel/rollup/path-plugin.cjs b/tools/bazel/rollup/path-plugin.cjs new file mode 100644 index 00000000000..076b3e5f04c --- /dev/null +++ b/tools/bazel/rollup/path-plugin.cjs @@ -0,0 +1,49 @@ +/** + * @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 + */ + +const {existsSync, statSync} = require('node:fs'); +const {join} = require('node:path'); +const {parseTsconfig, createPathsMatcher} = require('get-tsconfig'); + +function pathPlugin({tsconfigPath}) { + if (tsconfigPath === undefined) { + throw Error('A path tsconfig file must be provided.'); + } + const fullTsconfigPath = join(process.cwd(), tsconfigPath); + const tsconfig = parseTsconfig(fullTsconfigPath); + const pathMappingMatcher = createPathsMatcher({config: tsconfig, path: fullTsconfigPath}); + return { + name: 'paths', + resolveId: (source) => { + /** + * A list containing all of the potential paths which match the provided source based + * on the paths field from the tsconfig. + */ + const matchedSources = pathMappingMatcher(source); + if (matchedSources.length == 0) { + return null; + } + // We need to check each matched source to see if it loads at the path directly, or is + // a directory with an index.js file to import. + for (let matchedSource of matchedSources) { + const indexPath = join(matchedSource, 'index.js'); + if (existsSync(indexPath) && statSync(indexPath).isFile) { + return {id: indexPath}; + } + const filePath = matchedSource + '.js'; + if (existsSync(filePath) && statSync(filePath).isFile) { + return {id: filePath}; + } + } + + throw Error(`Cannot find ${source}\nLocations checked:\n-${matchedSources.join('\n')}`); + }, + }; +} + +module.exports = {pathPlugin};