|
| 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 | + |
| 9 | + |
| 10 | +import { buildWebpackBrowser } from '../../index'; |
| 11 | +import { OutputHashing } from '../../schema'; |
| 12 | +import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup'; |
| 13 | + |
| 14 | +describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => { |
| 15 | + describe('Option: "outputHashing"', () => { |
| 16 | + beforeEach(async () => { |
| 17 | + // Application code is not needed for asset tests |
| 18 | + await harness.writeFile('src/main.ts', ''); |
| 19 | + }); |
| 20 | + |
| 21 | + it('hashes all filenames when set to "all"', async () => { |
| 22 | + await harness.writeFile( |
| 23 | + 'src/styles.css', |
| 24 | + `h1 { background: url('./spectrum.png')}`, |
| 25 | + ); |
| 26 | + |
| 27 | + harness.useTarget('build', { |
| 28 | + ...BASE_OPTIONS, |
| 29 | + styles: ['src/styles.css'], |
| 30 | + outputHashing: OutputHashing.All, |
| 31 | + }); |
| 32 | + |
| 33 | + const { result } = await harness.executeOnce(); |
| 34 | + expect(result?.success).toBe(true); |
| 35 | + |
| 36 | + expect(harness.hasFileMatch('dist', /runtime\.[0-9a-f]{20}\.js$/)).toBeTrue(); |
| 37 | + expect(harness.hasFileMatch('dist', /main\.[0-9a-f]{20}\.js$/)).toBeTrue(); |
| 38 | + expect(harness.hasFileMatch('dist', /polyfills\.[0-9a-f]{20}\.js$/)).toBeTrue(); |
| 39 | + expect(harness.hasFileMatch('dist', /styles\.[0-9a-f]{20}\.css$/)).toBeTrue(); |
| 40 | + expect(harness.hasFileMatch('dist', /spectrum\.[0-9a-f]{20}\.png$/)).toBeTrue(); |
| 41 | + }); |
| 42 | + |
| 43 | + it(`doesn't hash any filenames when not set`, async () => { |
| 44 | + await harness.writeFile( |
| 45 | + 'src/styles.css', |
| 46 | + `h1 { background: url('./spectrum.png')}`, |
| 47 | + ); |
| 48 | + |
| 49 | + harness.useTarget('build', { |
| 50 | + ...BASE_OPTIONS, |
| 51 | + styles: ['src/styles.css'], |
| 52 | + }); |
| 53 | + |
| 54 | + const { result } = await harness.executeOnce(); |
| 55 | + expect(result?.success).toBe(true); |
| 56 | + |
| 57 | + expect(harness.hasFileMatch('dist', /runtime\.[0-9a-f]{20}\.js$/)).toBeFalse(); |
| 58 | + expect(harness.hasFileMatch('dist', /main\.[0-9a-f]{20}\.js$/)).toBeFalse(); |
| 59 | + expect(harness.hasFileMatch('dist', /polyfills\.[0-9a-f]{20}\.js$/)).toBeFalse(); |
| 60 | + expect(harness.hasFileMatch('dist', /styles\.[0-9a-f]{20}\.css$/)).toBeFalse(); |
| 61 | + expect(harness.hasFileMatch('dist', /spectrum\.[0-9a-f]{20}\.png$/)).toBeFalse(); |
| 62 | + }); |
| 63 | + |
| 64 | + it(`doesn't hash any filenames when set to "none"`, async () => { |
| 65 | + await harness.writeFile( |
| 66 | + 'src/styles.css', |
| 67 | + `h1 { background: url('./spectrum.png')}`, |
| 68 | + ); |
| 69 | + |
| 70 | + harness.useTarget('build', { |
| 71 | + ...BASE_OPTIONS, |
| 72 | + styles: ['src/styles.css'], |
| 73 | + outputHashing: OutputHashing.None, |
| 74 | + }); |
| 75 | + |
| 76 | + const { result } = await harness.executeOnce(); |
| 77 | + expect(result?.success).toBe(true); |
| 78 | + |
| 79 | + expect(harness.hasFileMatch('dist', /runtime\.[0-9a-f]{20}\.js$/)).toBeFalse(); |
| 80 | + expect(harness.hasFileMatch('dist', /main\.[0-9a-f]{20}\.js$/)).toBeFalse(); |
| 81 | + expect(harness.hasFileMatch('dist', /polyfills\.[0-9a-f]{20}\.js$/)).toBeFalse(); |
| 82 | + expect(harness.hasFileMatch('dist', /styles\.[0-9a-f]{20}\.css$/)).toBeFalse(); |
| 83 | + expect(harness.hasFileMatch('dist', /spectrum\.[0-9a-f]{20}\.png$/)).toBeFalse(); |
| 84 | + }); |
| 85 | + |
| 86 | + it(`hashes CSS resources filenames only when set to "media"`, async () => { |
| 87 | + await harness.writeFile( |
| 88 | + 'src/styles.css', |
| 89 | + `h1 { background: url('./spectrum.png')}`, |
| 90 | + ); |
| 91 | + |
| 92 | + harness.useTarget('build', { |
| 93 | + ...BASE_OPTIONS, |
| 94 | + styles: ['src/styles.css'], |
| 95 | + outputHashing: OutputHashing.Media, |
| 96 | + }); |
| 97 | + |
| 98 | + const { result } = await harness.executeOnce(); |
| 99 | + expect(result?.success).toBe(true); |
| 100 | + |
| 101 | + expect(harness.hasFileMatch('dist', /runtime\.[0-9a-f]{20}\.js$/)).toBeFalse(); |
| 102 | + expect(harness.hasFileMatch('dist', /main\.[0-9a-f]{20}\.js$/)).toBeFalse(); |
| 103 | + expect(harness.hasFileMatch('dist', /polyfills\.[0-9a-f]{20}\.js$/)).toBeFalse(); |
| 104 | + expect(harness.hasFileMatch('dist', /styles\.[0-9a-f]{20}\.css$/)).toBeFalse(); |
| 105 | + expect(harness.hasFileMatch('dist', /spectrum\.[0-9a-f]{20}\.png$/)).toBeTrue(); |
| 106 | + }); |
| 107 | + |
| 108 | + it(`hashes bundles filenames only when set to "bundles"`, async () => { |
| 109 | + await harness.writeFile( |
| 110 | + 'src/styles.css', |
| 111 | + `h1 { background: url('./spectrum.png')}`, |
| 112 | + ); |
| 113 | + |
| 114 | + harness.useTarget('build', { |
| 115 | + ...BASE_OPTIONS, |
| 116 | + styles: ['src/styles.css'], |
| 117 | + outputHashing: OutputHashing.Bundles, |
| 118 | + }); |
| 119 | + |
| 120 | + const { result } = await harness.executeOnce(); |
| 121 | + expect(result?.success).toBe(true); |
| 122 | + |
| 123 | + expect(harness.hasFileMatch('dist', /runtime\.[0-9a-f]{20}\.js$/)).toBeTrue(); |
| 124 | + expect(harness.hasFileMatch('dist', /main\.[0-9a-f]{20}\.js$/)).toBeTrue(); |
| 125 | + expect(harness.hasFileMatch('dist', /polyfills\.[0-9a-f]{20}\.js$/)).toBeTrue(); |
| 126 | + expect(harness.hasFileMatch('dist', /styles\.[0-9a-f]{20}\.css$/)).toBeTrue(); |
| 127 | + expect(harness.hasFileMatch('dist', /spectrum\.[0-9a-f]{20}\.png$/)).toBeFalse(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('does not hash non injected styles', async () => { |
| 131 | + harness.useTarget('build', { |
| 132 | + ...BASE_OPTIONS, |
| 133 | + outputHashing: OutputHashing.All, |
| 134 | + styles: [{ |
| 135 | + input: 'src/styles.css', |
| 136 | + inject: false, |
| 137 | + }], |
| 138 | + }); |
| 139 | + |
| 140 | + const { result } = await harness.executeOnce(); |
| 141 | + expect(result?.success).toBe(true); |
| 142 | + |
| 143 | + expect(harness.hasFileMatch('dist', /styles\.[0-9a-f]{20}\.js$/)).toBeFalse(); |
| 144 | + expect(harness.hasFileMatch('dist', /styles\.[0-9a-f]{20}\.js.map$/)).toBeFalse(); |
| 145 | + harness.expectFile('dist/styles.css').toExist(); |
| 146 | + harness.expectFile('dist/styles.css.map').toExist(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('does not override different files which has the same filenames when hashing is "none"', async () => { |
| 150 | + await harness.writeFiles({ |
| 151 | + 'src/styles.css': ` |
| 152 | + h1 { background: url('./test.svg')} |
| 153 | + h2 { background: url('./small/test.svg')} |
| 154 | + `, |
| 155 | + './src/test.svg': `<svg xmlns="http://www.w3.org/2000/svg"> |
| 156 | + <text x="20" y="20" font-size="20" fill="red">Hello World</text> |
| 157 | + </svg>`, |
| 158 | + './src/small/test.svg': `<svg xmlns="http://www.w3.org/2000/svg"> |
| 159 | + <text x="10" y="10" font-size="10" fill="red">Hello World</text> |
| 160 | + </svg>`, |
| 161 | + }); |
| 162 | + |
| 163 | + harness.useTarget('build', { |
| 164 | + ...BASE_OPTIONS, |
| 165 | + styles: ['src/styles.css'], |
| 166 | + outputHashing: OutputHashing.None, |
| 167 | + }); |
| 168 | + |
| 169 | + const { result } = await harness.executeOnce(); |
| 170 | + expect(result?.success).toBe(true); |
| 171 | + |
| 172 | + harness.expectFile('dist/test.svg').toExist(); |
| 173 | + harness.expectFile('dist/small-test.svg').toExist(); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments