-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular-devkit/build-angular): support targeting ES2017 with Zo…
…ne.js This change causes native async functions to be downleveled when an application targets ES2017 within its TypeScript configuration. Any source file that contains the async keyword will be processed including libraries. Since Zone.js does not support native async, this processing allows Zone.js to function with an ES2017 target.
- Loading branch information
1 parent
5cdf4cc
commit e2e8d57
Showing
7 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/angular_devkit/build_angular/src/browser/tests/behavior/typescript-target_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. 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.io/license | ||
*/ | ||
import { buildWebpackBrowser } from '../../index'; | ||
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; | ||
|
||
describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { | ||
describe('Behavior: "TypeScript Configuration - target"', () => { | ||
it('downlevels async functions when targetting ES2017', async () => { | ||
// Set TypeScript configuration target to ES2017 to enable native async | ||
await harness.modifyFile('src/tsconfig.app.json', (content) => { | ||
const tsconfig = JSON.parse(content); | ||
if (!tsconfig.compilerOptions) { | ||
tsconfig.compilerOptions = {}; | ||
} | ||
tsconfig.compilerOptions.target = 'es2017'; | ||
|
||
return JSON.stringify(tsconfig); | ||
}); | ||
|
||
// Add a JavaScript file with async code | ||
await harness.writeFile( | ||
'src/async-test.js', | ||
'async function testJs() { console.log("from-async-js-function"); }', | ||
); | ||
|
||
// Add an async function to the project as well as JavaScript file | ||
await harness.modifyFile( | ||
'src/main.ts', | ||
(content) => | ||
'import "./async-test";\n' + | ||
content + | ||
`\nasync function testApp(): Promise<void> { console.log("from-async-app-function"); }`, | ||
); | ||
|
||
harness.useTarget('build', { | ||
...BASE_OPTIONS, | ||
}); | ||
|
||
const { result, logs } = await harness.executeOnce(); | ||
|
||
expect(result?.success).toBe(true); | ||
expect(logs).not.toContain( | ||
jasmine.objectContaining({ | ||
message: jasmine.stringMatching('Zone.js does not support native async/await in ES2017+'), | ||
}), | ||
); | ||
|
||
harness.expectFile('dist/main.js').content.not.toMatch(/\sasync\s/); | ||
harness.expectFile('dist/main.js').content.toContain('"from-async-app-function"'); | ||
harness.expectFile('dist/main.js').content.toContain('"from-async-js-function"'); | ||
}); | ||
|
||
it('downlevels async functions when targetting greater than ES2017', async () => { | ||
// Set TypeScript configuration target greater than ES2017 to enable native async | ||
await harness.modifyFile('src/tsconfig.app.json', (content) => { | ||
const tsconfig = JSON.parse(content); | ||
if (!tsconfig.compilerOptions) { | ||
tsconfig.compilerOptions = {}; | ||
} | ||
tsconfig.compilerOptions.target = 'es2020'; | ||
|
||
return JSON.stringify(tsconfig); | ||
}); | ||
|
||
// Add an async function to the project | ||
await harness.writeFile( | ||
'src/main.ts', | ||
'async function test(): Promise<void> { console.log("from-async-function"); }', | ||
); | ||
|
||
harness.useTarget('build', { | ||
...BASE_OPTIONS, | ||
}); | ||
|
||
const { result, logs } = await harness.executeOnce(); | ||
|
||
expect(result?.success).toBe(true); | ||
expect(logs).not.toContain( | ||
jasmine.objectContaining({ | ||
message: jasmine.stringMatching('Zone.js does not support native async/await in ES2017+'), | ||
}), | ||
); | ||
|
||
harness.expectFile('dist/main.js').content.not.toMatch(/\sasync\s/); | ||
harness.expectFile('dist/main.js').content.toContain('"from-async-function"'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters