Skip to content

Commit 4c00059

Browse files
alexeaglealxhub
authored andcommittedJan 18, 2019
feat(compiler-cli): resolve generated Sass/Less files to .css inputs (#28166)
Users might have run the CSS Preprocessor tool *before* the Angular compiler. For example, we do it that way under Bazel. This means that the design-time reference is different from the compile-time one - the input to the Angular compiler is a plain .css file. We assume that the preprocessor does a trivial 1:1 mapping using the same basename with a different extension. PR Close #28166
1 parent 93d78c9 commit 4c00059

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed
 

‎packages/compiler-cli/src/transformers/compiler_host.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {DTS, GENERATED_FILES, isInRootDir, relativeToRootDirs} from './util';
1919

2020
const NODE_MODULES_PACKAGE_NAME = /node_modules\/((\w|-|\.)+|(@(\w|-|\.)+\/(\w|-|\.)+))/;
2121
const EXT = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/;
22+
const CSS_PREPROCESSOR_EXT = /(\.scss|\.less|\.styl)$/;
2223

2324
export function createCompilerHost(
2425
{options, tsHost = ts.createCompilerHost(options, true)}:
@@ -270,8 +271,15 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
270271
} else if (firstChar !== '.') {
271272
resourceName = `./${resourceName}`;
272273
}
273-
const filePathWithNgResource =
274+
let filePathWithNgResource =
274275
this.moduleNameToFileName(addNgResourceSuffix(resourceName), containingFile);
276+
// If the user specified styleUrl pointing to *.scss, but the Sass compiler was run before
277+
// Angular, then the resource may have been generated as *.css. Simply try the resolution again.
278+
if (!filePathWithNgResource && CSS_PREPROCESSOR_EXT.test(resourceName)) {
279+
const fallbackResourceName = resourceName.replace(CSS_PREPROCESSOR_EXT, '.css');
280+
filePathWithNgResource =
281+
this.moduleNameToFileName(addNgResourceSuffix(fallbackResourceName), containingFile);
282+
}
275283
const result = filePathWithNgResource ? stripNgResourceSuffix(filePathWithNgResource) : null;
276284
// Used under Bazel to report more specific error with remediation advice
277285
if (!result && (this.context as any).reportMissingResource) {

‎packages/compiler-cli/test/transformers/compiler_host_spec.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,21 @@ describe('NgCompilerHost', () => {
196196
const host = createHost({ngHost});
197197
expect(host.resourceNameToFileName('a', 'b')).toBe('someResult');
198198
});
199-
199+
it('should resolve Sass imports to generated .css files', () => {
200+
const host = createHost({files: {'tmp': {'src': {'a': {'style.css': 'h1: bold'}}}}});
201+
expect(host.resourceNameToFileName('./a/style.scss', '/tmp/src/index.ts'))
202+
.toBe('/tmp/src/a/style.css');
203+
});
204+
it('should resolve Less imports to generated .css files', () => {
205+
const host = createHost({files: {'tmp': {'src': {'a': {'style.css': 'h1: bold'}}}}});
206+
expect(host.resourceNameToFileName('./a/style.less', '/tmp/src/index.ts'))
207+
.toBe('/tmp/src/a/style.css');
208+
});
209+
it('should resolve Stylus imports to generated .css files', () => {
210+
const host = createHost({files: {'tmp': {'src': {'a': {'style.css': 'h1: bold'}}}}});
211+
expect(host.resourceNameToFileName('./a/style.styl', '/tmp/src/index.ts'))
212+
.toBe('/tmp/src/a/style.css');
213+
});
200214
});
201215

202216
describe('getSourceFile', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.