|
| 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: "TypeScript JSON module resolution"', () => { |
| 14 | + it('should resolve JSON files when imported with resolveJsonModule enabled', async () => { |
| 15 | + await harness.writeFiles({ |
| 16 | + 'src/x.json': `{"a": 1}`, |
| 17 | + 'src/main.ts': `import * as x from './x.json'; console.log(x);`, |
| 18 | + }); |
| 19 | + |
| 20 | + // Enable tsconfig resolveJsonModule option in tsconfig |
| 21 | + await harness.modifyFile('tsconfig.json', (content) => { |
| 22 | + const tsconfig = JSON.parse(content); |
| 23 | + tsconfig.compilerOptions.resolveJsonModule = true; |
| 24 | + |
| 25 | + return JSON.stringify(tsconfig); |
| 26 | + }); |
| 27 | + |
| 28 | + harness.useTarget('build', { |
| 29 | + ...BASE_OPTIONS, |
| 30 | + }); |
| 31 | + |
| 32 | + const { result } = await harness.executeOnce(); |
| 33 | + |
| 34 | + expect(result?.success).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should fail to resolve with TS if resolveJsonModule is not present', async () => { |
| 38 | + await harness.writeFiles({ |
| 39 | + 'src/x.json': `{"a": 1}`, |
| 40 | + 'src/main.ts': `import * as x from './x.json'; console.log(x);`, |
| 41 | + }); |
| 42 | + |
| 43 | + // Enable tsconfig resolveJsonModule option in tsconfig |
| 44 | + await harness.modifyFile('tsconfig.json', (content) => { |
| 45 | + const tsconfig = JSON.parse(content); |
| 46 | + tsconfig.compilerOptions.resolveJsonModule = undefined; |
| 47 | + |
| 48 | + return JSON.stringify(tsconfig); |
| 49 | + }); |
| 50 | + |
| 51 | + harness.useTarget('build', { |
| 52 | + ...BASE_OPTIONS, |
| 53 | + }); |
| 54 | + |
| 55 | + const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false }); |
| 56 | + |
| 57 | + expect(result?.success).toBe(false); |
| 58 | + expect(logs).toContain( |
| 59 | + jasmine.objectContaining({ |
| 60 | + message: jasmine.stringMatching(`Cannot find module './x.json'`), |
| 61 | + }), |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should fail to resolve with TS if resolveJsonModule is disabled', async () => { |
| 66 | + await harness.writeFiles({ |
| 67 | + 'src/x.json': `{"a": 1}`, |
| 68 | + 'src/main.ts': `import * as x from './x.json'; console.log(x);`, |
| 69 | + }); |
| 70 | + |
| 71 | + // Enable tsconfig resolveJsonModule option in tsconfig |
| 72 | + await harness.modifyFile('tsconfig.json', (content) => { |
| 73 | + const tsconfig = JSON.parse(content); |
| 74 | + tsconfig.compilerOptions.resolveJsonModule = false; |
| 75 | + |
| 76 | + return JSON.stringify(tsconfig); |
| 77 | + }); |
| 78 | + |
| 79 | + harness.useTarget('build', { |
| 80 | + ...BASE_OPTIONS, |
| 81 | + }); |
| 82 | + |
| 83 | + const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false }); |
| 84 | + |
| 85 | + expect(result?.success).toBe(false); |
| 86 | + expect(logs).toContain( |
| 87 | + jasmine.objectContaining({ |
| 88 | + message: jasmine.stringMatching(`Cannot find module './x.json'`), |
| 89 | + }), |
| 90 | + ); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments