@@ -6,7 +6,7 @@ import { RawSourceMap } from "source-map";
6
6
7
7
import { Block } from "../BlockTree" ;
8
8
import { Options , ResolvedConfiguration , resolveConfiguration } from "../configuration" ;
9
- import { FileIdentifier , ImportedFile , Importer } from "../importing" ;
9
+ import { FileIdentifier , ImportedCompiledCssFile , ImportedFile , Importer } from "../importing" ;
10
10
import { PromiseQueue } from "../util/PromiseQueue" ;
11
11
12
12
import { BlockParser , ParsedSource } from "./BlockParser" ;
@@ -183,26 +183,31 @@ export class BlockFactory {
183
183
private async _getBlockPromiseAsync ( identifier : FileIdentifier ) : Promise < Block > {
184
184
try {
185
185
let file = await this . importer . import ( identifier , this . configuration ) ;
186
+
187
+ let block ;
186
188
if ( file . type === "ImportedCompiledCssFile" ) {
187
- // TODO: Process ImportedCompiledCssFile type.
188
- throw new Error ( "Imported Compiled CSS files aren't supported yet." ) ;
189
+ block = await this . _reconstituteCompiledCssSource ( file ) ;
189
190
} else {
190
- let block = await this . _importAndPreprocessBlock ( file ) ;
191
- debug ( `Finalizing Block object for " ${ block . identifier } "` ) ;
191
+ block = await this . _importAndPreprocessBlock ( file ) ;
192
+ }
192
193
193
- // last check to make sure we don't return a new instance
194
- if ( this . blocks [ block . identifier ] ) {
195
- return this . blocks [ block . identifier ] ;
196
- }
194
+ debug ( `Finalizing Block object for "${ block . identifier } "` ) ;
197
195
198
- // Ensure this block name is unique.
199
- block . setName ( this . getUniqueBlockName ( block . name ) ) ;
196
+ // last check to make sure we don't return a new instance
197
+ if ( this . blocks [ block . identifier ] ) {
198
+ return this . blocks [ block . identifier ] ;
199
+ }
200
200
201
- // if the block has any errors, surface them here unless we're in fault tolerant mode .
202
- this . _surfaceBlockErrors ( block ) ;
203
- this . blocks [ block . identifier ] = block ;
204
- return block ;
201
+ // Ensure this block name is unique .
202
+ // Only need to run this on ImportedFile types.
203
+ if ( ! file . type || file . type === "ImportedFile" ) {
204
+ block . setName ( this . getUniqueBlockName ( block . name ) ) ;
205
205
}
206
+
207
+ // if the block has any errors, surface them here unless we're in fault tolerant mode.
208
+ this . _surfaceBlockErrors ( block ) ;
209
+ this . blocks [ block . identifier ] = block ;
210
+ return block ;
206
211
} catch ( error ) {
207
212
if ( this . preprocessQueue . activeJobCount > 0 ) {
208
213
debug ( `Block error. Currently there are ${ this . preprocessQueue . activeJobCount } preprocessing jobs. waiting.` ) ;
@@ -295,6 +300,44 @@ export class BlockFactory {
295
300
return this . parser . parseSource ( source ) ;
296
301
}
297
302
303
+ private async _reconstituteCompiledCssSource ( file : ImportedCompiledCssFile ) : Promise < Block > {
304
+ // Maybe we already have this block in cache?
305
+ if ( this . blocks [ file . identifier ] ) {
306
+ debug ( `Using pre-compiled Block for "${ file . identifier } "` ) ;
307
+ return this . blocks [ file . identifier ] ;
308
+ }
309
+
310
+ // NOTE: If we had to upgrade the syntax version of a definition file, here's where'd we do that.
311
+ // But this isn't a thing we need to do until we have multiple syntax versions.
312
+
313
+ // NOTE: No need to run preprocessor - we assume that Compiled CSS has already been preprocessed.
314
+ // Parse the definition file into an AST
315
+ const definitionAst = this . postcssImpl . parse ( file . definitionContents ) ;
316
+
317
+ // Parse the CSS contents into an AST
318
+ const cssContentsAst = this . postcssImpl . parse ( file . cssContents ) ;
319
+
320
+ // TODO: Sourcemaps?
321
+
322
+ // Sanity check! Did we actually get contents for both ASTs?
323
+ if ( ! definitionAst || ! definitionAst . nodes ) {
324
+ throw new Error ( `Unable to parse definition file into AST!\nIdentifier: ${ file . identifier } ` ) ;
325
+ }
326
+
327
+ if ( ! cssContentsAst || ! cssContentsAst . nodes ) {
328
+ throw new Error ( `Unable to parse CSS contents into AST!\nIdentifier: ${ file . identifier } ` ) ;
329
+ }
330
+
331
+ // Construct a Block out of the definition file.
332
+ const block = this . parser . parseDefinitionSource ( definitionAst , file . identifier , file . blockId ) ;
333
+
334
+ // Merge the rules from the CSS contents into the Block.
335
+ // TODO: Actually merge the CSS rules in. (^_^")
336
+
337
+ // And we're done!
338
+ return block ;
339
+ }
340
+
298
341
/**
299
342
* Similar to getBlock(), this imports and parses a block data file. However, this
300
343
* method parses a block relative to another block.
0 commit comments