|
| 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.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { buildEsbuildBrowser } from '../../index'; |
| 10 | +import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; |
| 11 | + |
| 12 | +describeBuilder(buildEsbuildBrowser, BROWSER_BUILDER_INFO, (harness) => { |
| 13 | + describe('Behavior: "Browser support"', () => { |
| 14 | + it('creates correct sourcemaps when downleveling async functions', async () => { |
| 15 | + // Add a JavaScript file with async code |
| 16 | + await harness.writeFile( |
| 17 | + 'src/async-test.js', |
| 18 | + 'async function testJs() { console.log("from-async-js-function"); }', |
| 19 | + ); |
| 20 | + |
| 21 | + // Add an async function to the project as well as JavaScript file |
| 22 | + // The type `Void123` is used as a unique identifier for the final sourcemap |
| 23 | + // If sourcemaps are not properly propagated then it will not be in the final sourcemap |
| 24 | + await harness.modifyFile( |
| 25 | + 'src/main.ts', |
| 26 | + (content) => |
| 27 | + 'import "./async-test";\n' + |
| 28 | + content + |
| 29 | + '\ntype Void123 = void;' + |
| 30 | + `\nasync function testApp(): Promise<Void123> { console.log("from-async-app-function"); }`, |
| 31 | + ); |
| 32 | + |
| 33 | + harness.useTarget('build', { |
| 34 | + ...BASE_OPTIONS, |
| 35 | + sourceMap: { |
| 36 | + scripts: true, |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + const { result } = await harness.executeOnce(); |
| 41 | + |
| 42 | + expect(result?.success).toBe(true); |
| 43 | + harness.expectFile('dist/main.js').content.not.toMatch(/\sasync\s+function\s/); |
| 44 | + harness.expectFile('dist/main.js.map').content.toContain('Promise<Void123>'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('downlevels async functions ', async () => { |
| 48 | + // Add an async function to the project |
| 49 | + await harness.writeFile( |
| 50 | + 'src/main.ts', |
| 51 | + 'async function test(): Promise<void> { console.log("from-async-function"); }\ntest();', |
| 52 | + ); |
| 53 | + |
| 54 | + harness.useTarget('build', { |
| 55 | + ...BASE_OPTIONS, |
| 56 | + }); |
| 57 | + |
| 58 | + const { result } = await harness.executeOnce(); |
| 59 | + |
| 60 | + expect(result?.success).toBe(true); |
| 61 | + harness.expectFile('dist/main.js').content.not.toMatch(/\sasync\s/); |
| 62 | + harness.expectFile('dist/main.js').content.toContain('"from-async-function"'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('warns when IE is present in browserslist', async () => { |
| 66 | + await harness.appendToFile( |
| 67 | + '.browserslistrc', |
| 68 | + ` |
| 69 | + IE 9 |
| 70 | + IE 11 |
| 71 | + `, |
| 72 | + ); |
| 73 | + |
| 74 | + harness.useTarget('build', { |
| 75 | + ...BASE_OPTIONS, |
| 76 | + }); |
| 77 | + |
| 78 | + const { result, logs } = await harness.executeOnce(); |
| 79 | + expect(result?.success).toBeTrue(); |
| 80 | + |
| 81 | + expect(logs).toContain( |
| 82 | + jasmine.objectContaining({ |
| 83 | + level: 'warn', |
| 84 | + message: |
| 85 | + `One or more browsers which are configured in the project's Browserslist ` + |
| 86 | + 'configuration will be ignored as ES5 output is not supported by the Angular CLI.\n' + |
| 87 | + 'Ignored browsers: ie 11, ie 9', |
| 88 | + }), |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('downlevels "for await...of"', async () => { |
| 93 | + // Add an async function to the project |
| 94 | + await harness.writeFile( |
| 95 | + 'src/main.ts', |
| 96 | + ` |
| 97 | + (async () => { |
| 98 | + for await (const o of [1, 2, 3]) { |
| 99 | + console.log("for await...of"); |
| 100 | + } |
| 101 | + })(); |
| 102 | + `, |
| 103 | + ); |
| 104 | + |
| 105 | + harness.useTarget('build', { |
| 106 | + ...BASE_OPTIONS, |
| 107 | + }); |
| 108 | + |
| 109 | + const { result } = await harness.executeOnce(); |
| 110 | + |
| 111 | + expect(result?.success).toBe(true); |
| 112 | + harness.expectFile('dist/main.js').content.not.toMatch(/\sawait\s/); |
| 113 | + harness.expectFile('dist/main.js').content.toContain('"for await...of"'); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments