|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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 | +import { logging } from '@angular-devkit/core'; |
| 9 | +import { buildWebpackBrowser } from '../../index'; |
| 10 | +import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; |
| 11 | + |
| 12 | +describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, harness => { |
| 13 | + describe('Option: "allowedCommonJsDependencies"', () => { |
| 14 | + describe('given option is not set', () => { |
| 15 | + for (const aot of [true, false]) { |
| 16 | + it(`should not show warning for styles import in ${aot ? 'AOT' : 'JIT'} Mode`, async () => { |
| 17 | + await harness.writeFile('./test.css', `body { color: red; };`); |
| 18 | + await harness.appendToFile('src/app/app.component.ts', `import '../../test.css';`); |
| 19 | + |
| 20 | + harness.useTarget('build', { |
| 21 | + ...BASE_OPTIONS, |
| 22 | + allowedCommonJsDependencies: [], |
| 23 | + aot, |
| 24 | + }); |
| 25 | + |
| 26 | + const { result, logs } = await harness.executeOnce(); |
| 27 | + |
| 28 | + expect(result?.success).toBe(true); |
| 29 | + expect(logs).not.toContain( |
| 30 | + jasmine.objectContaining<logging.LogEntry>({ |
| 31 | + message: jasmine.stringMatching(/CommonJS or AMD dependencies/), |
| 32 | + }), |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it(`should show warning when depending on a Common JS bundle in ${aot ? 'AOT' : 'JIT'} Mode`, async () => { |
| 37 | + // Add a Common JS dependency |
| 38 | + await harness.appendToFile('src/app/app.component.ts', `import 'bootstrap';`); |
| 39 | + |
| 40 | + harness.useTarget('build', { |
| 41 | + ...BASE_OPTIONS, |
| 42 | + allowedCommonJsDependencies: [], |
| 43 | + aot, |
| 44 | + }); |
| 45 | + |
| 46 | + const { result, logs } = await harness.executeOnce(); |
| 47 | + |
| 48 | + expect(result?.success).toBe(true); |
| 49 | + expect(logs).toContain( |
| 50 | + jasmine.objectContaining<logging.LogEntry>({ |
| 51 | + message: jasmine.stringMatching(/Warning: .+app\.component\.ts depends on 'bootstrap'\. CommonJS or AMD dependencies/), |
| 52 | + }), |
| 53 | + ); |
| 54 | + expect(logs).not.toContain( |
| 55 | + jasmine.objectContaining<logging.LogEntry>({ message: jasmine.stringMatching('jquery') }), |
| 56 | + 'Should not warn on transitive CommonJS packages which parent is also CommonJS.', |
| 57 | + ); |
| 58 | + }); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it('should not show warning when depending on a Common JS bundle which is allowed', async () => { |
| 63 | + // Add a Common JS dependency |
| 64 | + await harness.appendToFile('src/app/app.component.ts', ` |
| 65 | + import 'bootstrap'; |
| 66 | + import 'zone.js/dist/zone-error'; |
| 67 | + `); |
| 68 | + |
| 69 | + harness.useTarget('build', { |
| 70 | + ...BASE_OPTIONS, |
| 71 | + allowedCommonJsDependencies: [ |
| 72 | + 'bootstrap', |
| 73 | + 'zone.js', |
| 74 | + ], |
| 75 | + }); |
| 76 | + |
| 77 | + const { result, logs } = await harness.executeOnce(); |
| 78 | + |
| 79 | + expect(result?.success).toBe(true); |
| 80 | + expect(logs).not.toContain( |
| 81 | + jasmine.objectContaining<logging.LogEntry>({ |
| 82 | + message: jasmine.stringMatching(/CommonJS or AMD dependencies/), |
| 83 | + }), |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it(`should not show warning when importing non global local data '@angular/common/locale/fr'`, async () => { |
| 88 | + await harness.appendToFile('src/app/app.component.ts', `import '@angular/common/locales/fr';`); |
| 89 | + |
| 90 | + harness.useTarget('build', { |
| 91 | + ...BASE_OPTIONS, |
| 92 | + allowedCommonJsDependencies: [], |
| 93 | + }); |
| 94 | + |
| 95 | + const { result, logs } = await harness.executeOnce(); |
| 96 | + |
| 97 | + expect(result?.success).toBe(true); |
| 98 | + expect(logs).not.toContain( |
| 99 | + jasmine.objectContaining<logging.LogEntry>({ |
| 100 | + message: jasmine.stringMatching(/CommonJS or AMD dependencies/), |
| 101 | + }), |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should not show warning in JIT for templateUrl and styleUrl when using paths', async () => { |
| 106 | + await harness.modifyFile( |
| 107 | + 'tsconfig.json', content => { |
| 108 | + return content.replace(/"baseUrl": ".\/",/, ` |
| 109 | + "baseUrl": "./", |
| 110 | + "paths": { |
| 111 | + "@app/*": [ |
| 112 | + "src/app/*" |
| 113 | + ] |
| 114 | + }, |
| 115 | + `); |
| 116 | + }); |
| 117 | + |
| 118 | + await harness.modifyFile( |
| 119 | + 'src/app/app.module.ts', |
| 120 | + content => content.replace('./app.component', '@app/app.component'), |
| 121 | + ); |
| 122 | + |
| 123 | + harness.useTarget('build', { |
| 124 | + ...BASE_OPTIONS, |
| 125 | + allowedCommonJsDependencies: [], |
| 126 | + aot: false, |
| 127 | + }); |
| 128 | + |
| 129 | + const { result, logs } = await harness.executeOnce(); |
| 130 | + |
| 131 | + expect(result?.success).toBe(true); |
| 132 | + expect(logs).not.toContain( |
| 133 | + jasmine.objectContaining<logging.LogEntry>({ |
| 134 | + message: jasmine.stringMatching(/CommonJS or AMD dependencies/), |
| 135 | + }), |
| 136 | + ); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments