|
| 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.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { buildApplication } from '../../index'; |
| 10 | +import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; |
| 11 | + |
| 12 | +describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { |
| 13 | + describe('Behavior: "loader import attribute"', () => { |
| 14 | + beforeEach(async () => { |
| 15 | + await harness.modifyFile('tsconfig.json', (content) => { |
| 16 | + return content.replace('"module": "ES2022"', '"module": "esnext"'); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should inline text content for loader attribute set to "text"', async () => { |
| 21 | + harness.useTarget('build', { |
| 22 | + ...BASE_OPTIONS, |
| 23 | + }); |
| 24 | + |
| 25 | + await harness.writeFile('./src/a.unknown', 'ABC'); |
| 26 | + await harness.writeFile( |
| 27 | + 'src/main.ts', |
| 28 | + '// @ts-expect-error\nimport contents from "./a.unknown" with { loader: "text" };\n console.log(contents);', |
| 29 | + ); |
| 30 | + |
| 31 | + const { result } = await harness.executeOnce(); |
| 32 | + expect(result?.success).toBe(true); |
| 33 | + harness.expectFile('dist/browser/main.js').content.toContain('ABC'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should inline binary content for loader attribute set to "binary"', async () => { |
| 37 | + harness.useTarget('build', { |
| 38 | + ...BASE_OPTIONS, |
| 39 | + }); |
| 40 | + |
| 41 | + await harness.writeFile('./src/a.unknown', 'ABC'); |
| 42 | + await harness.writeFile( |
| 43 | + 'src/main.ts', |
| 44 | + '// @ts-expect-error\nimport contents from "./a.unknown" with { loader: "binary" };\n console.log(contents);', |
| 45 | + ); |
| 46 | + |
| 47 | + const { result } = await harness.executeOnce(); |
| 48 | + expect(result?.success).toBe(true); |
| 49 | + // Should contain the binary encoding used esbuild and not the text content |
| 50 | + harness.expectFile('dist/browser/main.js').content.toContain('__toBinary("QUJD")'); |
| 51 | + harness.expectFile('dist/browser/main.js').content.not.toContain('ABC'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should emit an output file for loader attribute set to "file"', async () => { |
| 55 | + harness.useTarget('build', { |
| 56 | + ...BASE_OPTIONS, |
| 57 | + }); |
| 58 | + |
| 59 | + await harness.writeFile('./src/a.unknown', 'ABC'); |
| 60 | + await harness.writeFile( |
| 61 | + 'src/main.ts', |
| 62 | + '// @ts-expect-error\nimport contents from "./a.unknown" with { loader: "file" };\n console.log(contents);', |
| 63 | + ); |
| 64 | + |
| 65 | + const { result } = await harness.executeOnce(); |
| 66 | + expect(result?.success).toBe(true); |
| 67 | + harness.expectFile('dist/browser/main.js').content.toContain('a.unknown'); |
| 68 | + harness.expectFile('dist/browser/media/a.unknown').toExist(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should emit an output file with hashing when enabled for loader attribute set to "file"', async () => { |
| 72 | + harness.useTarget('build', { |
| 73 | + ...BASE_OPTIONS, |
| 74 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 75 | + outputHashing: 'media' as any, |
| 76 | + }); |
| 77 | + |
| 78 | + await harness.writeFile('./src/a.unknown', 'ABC'); |
| 79 | + await harness.writeFile( |
| 80 | + 'src/main.ts', |
| 81 | + '// @ts-expect-error\nimport contents from "./a.unknown" with { loader: "file" };\n console.log(contents);', |
| 82 | + ); |
| 83 | + |
| 84 | + const { result } = await harness.executeOnce(); |
| 85 | + expect(result?.success).toBe(true); |
| 86 | + harness.expectFile('dist/browser/main.js').content.toContain('a.unknown'); |
| 87 | + expect(harness.hasFileMatch('dist/browser/media', /a-[0-9A-Z]{8}\.unknown$/)).toBeTrue(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should allow overriding default `.txt` extension behavior', async () => { |
| 91 | + harness.useTarget('build', { |
| 92 | + ...BASE_OPTIONS, |
| 93 | + }); |
| 94 | + |
| 95 | + await harness.writeFile('./src/a.txt', 'ABC'); |
| 96 | + await harness.writeFile( |
| 97 | + 'src/main.ts', |
| 98 | + '// @ts-expect-error\nimport contents from "./a.txt" with { loader: "file" };\n console.log(contents);', |
| 99 | + ); |
| 100 | + |
| 101 | + const { result } = await harness.executeOnce(); |
| 102 | + expect(result?.success).toBe(true); |
| 103 | + harness.expectFile('dist/browser/main.js').content.toContain('a.txt'); |
| 104 | + harness.expectFile('dist/browser/media/a.txt').toExist(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should allow overriding default `.js` extension behavior', async () => { |
| 108 | + harness.useTarget('build', { |
| 109 | + ...BASE_OPTIONS, |
| 110 | + }); |
| 111 | + |
| 112 | + await harness.writeFile('./src/a.js', 'ABC'); |
| 113 | + await harness.writeFile( |
| 114 | + 'src/main.ts', |
| 115 | + '// @ts-expect-error\nimport contents from "./a.js" with { loader: "file" };\n console.log(contents);', |
| 116 | + ); |
| 117 | + |
| 118 | + const { result } = await harness.executeOnce(); |
| 119 | + expect(result?.success).toBe(true); |
| 120 | + harness.expectFile('dist/browser/main.js').content.toContain('a.js'); |
| 121 | + harness.expectFile('dist/browser/media/a.js').toExist(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should fail with an error if an invalid loader attribute value is used', async () => { |
| 125 | + harness.useTarget('build', { |
| 126 | + ...BASE_OPTIONS, |
| 127 | + }); |
| 128 | + |
| 129 | + harness.useTarget('build', { |
| 130 | + ...BASE_OPTIONS, |
| 131 | + }); |
| 132 | + |
| 133 | + await harness.writeFile('./src/a.unknown', 'ABC'); |
| 134 | + await harness.writeFile( |
| 135 | + 'src/main.ts', |
| 136 | + '// @ts-expect-error\nimport contents from "./a.unknown" with { loader: "invalid" };\n console.log(contents);', |
| 137 | + ); |
| 138 | + |
| 139 | + const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false }); |
| 140 | + expect(result?.success).toBe(false); |
| 141 | + expect(logs).toContain( |
| 142 | + jasmine.objectContaining({ |
| 143 | + message: jasmine.stringMatching('Unsupported loader import attribute'), |
| 144 | + }), |
| 145 | + ); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments