fix(ivy): correct absolute path processing for templateUrl and styleUrls (#28789)

Prior to this change absolute file paths (like `/a/b/c/style.css`) were calculated taking current component file location into account. As a result, absolute file paths were calculated using current file as a root. This change updates this logic to ignore current file path in case of absolute paths.

PR Close #28789
This commit is contained in:
Andrew Kushnir 2019-02-17 16:25:45 -08:00 committed by Igor Minar
parent 72d043f669
commit df627e65df
2 changed files with 72 additions and 0 deletions

View File

@ -115,6 +115,9 @@ export class HostResourceLoader implements ResourceLoader {
// Strip a leading '/' if one is present.
if (url.startsWith('/')) {
url = url.substr(1);
// Do not take current file location into account if we process absolute path.
fromFile = '';
}
// Turn absolute paths into relative paths.
if (!url.startsWith('.')) {

View File

@ -826,6 +826,75 @@ describe('ngtsc behavioral tests', () => {
});
});
describe('templateUrl and styleUrls processing', () => {
const testsForResource = (resource: string) => [
// [component location, resource location, resource reference]
// component and resource are in the same folder
[`a/app.ts`, `a/${resource}`, `./${resource}`], //
[`a/app.ts`, `a/${resource}`, resource], //
[`a/app.ts`, `a/${resource}`, `/a/${resource}`],
// resource is one level up
[`a/app.ts`, resource, `../${resource}`], //
[`a/app.ts`, resource, `/${resource}`],
// component and resource are in different folders
[`a/app.ts`, `b/${resource}`, `../b/${resource}`], //
[`a/app.ts`, `b/${resource}`, `/b/${resource}`],
// resource is in subfolder of component directory
[`a/app.ts`, `a/b/c/${resource}`, `./b/c/${resource}`], //
[`a/app.ts`, `a/b/c/${resource}`, `b/c/${resource}`], //
[`a/app.ts`, `a/b/c/${resource}`, `/a/b/c/${resource}`],
];
testsForResource('style.css').forEach((test) => {
const [compLoc, styleLoc, styleRef] = test;
it(`should handle ${styleRef}`, () => {
env.tsconfig();
env.write(styleLoc, ':host { background-color: blue; }');
env.write(compLoc, `
import {Component} from '@angular/core';
@Component({
selector: 'test-cmp',
styleUrls: ['${styleRef}'],
template: '...',
})
export class TestCmp {}
`);
env.driveMain();
const jsContents = env.getContents(compLoc.replace('.ts', '.js'));
expect(jsContents).toContain('background-color: blue');
});
});
testsForResource('template.html').forEach((test) => {
const [compLoc, templateLoc, templateRef] = test;
it(`should handle ${templateRef}`, () => {
env.tsconfig();
env.write(templateLoc, 'Template Content');
env.write(compLoc, `
import {Component} from '@angular/core';
@Component({
selector: 'test-cmp',
templateUrl: '${templateRef}'
})
export class TestCmp {}
`);
env.driveMain();
const jsContents = env.getContents(compLoc.replace('.ts', '.js'));
expect(jsContents).toContain('Template Content');
});
});
});
describe('former View Engine AST transform bugs', () => {
it('should compile array literals behind conditionals', () => {
env.tsconfig();