|
| 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 | +// tslint:disable:no-big-function |
| 9 | + |
| 10 | +import { runTargetSpec } from '@angular-devkit/architect/testing'; |
| 11 | +import { normalize, virtualFs } from '@angular-devkit/core'; |
| 12 | +import { tap } from 'rxjs/operators'; |
| 13 | +import { browserTargetSpec, host } from '../utils'; |
| 14 | + |
| 15 | + |
| 16 | +describe('Browser Builder styles resources output path', () => { |
| 17 | + const stylesBundle = 'dist/styles.css'; |
| 18 | + const mainBundle = 'dist/main.js'; |
| 19 | + |
| 20 | + const imgSvg = ` |
| 21 | + <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> |
| 22 | + <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /> |
| 23 | + </svg> |
| 24 | + `; |
| 25 | + |
| 26 | + function writeFiles() { |
| 27 | + // Use a large image for the relative ref so it cannot be inlined. |
| 28 | + host.copyFile('src/spectrum.png', './src/assets/global-img-relative.png'); |
| 29 | + host.copyFile('src/spectrum.png', './src/assets/component-img-relative.png'); |
| 30 | + host.writeMultipleFiles({ |
| 31 | + 'src/styles.css': ` |
| 32 | + h1 { background: url('/assets/global-img-absolute.svg'); } |
| 33 | + h2 { background: url('./assets/global-img-relative.png'); } |
| 34 | + `, |
| 35 | + 'src/app/app.component.css': ` |
| 36 | + h3 { background: url('/assets/component-img-absolute.svg'); } |
| 37 | + h4 { background: url('../assets/component-img-relative.png'); } |
| 38 | + `, |
| 39 | + 'src/assets/global-img-absolute.svg': imgSvg, |
| 40 | + 'src/assets/component-img-absolute.svg': imgSvg, |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + beforeEach(done => host.initialize().toPromise().then(done, done.fail)); |
| 45 | + afterEach(done => host.restore().toPromise().then(done, done.fail)); |
| 46 | + |
| 47 | + it(`supports resourcesOutputPath in resource urls`, (done) => { |
| 48 | + writeFiles(); |
| 49 | + // Check base paths are correctly generated. |
| 50 | + const overrides = { |
| 51 | + aot: true, |
| 52 | + extractCss: true, |
| 53 | + resourcesOutputPath: 'out-assets', |
| 54 | + }; |
| 55 | + |
| 56 | + runTargetSpec(host, browserTargetSpec, overrides, undefined, undefined).pipe( |
| 57 | + tap(() => { |
| 58 | + const styles = virtualFs.fileBufferToString( |
| 59 | + host.scopedSync().read(normalize(stylesBundle)), |
| 60 | + ); |
| 61 | + |
| 62 | + const main = virtualFs.fileBufferToString(host.scopedSync().read(normalize(mainBundle))); |
| 63 | + expect(styles).toContain(`url('/assets/global-img-absolute.svg')`); |
| 64 | + expect(styles).toContain(`url('out-assets/global-img-relative.png')`); |
| 65 | + expect(main).toContain(`url('/assets/component-img-absolute.svg')`); |
| 66 | + expect(main).toContain(`url('out-assets/component-img-relative.png')`); |
| 67 | + |
| 68 | + expect(host.scopedSync() |
| 69 | + .exists(normalize('dist/assets/global-img-absolute.svg'))).toBe(true); |
| 70 | + expect(host.scopedSync() |
| 71 | + .exists(normalize('dist/out-assets/global-img-relative.png'))).toBe(true); |
| 72 | + expect(host.scopedSync() |
| 73 | + .exists(normalize('dist/assets/component-img-absolute.svg'))).toBe(true); |
| 74 | + expect(host.scopedSync() |
| 75 | + .exists(normalize('dist/out-assets/component-img-relative.png'))).toBe(true); |
| 76 | + }), |
| 77 | + ).toPromise().then(done, done.fail); |
| 78 | + }); |
| 79 | + |
| 80 | + it(`supports blank resourcesOutputPath`, (done) => { |
| 81 | + writeFiles(); |
| 82 | + |
| 83 | + // Check base paths are correctly generated. |
| 84 | + const overrides = { aot: true, extractCss: true }; |
| 85 | + runTargetSpec(host, browserTargetSpec, overrides, undefined, undefined).pipe( |
| 86 | + tap(() => { |
| 87 | + const styles = virtualFs.fileBufferToString( |
| 88 | + host.scopedSync().read(normalize(stylesBundle)), |
| 89 | + ); |
| 90 | + |
| 91 | + const main = virtualFs.fileBufferToString(host.scopedSync().read(normalize(mainBundle))); |
| 92 | + expect(styles).toContain(`url('/assets/global-img-absolute.svg')`); |
| 93 | + expect(styles).toContain(`url('global-img-relative.png')`); |
| 94 | + expect(main).toContain(`url('/assets/component-img-absolute.svg')`); |
| 95 | + expect(main).toContain(`url('component-img-relative.png')`); |
| 96 | + expect(host.scopedSync().exists(normalize('dist/assets/global-img-absolute.svg'))) |
| 97 | + .toBe(true); |
| 98 | + expect(host.scopedSync().exists(normalize('dist/global-img-relative.png'))) |
| 99 | + .toBe(true); |
| 100 | + expect(host.scopedSync().exists(normalize('dist/assets/component-img-absolute.svg'))) |
| 101 | + .toBe(true); |
| 102 | + expect(host.scopedSync().exists(normalize('dist/component-img-relative.png'))) |
| 103 | + .toBe(true); |
| 104 | + }), |
| 105 | + ).toPromise().then(done, done.fail); |
| 106 | + }); |
| 107 | + |
| 108 | +}); |
0 commit comments