diff --git a/packages/compiler-cli/src/transformers/compiler_host.ts b/packages/compiler-cli/src/transformers/compiler_host.ts index bf5f4e8e642bb..284c96db55c1f 100644 --- a/packages/compiler-cli/src/transformers/compiler_host.ts +++ b/packages/compiler-cli/src/transformers/compiler_host.ts @@ -19,6 +19,7 @@ import {DTS, GENERATED_FILES, isInRootDir, relativeToRootDirs} from './util'; const NODE_MODULES_PACKAGE_NAME = /node_modules\/((\w|-|\.)+|(@(\w|-|\.)+\/(\w|-|\.)+))/; const EXT = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/; +const CSS_PREPROCESSOR_EXT = /(\.scss|\.less|\.styl)$/; export function createCompilerHost( {options, tsHost = ts.createCompilerHost(options, true)}: @@ -270,8 +271,15 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos } else if (firstChar !== '.') { resourceName = `./${resourceName}`; } - const filePathWithNgResource = + let filePathWithNgResource = this.moduleNameToFileName(addNgResourceSuffix(resourceName), containingFile); + // If the user specified styleUrl pointing to *.scss, but the Sass compiler was run before + // Angular, then the resource may have been generated as *.css. Simply try the resolution again. + if (!filePathWithNgResource && CSS_PREPROCESSOR_EXT.test(resourceName)) { + const fallbackResourceName = resourceName.replace(CSS_PREPROCESSOR_EXT, '.css'); + filePathWithNgResource = + this.moduleNameToFileName(addNgResourceSuffix(fallbackResourceName), containingFile); + } const result = filePathWithNgResource ? stripNgResourceSuffix(filePathWithNgResource) : null; // Used under Bazel to report more specific error with remediation advice if (!result && (this.context as any).reportMissingResource) { diff --git a/packages/compiler-cli/test/transformers/compiler_host_spec.ts b/packages/compiler-cli/test/transformers/compiler_host_spec.ts index 357bdc543f362..c411a638ac6ef 100644 --- a/packages/compiler-cli/test/transformers/compiler_host_spec.ts +++ b/packages/compiler-cli/test/transformers/compiler_host_spec.ts @@ -196,7 +196,21 @@ describe('NgCompilerHost', () => { const host = createHost({ngHost}); expect(host.resourceNameToFileName('a', 'b')).toBe('someResult'); }); - + it('should resolve Sass imports to generated .css files', () => { + const host = createHost({files: {'tmp': {'src': {'a': {'style.css': 'h1: bold'}}}}}); + expect(host.resourceNameToFileName('./a/style.scss', '/tmp/src/index.ts')) + .toBe('/tmp/src/a/style.css'); + }); + it('should resolve Less imports to generated .css files', () => { + const host = createHost({files: {'tmp': {'src': {'a': {'style.css': 'h1: bold'}}}}}); + expect(host.resourceNameToFileName('./a/style.less', '/tmp/src/index.ts')) + .toBe('/tmp/src/a/style.css'); + }); + it('should resolve Stylus imports to generated .css files', () => { + const host = createHost({files: {'tmp': {'src': {'a': {'style.css': 'h1: bold'}}}}}); + expect(host.resourceNameToFileName('./a/style.styl', '/tmp/src/index.ts')) + .toBe('/tmp/src/a/style.css'); + }); }); describe('getSourceFile', () => {