Skip to content

Commit 507f756

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): downlevel class private methods when targeting Safari <=v15
This commits enables `@babel/plugin-proposal-private-methods` when targeting Safari <=v15 as this is needed to handle private class methods when using `@babel/plugin-proposal-class-properties`. Closes #24411 (cherry picked from commit 9737301)
1 parent b2d4415 commit 507f756

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

packages/angular_devkit/build_angular/src/babel/presets/application.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ export default function (api: unknown, options: ApplicationPresetOptions) {
197197
// downlevel class properties by ensuring the class properties Babel plugin
198198
// is always included- regardless of the preset-env targets.
199199
if (options.supportedBrowsers.some((b) => safariClassFieldScopeBugBrowsers.has(b))) {
200-
includePlugins.push('@babel/plugin-proposal-class-properties');
200+
includePlugins.push(
201+
'@babel/plugin-proposal-class-properties',
202+
'@babel/plugin-proposal-private-methods',
203+
);
201204
}
202205

203206
presets.push([
Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,70 @@
1-
import { expectFileToExist, readFile, writeFile } from '../../utils/fs';
1+
import assert from 'node:assert';
2+
import { expectFileToExist, readFile, writeFile, replaceInFile } from '../../utils/fs';
23
import { ng } from '../../utils/process';
3-
import { updateJsonFile } from '../../utils/project';
44

55
const unexpectedStaticFieldErrorMessage =
66
'Found unexpected static field. This indicates that the Safari <=v15 ' +
77
'workaround for a scope variable tracking is not working. ' +
88
'See: https://github.com/angular/angular-cli/pull/24357';
99

1010
export default async function () {
11-
await updateJsonFile('angular.json', (workspace) => {
12-
const build = workspace.projects['test-project'].architect.build;
13-
build.defaultConfiguration = undefined;
14-
build.options = {
15-
...build.options,
16-
optimization: false,
17-
outputHashing: 'none',
18-
};
19-
});
11+
// Add a private method
12+
await replaceInFile(
13+
'src/app/app.component.ts',
14+
`title = 'test-project';`,
15+
`
16+
#myPrivateMethod() { return 1 }
17+
18+
constructor() {
19+
console.log(this.#myPrivateMethod)
20+
}
21+
22+
title = 'test-project';`,
23+
);
2024

2125
// Matches two types of static fields that indicate that the Safari bug
2226
// may still occur. With the workaround this should not appear in bundles.
2327
// - static { this.ecmp = bla }
2428
// - static #_ = this.ecmp = bla
2529
const staticIndicatorRegex = /static\s+(\{|#[_\d]+\s+=)/;
2630

27-
await ng('build');
31+
await ng('build', '--configuration=development');
2832
await expectFileToExist('dist/test-project/main.js');
2933
const mainContent = await readFile('dist/test-project/main.js');
30-
3134
// TODO: This default cause can be removed in the future when Safari v15
3235
// is longer included in the default browserlist configuration of CLI apps.
33-
if (staticIndicatorRegex.test(mainContent)) {
34-
throw new Error(unexpectedStaticFieldErrorMessage);
35-
}
36+
assert.doesNotMatch(mainContent, staticIndicatorRegex, unexpectedStaticFieldErrorMessage);
3637

3738
await writeFile('.browserslistrc', 'last 1 chrome version');
38-
39-
await ng('build');
39+
await ng('build', '--configuration=development');
4040
await expectFileToExist('dist/test-project/main.js');
4141
const mainContentChromeLatest = await readFile('dist/test-project/main.js');
4242

43-
if (!staticIndicatorRegex.test(mainContentChromeLatest)) {
44-
throw new Error('Expected static fields to be used when Safari <=v15 is not targeted.');
45-
}
43+
assert.match(
44+
mainContentChromeLatest,
45+
staticIndicatorRegex,
46+
'Expected static fields to be used when Safari <=v15 is not targeted.',
47+
);
48+
assert.match(
49+
mainContentChromeLatest,
50+
/#myPrivateMethod/,
51+
'Expected private method to be used when Safari <=v15 is not targeted.',
52+
);
4653

4754
await writeFile('.browserslistrc', 'Safari <=15');
4855

49-
await ng('build');
56+
await ng('build', '--configuration=development');
5057
await expectFileToExist('dist/test-project/main.js');
5158
const mainContentSafari15Explicit = await readFile('dist/test-project/main.js');
59+
assert.doesNotMatch(
60+
mainContentSafari15Explicit,
61+
staticIndicatorRegex,
62+
unexpectedStaticFieldErrorMessage,
63+
);
5264

53-
if (staticIndicatorRegex.test(mainContentSafari15Explicit)) {
54-
throw new Error(unexpectedStaticFieldErrorMessage);
55-
}
65+
assert.match(
66+
mainContentSafari15Explicit,
67+
/var _myPrivateMethod/,
68+
'Expected private method to be downlevelled when Safari <=v15 is targeted',
69+
);
5670
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy