|
1 | | -import { expectFileToExist, readFile, writeFile } from '../../utils/fs'; |
| 1 | +import assert from 'node:assert'; |
| 2 | +import { expectFileToExist, readFile, writeFile, replaceInFile } from '../../utils/fs'; |
2 | 3 | import { ng } from '../../utils/process'; |
3 | | -import { updateJsonFile } from '../../utils/project'; |
4 | 4 |
|
5 | 5 | const unexpectedStaticFieldErrorMessage = |
6 | 6 | 'Found unexpected static field. This indicates that the Safari <=v15 ' + |
7 | 7 | 'workaround for a scope variable tracking is not working. ' + |
8 | 8 | 'See: https://github.com/angular/angular-cli/pull/24357'; |
9 | 9 |
|
10 | 10 | 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 | + ); |
20 | 24 |
|
21 | 25 | // Matches two types of static fields that indicate that the Safari bug |
22 | 26 | // may still occur. With the workaround this should not appear in bundles. |
23 | 27 | // - static { this.ecmp = bla } |
24 | 28 | // - static #_ = this.ecmp = bla |
25 | 29 | const staticIndicatorRegex = /static\s+(\{|#[_\d]+\s+=)/; |
26 | 30 |
|
27 | | - await ng('build'); |
| 31 | + await ng('build', '--configuration=development'); |
28 | 32 | await expectFileToExist('dist/test-project/main.js'); |
29 | 33 | const mainContent = await readFile('dist/test-project/main.js'); |
30 | | - |
31 | 34 | // TODO: This default cause can be removed in the future when Safari v15 |
32 | 35 | // 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); |
36 | 37 |
|
37 | 38 | await writeFile('.browserslistrc', 'last 1 chrome version'); |
38 | | - |
39 | | - await ng('build'); |
| 39 | + await ng('build', '--configuration=development'); |
40 | 40 | await expectFileToExist('dist/test-project/main.js'); |
41 | 41 | const mainContentChromeLatest = await readFile('dist/test-project/main.js'); |
42 | 42 |
|
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 | + ); |
46 | 53 |
|
47 | 54 | await writeFile('.browserslistrc', 'Safari <=15'); |
48 | 55 |
|
49 | | - await ng('build'); |
| 56 | + await ng('build', '--configuration=development'); |
50 | 57 | await expectFileToExist('dist/test-project/main.js'); |
51 | 58 | const mainContentSafari15Explicit = await readFile('dist/test-project/main.js'); |
| 59 | + assert.doesNotMatch( |
| 60 | + mainContentSafari15Explicit, |
| 61 | + staticIndicatorRegex, |
| 62 | + unexpectedStaticFieldErrorMessage, |
| 63 | + ); |
52 | 64 |
|
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 | + ); |
56 | 70 | } |
0 commit comments