@@ -15,6 +15,7 @@ import {TemplateParser} from './template_parser';
1515import { DirectiveNormalizer } from './directive_normalizer' ;
1616import { OutputEmitter } from './output/abstract_emitter' ;
1717import * as o from './output/output_ast' ;
18+ import { XHR } from './xhr' ;
1819
1920import {
2021 MODULE_SUFFIX ,
@@ -31,6 +32,10 @@ export class SourceModule {
3132 constructor ( public moduleUrl : string , public source : string ) { }
3233}
3334
35+ export class StyleSheetSourceWithImports {
36+ constructor ( public source : SourceModule , public importedUrls : string [ ] ) { }
37+ }
38+
3439export class NormalizedComponentWithViewDirectives {
3540 constructor ( public component : CompileDirectiveMetadata ,
3641 public directives : CompileDirectiveMetadata [ ] , public pipes : CompilePipeMetadata [ ] ) { }
@@ -39,7 +44,8 @@ export class NormalizedComponentWithViewDirectives {
3944export class OfflineCompiler {
4045 constructor ( private _directiveNormalizer : DirectiveNormalizer ,
4146 private _templateParser : TemplateParser , private _styleCompiler : StyleCompiler ,
42- private _viewCompiler : ViewCompiler , private _outputEmitter : OutputEmitter ) { }
47+ private _viewCompiler : ViewCompiler , private _outputEmitter : OutputEmitter ,
48+ private _xhr : XHR ) { }
4349
4450 normalizeDirectiveMetadata ( directive : CompileDirectiveMetadata ) :
4551 Promise < CompileDirectiveMetadata > {
@@ -80,15 +86,19 @@ export class OfflineCompiler {
8086 return this . _codegenSourceModule ( moduleUrl , statements , exportedVars ) ;
8187 }
8288
83- compileStylesheet ( stylesheetUrl : string , cssText : string ) : SourceModule [ ] {
84- var plainStyles = this . _styleCompiler . compileStylesheet ( stylesheetUrl , cssText , false ) ;
85- var shimStyles = this . _styleCompiler . compileStylesheet ( stylesheetUrl , cssText , true ) ;
86- return [
87- this . _codegenSourceModule ( _stylesModuleUrl ( stylesheetUrl , false ) ,
88- _resolveStyleStatements ( plainStyles ) , [ plainStyles . stylesVar ] ) ,
89- this . _codegenSourceModule ( _stylesModuleUrl ( stylesheetUrl , true ) ,
90- _resolveStyleStatements ( shimStyles ) , [ shimStyles . stylesVar ] )
91- ] ;
89+ loadAndCompileStylesheet ( stylesheetUrl : string , shim : boolean ,
90+ suffix : string ) : Promise < StyleSheetSourceWithImports > {
91+ return this . _xhr . get ( stylesheetUrl )
92+ . then ( ( cssText ) => {
93+ var compileResult = this . _styleCompiler . compileStylesheet ( stylesheetUrl , cssText , shim ) ;
94+ var importedUrls = [ ] ;
95+ compileResult . dependencies . forEach ( ( dep ) => {
96+ importedUrls . push ( dep . moduleUrl ) ;
97+ dep . valuePlaceholder . moduleUrl = _stylesModuleUrl ( dep . moduleUrl , dep . isShimmed , suffix ) ;
98+ } ) ;
99+ return new StyleSheetSourceWithImports (
100+ this . _codgenStyles ( stylesheetUrl , shim , suffix , compileResult ) , importedUrls ) ;
101+ } ) ;
92102 }
93103
94104 private _compileComponent ( compMeta : CompileDirectiveMetadata ,
@@ -99,11 +109,18 @@ export class OfflineCompiler {
99109 directives , pipes , compMeta . type . name ) ;
100110 var viewResult = this . _viewCompiler . compileComponent ( compMeta , parsedTemplate ,
101111 o . variable ( styleResult . stylesVar ) , pipes ) ;
102- ListWrapper . addAll ( targetStatements , _resolveStyleStatements ( styleResult ) ) ;
112+ ListWrapper . addAll ( targetStatements ,
113+ _resolveStyleStatements ( compMeta . type . moduleUrl , styleResult ) ) ;
103114 ListWrapper . addAll ( targetStatements , _resolveViewStatements ( viewResult ) ) ;
104115 return viewResult . viewFactoryVar ;
105116 }
106117
118+ private _codgenStyles ( inputUrl : string , shim : boolean , suffix : string ,
119+ stylesCompileResult : StylesCompileResult ) : SourceModule {
120+ return this . _codegenSourceModule ( _stylesModuleUrl ( inputUrl , shim , suffix ) ,
121+ stylesCompileResult . statements ,
122+ [ stylesCompileResult . stylesVar ] ) ;
123+ }
107124
108125 private _codegenSourceModule ( moduleUrl : string , statements : o . Statement [ ] ,
109126 exportedVars : string [ ] ) : SourceModule {
@@ -119,25 +136,36 @@ function _resolveViewStatements(compileResult: ViewCompileResult): o.Statement[]
119136}
120137
121138
122- function _resolveStyleStatements ( compileResult : StylesCompileResult ) : o . Statement [ ] {
139+ function _resolveStyleStatements ( containingModuleUrl : string ,
140+ compileResult : StylesCompileResult ) : o . Statement [ ] {
141+ var containingSuffix = _splitSuffix ( containingModuleUrl ) [ 1 ] ;
123142 compileResult . dependencies . forEach ( ( dep ) => {
124- dep . valuePlaceholder . moduleUrl = _stylesModuleUrl ( dep . sourceUrl , dep . isShimmed ) ;
143+ dep . valuePlaceholder . moduleUrl =
144+ _stylesModuleUrl ( dep . moduleUrl , dep . isShimmed , containingSuffix ) ;
125145 } ) ;
126146 return compileResult . statements ;
127147}
128148
129149function _templateModuleUrl ( comp : CompileDirectiveMetadata ) : string {
130- var moduleUrl = comp . type . moduleUrl ;
131- var urlWithoutSuffix = moduleUrl . substring ( 0 , moduleUrl . length - MODULE_SUFFIX . length ) ;
132- return `${ urlWithoutSuffix } .ngfactory${ MODULE_SUFFIX } ` ;
150+ var urlWithSuffix = _splitSuffix ( comp . type . moduleUrl ) ;
151+ return `${ urlWithSuffix [ 0 ] } .ngfactory${ urlWithSuffix [ 1 ] } ` ;
133152}
134153
135- function _stylesModuleUrl ( stylesheetUrl : string , shim : boolean ) : string {
136- return shim ? `${ stylesheetUrl } .shim${ MODULE_SUFFIX } ` : `${ stylesheetUrl } ${ MODULE_SUFFIX } ` ;
154+ function _stylesModuleUrl ( stylesheetUrl : string , shim : boolean , suffix : string ) : string {
155+ return shim ? `${ stylesheetUrl } .shim${ suffix } ` : `${ stylesheetUrl } ${ suffix } ` ;
137156}
138157
139158function _assertComponent ( meta : CompileDirectiveMetadata ) {
140159 if ( ! meta . isComponent ) {
141160 throw new BaseException ( `Could not compile '${ meta . type . name } ' because it is not a component.` ) ;
142161 }
143162}
163+
164+ function _splitSuffix ( path : string ) : string [ ] {
165+ let lastDot = path . lastIndexOf ( '.' ) ;
166+ if ( lastDot !== - 1 ) {
167+ return [ path . substring ( 0 , lastDot ) , path . substring ( lastDot ) ] ;
168+ } else {
169+ return [ path , '' ] ;
170+ }
171+ }
0 commit comments