Skip to content

Commit eb3992d

Browse files
committed
refactor(@schematics/angular): use new .browserslistrc file in ng generate config
Note that the generated file does include `bl2bl` branding: ``` # This file was created by bl2bl2. # Learn more at https://github.com/web-platform-dx/bl2bl ``` I opted to leave that in as it is technically accurate, though might be a bit confusing to users in this context.
1 parent 4e1b962 commit eb3992d

File tree

6 files changed

+60
-26
lines changed

6 files changed

+60
-26
lines changed

packages/angular/build/BUILD.bazel

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ bl2bl_bin.bl2bl(
4747
chdir = "%s/src/baseline" % package_name(),
4848
)
4949

50+
filegroup(
51+
name = "angular_baseline_config",
52+
srcs = ["src/browserslist/package.json"],
53+
visibility = ["//packages/schematics/angular:__pkg__"],
54+
)
55+
5056
RUNTIME_ASSETS = glob(
5157
include = [
5258
"src/**/schema.json",

packages/schematics/angular/BUILD.bazel

+9
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,20 @@ copy_to_bin(
4242
srcs = glob(["**/schema.json"]),
4343
)
4444

45+
# Vendor the Baseline configuration into this package.
46+
genrule(
47+
name = "angular_baseline_config",
48+
srcs = ["//packages/angular/build:angular_baseline_config"],
49+
outs = ["config/baseline/package.json"],
50+
cmd = "cp $< $@",
51+
)
52+
4553
RUNTIME_ASSETS = [
4654
"collection.json",
4755
"migrations/migration-collection.json",
4856
"package.json",
4957
"utility/latest-versions/package.json",
58+
":angular_baseline_config",
5059
] + glob(
5160
include = [
5261
"*/schema.json",

packages/schematics/angular/config/files/.browserslistrc.template

-17
This file was deleted.

packages/schematics/angular/config/index.ts

+35-7
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ import {
1414
filter,
1515
mergeWith,
1616
move,
17+
noop,
1718
strings,
1819
url,
1920
} from '@angular-devkit/schematics';
21+
import { promises as fs } from 'node:fs';
2022
import { posix as path } from 'node:path';
2123
import { relativePathToWorkspaceRoot } from '../utility/paths';
2224
import { getWorkspace as readWorkspace, updateWorkspace } from '../utility/workspace';
2325
import { Builders as AngularBuilder } from '../utility/workspace-models';
2426
import { Schema as ConfigOptions, Type as ConfigType } from './schema';
27+
import { JSONFile } from '../utility/json-file';
28+
import { addPackageJsonDependency, NodeDependencyType } from '../utility/dependencies';
29+
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
30+
import { latestVersions } from '../utility/latest-versions';
2531

2632
export default function (options: ConfigOptions): Rule {
2733
switch (options.type) {
@@ -35,20 +41,42 @@ export default function (options: ConfigOptions): Rule {
3541
}
3642

3743
function addBrowserslistConfig(options: ConfigOptions): Rule {
38-
return async (host) => {
44+
return async (host, context) => {
3945
const workspace = await readWorkspace(host);
4046
const project = workspace.projects.get(options.project);
4147
if (!project) {
4248
throw new SchematicsException(`Project name "${options.project}" doesn't not exist.`);
4349
}
4450

45-
return mergeWith(
46-
apply(url('./files'), [
47-
filter((p) => p.endsWith('.browserslistrc.template')),
48-
applyTemplates({}),
49-
move(project.root),
50-
]),
51+
// Read Angular's default vendored `.browserslistrc` file.
52+
const config = JSON.parse(
53+
await fs.readFile(path.join(__dirname, 'baseline/package.json'), 'utf8'),
54+
);
55+
const widelyAvailableOnDate = config.bl2bl.baselineThreshold as string;
56+
57+
const pkgJson = new JSONFile(host, 'package.json');
58+
pkgJson.modify(
59+
['browserslist'],
60+
['extends browserslist-config-baseline'],
61+
/* insertInOrder */ false,
62+
);
63+
pkgJson.modify(
64+
['browserslist-config-baseline'],
65+
{
66+
widelyAvailableOnDate,
67+
},
68+
/* insertInOrder */ false,
5169
);
70+
71+
addPackageJsonDependency(host, {
72+
type: NodeDependencyType.Dev,
73+
name: 'browserslist-config-baseline',
74+
version: latestVersions['browserslist-config-baseline'],
75+
});
76+
77+
context.addTask(new NodePackageInstallTask());
78+
79+
return noop;
5280
};
5381
}
5482

packages/schematics/angular/config/index_spec.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,16 @@ describe('Config Schematic', () => {
9292
});
9393

9494
describe(`when 'type' is 'browserslist'`, () => {
95-
it('should create a .browserslistrc file', async () => {
95+
it('should create a browserslist configuration', async () => {
9696
const tree = await runConfigSchematic(ConfigType.Browserslist);
97-
expect(tree.exists('projects/foo/.browserslistrc')).toBeTrue();
97+
const config = JSON.parse(tree.readContent('package.json'));
98+
99+
expect(config.browserslist).toEqual(['extends browserslist-config-baseline']);
100+
expect(config['browserslist-config-baseline']).toEqual({
101+
widelyAvailableOnDate: jasmine.any(String),
102+
});
103+
104+
expect(config.devDependencies['browserslist-config-baseline']).toBeDefined();
98105
});
99106
});
100107
});

packages/schematics/angular/utility/latest-versions/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@types/jasmine": "~5.1.0",
88
"@types/node": "^20.17.19",
99
"browser-sync": "^3.0.0",
10+
"browserslist-config-baseline": "^0.4.0",
1011
"express": "^5.1.0",
1112
"jasmine-core": "~5.6.0",
1213
"jasmine-spec-reporter": "~7.0.0",

0 commit comments

Comments
 (0)