|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import { rm } from 'node:fs/promises'; |
| 10 | +import { ParallelCompilation } from '../../../../tools/angular/compilation/parallel-compilation'; |
10 | 11 | import { executeLibraryBuilder } from '../../builder'; |
11 | 12 | import { CompilationMode, InlineStyleLanguage } from '../../schema'; |
12 | 13 | import { BASE_OPTIONS, LIBRARY_BUILDER_INFO, describeLibraryBuilder } from '../setup'; |
@@ -847,5 +848,176 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) => |
847 | 848 | }, |
848 | 849 | ]); |
849 | 850 | }); |
| 851 | + |
| 852 | + it('should update .mjs bundle without invalidating .ts or re-emitting .d.ts when external Sass or partial is modified', async () => { |
| 853 | + await harness.writeFiles({ |
| 854 | + 'projects/lib/src/lib/_vars.scss': '$primary-color: rgb(10, 20, 30);', |
| 855 | + 'projects/lib/src/lib/lib.component.scss': |
| 856 | + '@use "./vars"; :host { color: vars.$primary-color; padding: 4px; }', |
| 857 | + 'projects/lib/src/lib/lib.component.ts': ` |
| 858 | + import { Component } from '@angular/core'; |
| 859 | +
|
| 860 | + @Component({ |
| 861 | + selector: 'lib-external-sass', |
| 862 | + template: '<span>External Sass</span>', |
| 863 | + styleUrl: './lib.component.scss', |
| 864 | + }) |
| 865 | + export class LibComponent {} |
| 866 | + `, |
| 867 | + }); |
| 868 | + |
| 869 | + const updateSpy = spyOn(ParallelCompilation.prototype, 'update').and.callThrough(); |
| 870 | + const emitSpy = spyOn(ParallelCompilation.prototype, 'emitAffectedFiles').and.callThrough(); |
| 871 | + |
| 872 | + harness.useTarget('build', { |
| 873 | + ...BASE_OPTIONS, |
| 874 | + watch: true, |
| 875 | + }); |
| 876 | + |
| 877 | + await harness.executeWithCases([ |
| 878 | + async ({ result }) => { |
| 879 | + expect(result?.success).toBeTrue(); |
| 880 | + const content = harness.readFile('dist/lib/fesm2022/lib.mjs'); |
| 881 | + expect(content).toMatch(/color:\s*(?:#0a141e|rgb\(10,\s*20,\s*30\))/); |
| 882 | + expect(content).toMatch(/padding:\s*4px/); |
| 883 | + |
| 884 | + updateSpy.calls.reset(); |
| 885 | + emitSpy.calls.reset(); |
| 886 | + |
| 887 | + // Modify the external component stylesheet directly |
| 888 | + await harness.writeFile( |
| 889 | + 'projects/lib/src/lib/lib.component.scss', |
| 890 | + '@use "./vars"; :host { color: vars.$primary-color; padding: 16px; }', |
| 891 | + ); |
| 892 | + }, |
| 893 | + async ({ result }) => { |
| 894 | + expect(result?.success).toBeTrue(); |
| 895 | + const content = harness.readFile('dist/lib/fesm2022/lib.mjs'); |
| 896 | + expect(content).toMatch(/color:\s*(?:#0a141e|rgb\(10,\s*20,\s*30\))/); |
| 897 | + expect(content).toMatch(/padding:\s*16px/); |
| 898 | + |
| 899 | + expect(updateSpy).toHaveBeenCalledTimes(1); |
| 900 | + const updatedFilesAfterScss = [...(updateSpy.calls.mostRecent().args[0] ?? [])]; |
| 901 | + expect(updatedFilesAfterScss.some((f) => f.endsWith('.ts'))).toBeFalse(); |
| 902 | + |
| 903 | + expect(emitSpy).toHaveBeenCalledTimes(1); |
| 904 | + const emittedAfterScss = [...(await emitSpy.calls.mostRecent().returnValue)]; |
| 905 | + expect(emittedAfterScss.some((f) => f.filename.endsWith('lib.component.js'))).toBeTrue(); |
| 906 | + expect(emittedAfterScss.some((f) => f.filename.endsWith('.d.ts'))).toBeFalse(); |
| 907 | + |
| 908 | + updateSpy.calls.reset(); |
| 909 | + emitSpy.calls.reset(); |
| 910 | + |
| 911 | + // Modify the imported Sass partial |
| 912 | + await harness.writeFile( |
| 913 | + 'projects/lib/src/lib/_vars.scss', |
| 914 | + '$primary-color: rgb(200, 100, 50);', |
| 915 | + ); |
| 916 | + }, |
| 917 | + async ({ result }) => { |
| 918 | + expect(result?.success).toBeTrue(); |
| 919 | + const content = harness.readFile('dist/lib/fesm2022/lib.mjs'); |
| 920 | + expect(content).toMatch(/color:\s*(?:#c86432|rgb\(200,\s*100,\s*50\))/); |
| 921 | + expect(content).toMatch(/padding:\s*16px/); |
| 922 | + |
| 923 | + expect(updateSpy).toHaveBeenCalledTimes(1); |
| 924 | + const updatedFilesAfterPartial = [...(updateSpy.calls.mostRecent().args[0] ?? [])]; |
| 925 | + expect(updatedFilesAfterPartial.some((f) => f.endsWith('.ts'))).toBeFalse(); |
| 926 | + expect(updatedFilesAfterPartial.some((f) => f.endsWith('lib.component.scss'))).toBeTrue(); |
| 927 | + |
| 928 | + expect(emitSpy).toHaveBeenCalledTimes(1); |
| 929 | + const emittedAfterPartial = [...(await emitSpy.calls.mostRecent().returnValue)]; |
| 930 | + expect( |
| 931 | + emittedAfterPartial.some((f) => f.filename.endsWith('lib.component.js')), |
| 932 | + ).toBeTrue(); |
| 933 | + expect(emittedAfterPartial.some((f) => f.filename.endsWith('.d.ts'))).toBeFalse(); |
| 934 | + }, |
| 935 | + ]); |
| 936 | + }); |
| 937 | + |
| 938 | + it('should update .mjs bundle and report template errors without re-emitting .d.ts when external template is modified', async () => { |
| 939 | + await harness.writeFiles({ |
| 940 | + 'projects/lib/src/lib/lib.component.html': '<h1>{{ title }}</h1>', |
| 941 | + 'projects/lib/src/lib/lib.component.ts': ` |
| 942 | + import { Component } from '@angular/core'; |
| 943 | +
|
| 944 | + @Component({ |
| 945 | + selector: 'lib-external-template', |
| 946 | + templateUrl: './lib.component.html', |
| 947 | + }) |
| 948 | + export class LibComponent { |
| 949 | + title = 'Hello'; |
| 950 | + } |
| 951 | + `, |
| 952 | + }); |
| 953 | + |
| 954 | + const emitSpy = spyOn(ParallelCompilation.prototype, 'emitAffectedFiles').and.callThrough(); |
| 955 | + |
| 956 | + harness.useTarget('build', { |
| 957 | + ...BASE_OPTIONS, |
| 958 | + watch: true, |
| 959 | + }); |
| 960 | + |
| 961 | + await harness.executeWithCases([ |
| 962 | + async ({ result }) => { |
| 963 | + expect(result?.success).toBeTrue(); |
| 964 | + expect(harness.readFile('dist/lib/fesm2022/lib.mjs')).toContain('h1'); |
| 965 | + |
| 966 | + emitSpy.calls.reset(); |
| 967 | + |
| 968 | + // Modify external HTML template with valid markup |
| 969 | + await harness.writeFile( |
| 970 | + 'projects/lib/src/lib/lib.component.html', |
| 971 | + '<section>{{ title }}</section>', |
| 972 | + ); |
| 973 | + }, |
| 974 | + async ({ result }) => { |
| 975 | + expect(result?.success).toBeTrue(); |
| 976 | + expect(harness.readFile('dist/lib/fesm2022/lib.mjs')).toContain('section'); |
| 977 | + |
| 978 | + expect(emitSpy).toHaveBeenCalledTimes(1); |
| 979 | + const emittedAfterHtml = [...(await emitSpy.calls.mostRecent().returnValue)]; |
| 980 | + expect(emittedAfterHtml.some((f) => f.filename.endsWith('lib.component.js'))).toBeTrue(); |
| 981 | + expect(emittedAfterHtml.some((f) => f.filename.endsWith('.d.ts'))).toBeFalse(); |
| 982 | + |
| 983 | + emitSpy.calls.reset(); |
| 984 | + |
| 985 | + // Introduce a template type-checking error in the external HTML template |
| 986 | + await harness.writeFile( |
| 987 | + 'projects/lib/src/lib/lib.component.html', |
| 988 | + '<section>{{ nonExistentProp }}</section>', |
| 989 | + ); |
| 990 | + }, |
| 991 | + async ({ result, logs }) => { |
| 992 | + expect(result?.success).toBeFalse(); |
| 993 | + expect(logs).toContain( |
| 994 | + jasmine.objectContaining({ |
| 995 | + message: jasmine.stringContaining('nonExistentProp'), |
| 996 | + }), |
| 997 | + ); |
| 998 | + |
| 999 | + expect(emitSpy).toHaveBeenCalledTimes(1); |
| 1000 | + const emittedOnError = [...(await emitSpy.calls.mostRecent().returnValue)]; |
| 1001 | + expect(emittedOnError.some((f) => f.filename.endsWith('.d.ts'))).toBeFalse(); |
| 1002 | + |
| 1003 | + emitSpy.calls.reset(); |
| 1004 | + |
| 1005 | + // Fix the external HTML template error |
| 1006 | + await harness.writeFile( |
| 1007 | + 'projects/lib/src/lib/lib.component.html', |
| 1008 | + '<footer>{{ title }}</footer>', |
| 1009 | + ); |
| 1010 | + }, |
| 1011 | + async ({ result }) => { |
| 1012 | + expect(result?.success).toBeTrue(); |
| 1013 | + expect(harness.readFile('dist/lib/fesm2022/lib.mjs')).toContain('footer'); |
| 1014 | + |
| 1015 | + expect(emitSpy).toHaveBeenCalledTimes(1); |
| 1016 | + const emittedOnRecovery = [...(await emitSpy.calls.mostRecent().returnValue)]; |
| 1017 | + expect(emittedOnRecovery.some((f) => f.filename.endsWith('lib.component.js'))).toBeTrue(); |
| 1018 | + expect(emittedOnRecovery.some((f) => f.filename.endsWith('.d.ts'))).toBeFalse(); |
| 1019 | + }, |
| 1020 | + ]); |
| 1021 | + }); |
850 | 1022 | }); |
851 | 1023 | }); |
0 commit comments