Skip to content

Commit efc2a41

Browse files
josephperrottAndrewKushnir
authored andcommitted
build: use rollup directory instead of rollup through @bazel/rollup (#62556)
Use rollup directly instead of using the previous toolchain implementation PR Close #62556
1 parent 9c45c32 commit efc2a41

File tree

6 files changed

+184
-51
lines changed

6 files changed

+184
-51
lines changed

packages/core/schematics/BUILD.bazel

Lines changed: 91 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@aspect_rules_js//js:defs.bzl", "js_library")
2-
load("@npm//@bazel/rollup:index.bzl", "rollup_bundle")
2+
load("@npm2//:rollup/package_json.bzl", rollup = "bin")
33
load("//tools:defaults.bzl", "pkg_npm")
44
load("//tools:defaults2.bzl", "ts_config")
55

@@ -61,46 +61,98 @@ pkg_npm(
6161
],
6262
)
6363

64-
rollup_bundle(
64+
bundle_entrypoints = [
65+
[
66+
"inject-migration",
67+
"packages/core/schematics/ng-generate/inject-migration/index.js",
68+
],
69+
[
70+
"route-lazy-loading",
71+
"packages/core/schematics/ng-generate/route-lazy-loading/index.js",
72+
],
73+
[
74+
"standalone-migration",
75+
"packages/core/schematics/ng-generate/standalone-migration/index.js",
76+
],
77+
[
78+
"cleanup-unused-imports",
79+
"packages/core/schematics/ng-generate/cleanup-unused-imports/index.js",
80+
],
81+
[
82+
"signals",
83+
"packages/core/schematics/ng-generate/signals/index.js",
84+
],
85+
[
86+
"signal-input-migration",
87+
"packages/core/schematics/ng-generate/signal-input-migration/index.js",
88+
],
89+
[
90+
"signal-queries-migration",
91+
"packages/core/schematics/ng-generate/signal-queries-migration/index.js",
92+
],
93+
[
94+
"output-migration",
95+
"packages/core/schematics/ng-generate/output-migration/index.js",
96+
],
97+
[
98+
"self-closing-tags-migration",
99+
"packages/core/schematics/ng-generate/self-closing-tags-migration/index.js",
100+
],
101+
[
102+
"inject-flags",
103+
"packages/core/schematics/migrations/inject-flags/index.js",
104+
],
105+
[
106+
"test-bed-get",
107+
"packages/core/schematics/migrations/test-bed-get/index.js",
108+
],
109+
[
110+
"document-core",
111+
"packages/core/schematics/migrations/document-core/index.js",
112+
],
113+
[
114+
"control-flow-migration",
115+
"packages/core/schematics/migrations/control-flow-migration/index.js",
116+
],
117+
]
118+
119+
rollup.rollup(
65120
name = "bundles",
66-
config_file = ":rollup.config.js",
67-
entry_points = {
68-
"//packages/core/schematics/ng-generate/inject-migration:index.ts": "inject-migration",
69-
"//packages/core/schematics/ng-generate/route-lazy-loading:index.ts": "route-lazy-loading",
70-
"//packages/core/schematics/ng-generate/standalone-migration:index.ts": "standalone-migration",
71-
"//packages/core/schematics/ng-generate/cleanup-unused-imports:index.ts": "cleanup-unused-imports",
72-
"//packages/core/schematics/ng-generate/signals:index.ts": "signals",
73-
"//packages/core/schematics/ng-generate/signal-input-migration:index.ts": "signal-input-migration",
74-
"//packages/core/schematics/ng-generate/signal-queries-migration:index.ts": "signal-queries-migration",
75-
"//packages/core/schematics/ng-generate/output-migration:index.ts": "output-migration",
76-
"//packages/core/schematics/ng-generate/self-closing-tags-migration:index.ts": "self-closing-tags-migration",
77-
"//packages/core/schematics/migrations/inject-flags:index.ts": "inject-flags",
78-
"//packages/core/schematics/migrations/test-bed-get:index.ts": "test-bed-get",
79-
"//packages/core/schematics/migrations/document-core:index.ts": "document-core",
80-
"//packages/core/schematics/migrations/control-flow-migration:index.ts": "control-flow-migration",
81-
},
82-
format = "cjs",
83-
link_workspace_root = True,
84-
output_dir = True,
85-
sourcemap = "false",
121+
srcs = [
122+
"rollup.config.js",
123+
"//:node_modules/@rollup/plugin-commonjs",
124+
"//:node_modules/@rollup/plugin-node-resolve",
125+
"//:node_modules/magic-string",
126+
"//:node_modules/semver",
127+
"//packages/core/schematics:tsconfig_build",
128+
"//packages/core/schematics/migrations/control-flow-migration:control-flow-migration_rjs",
129+
"//packages/core/schematics/migrations/document-core:document-core_rjs",
130+
"//packages/core/schematics/migrations/inject-flags:inject-flags_rjs",
131+
"//packages/core/schematics/migrations/test-bed-get:test-bed-get_rjs",
132+
"//packages/core/schematics/ng-generate/cleanup-unused-imports:cleanup-unused-imports_rjs",
133+
"//packages/core/schematics/ng-generate/inject-migration:inject-migration_rjs",
134+
"//packages/core/schematics/ng-generate/output-migration:output-migration_rjs",
135+
"//packages/core/schematics/ng-generate/route-lazy-loading:route-lazy-loading_rjs",
136+
"//packages/core/schematics/ng-generate/self-closing-tags-migration:self-closing-tags-migration_rjs",
137+
"//packages/core/schematics/ng-generate/signal-input-migration:signal-input-migration_rjs",
138+
"//packages/core/schematics/ng-generate/signal-queries-migration:signal-queries-migration_rjs",
139+
"//packages/core/schematics/ng-generate/signals:signals_rjs",
140+
"//packages/core/schematics/ng-generate/standalone-migration:standalone-migration_rjs",
141+
"//tools/bazel/rollup:path-plugin",
142+
],
143+
args = [
144+
"--format=cjs",
145+
"--config=$(rootpath rollup.config.js)",
146+
"--dir=packages/core/schematics/bundles",
147+
"--no-sourcemap",
148+
] + ["--input=%s=%s" % (name, path) for [
149+
name,
150+
path,
151+
] in bundle_entrypoints],
152+
out_dirs = [
153+
"bundles",
154+
],
86155
visibility = [
87156
"//packages/core/schematics/test:__pkg__",
88157
],
89-
deps = [
90-
"//packages/core/schematics/migrations/control-flow-migration",
91-
"//packages/core/schematics/migrations/document-core",
92-
"//packages/core/schematics/migrations/inject-flags",
93-
"//packages/core/schematics/migrations/test-bed-get",
94-
"//packages/core/schematics/ng-generate/cleanup-unused-imports",
95-
"//packages/core/schematics/ng-generate/inject-migration",
96-
"//packages/core/schematics/ng-generate/output-migration",
97-
"//packages/core/schematics/ng-generate/route-lazy-loading",
98-
"//packages/core/schematics/ng-generate/self-closing-tags-migration",
99-
"//packages/core/schematics/ng-generate/signal-input-migration",
100-
"//packages/core/schematics/ng-generate/signal-queries-migration",
101-
"//packages/core/schematics/ng-generate/signals",
102-
"//packages/core/schematics/ng-generate/standalone-migration",
103-
"@npm//@rollup/plugin-commonjs",
104-
"@npm//@rollup/plugin-node-resolve",
105-
],
106158
)

packages/core/schematics/rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
const {nodeResolve} = require('@rollup/plugin-node-resolve');
99
const commonjs = require('@rollup/plugin-commonjs');
10+
const {pathPlugin} = require('../../../tools/bazel/rollup/path-plugin.cjs');
1011

1112
/** Removed license banners from input files. */
1213
const stripBannerPlugin = {
@@ -43,6 +44,7 @@ const banner = `'use strict';
4344
*/`;
4445

4546
const plugins = [
47+
pathPlugin({tsconfigPath: 'packages/core/schematics/tsconfig.json'}),
4648
nodeResolve({
4749
jail: process.cwd(),
4850
}),
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
load("@npm//@bazel/rollup:index.bzl", "rollup_bundle")
1+
load("@npm2//:rollup/package_json.bzl", rollup = "bin")
22

3-
rollup_bundle(
3+
rollup.rollup(
44
name = "language-service",
5-
config_file = "rollup.config.js",
6-
entry_point = "//packages/language-service/src:ts_plugin.ts",
7-
format = "amd",
8-
silent = True,
9-
visibility = ["//packages/language-service:__pkg__"],
10-
deps = [
11-
"//packages/language-service/src",
12-
"@npm//@rollup/plugin-commonjs",
13-
"@npm//@rollup/plugin-node-resolve",
5+
srcs = [
6+
"rollup.config.js",
7+
"//:node_modules/@angular/compiler-cli",
8+
"//:node_modules/@rollup/plugin-commonjs",
9+
"//:node_modules/@rollup/plugin-node-resolve",
10+
"//packages/language-service:tsconfig_build",
11+
"//packages/language-service/src:src_rjs",
12+
"//packages/language-service/src:ts_plugin.js",
13+
"//tools/bazel/rollup:path-plugin",
14+
],
15+
outs = [
16+
"language-service.js",
1417
],
18+
args = [
19+
"--format=amd",
20+
"--config=$(rootpath rollup.config.js)",
21+
"--input=packages/language-service/src/ts_plugin.js",
22+
"--file=packages/language-service/bundles/language-service.js",
23+
"--sourcemap=inline",
24+
],
25+
visibility = ["//packages/language-service:__pkg__"],
1526
)

packages/language-service/bundles/rollup.config.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
const {nodeResolve} = require('@rollup/plugin-node-resolve');
1010
const commonJs = require('@rollup/plugin-commonjs');
11+
const {pathPlugin} = require('../../../tools/bazel/rollup/path-plugin.cjs');
1112

1213
// This is a custom AMD file header that patches the AMD `define` call generated
1314
// by rollup so that the bundle exposes a CJS-exported function which takes an
@@ -47,7 +48,13 @@ const external = ['os', 'fs', 'path', 'typescript'];
4748

4849
const config = {
4950
external,
50-
plugins: [nodeResolve({preferBuiltins: true}), commonJs()],
51+
plugins: [
52+
pathPlugin({
53+
tsconfigPath: 'packages/language-service/tsconfig.json',
54+
}),
55+
nodeResolve({preferBuiltins: true}),
56+
commonJs(),
57+
],
5158
output: {
5259
banner: amdFileHeader,
5360
},

tools/bazel/rollup/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("@aspect_rules_js//js:defs.bzl", "js_library")
2+
3+
js_library(
4+
name = "path-plugin",
5+
srcs = [
6+
"path-plugin.cjs",
7+
],
8+
visibility = ["//visibility:public"],
9+
deps = [
10+
"//:node_modules/get-tsconfig",
11+
],
12+
)

tools/bazel/rollup/path-plugin.cjs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.dev/license
7+
*/
8+
9+
const {existsSync, statSync} = require('node:fs');
10+
const {join} = require('node:path');
11+
const {parseTsconfig, createPathsMatcher} = require('get-tsconfig');
12+
13+
function pathPlugin({tsconfigPath}) {
14+
if (tsconfigPath === undefined) {
15+
throw Error('A path tsconfig file must be provided.');
16+
}
17+
const fullTsconfigPath = join(process.cwd(), tsconfigPath);
18+
const tsconfig = parseTsconfig(fullTsconfigPath);
19+
const pathMappingMatcher = createPathsMatcher({config: tsconfig, path: fullTsconfigPath});
20+
return {
21+
name: 'paths',
22+
resolveId: (source) => {
23+
/**
24+
* A list containing all of the potential paths which match the provided source based
25+
* on the paths field from the tsconfig.
26+
*/
27+
const matchedSources = pathMappingMatcher(source);
28+
if (matchedSources.length == 0) {
29+
return null;
30+
}
31+
// We need to check each matched source to see if it loads at the path directly, or is
32+
// a directory with an index.js file to import.
33+
for (let matchedSource of matchedSources) {
34+
const indexPath = join(matchedSource, 'index.js');
35+
if (existsSync(indexPath) && statSync(indexPath).isFile) {
36+
return {id: indexPath};
37+
}
38+
const filePath = matchedSource + '.js';
39+
if (existsSync(filePath) && statSync(filePath).isFile) {
40+
return {id: filePath};
41+
}
42+
}
43+
44+
throw Error(`Cannot find ${source}\nLocations checked:\n-${matchedSources.join('\n')}`);
45+
},
46+
};
47+
}
48+
49+
module.exports = {pathPlugin};

0 commit comments

Comments
 (0)