@@ -26,14 +26,34 @@ const DEBUG = debugGenerator("css-blocks:glimmer:rewriter");
26
26
27
27
export type GlimmerStyleMapping = StyleMapping < TEMPLATE_TYPE > ;
28
28
29
- export class GlimmerRewriter implements ASTPlugin {
29
+ interface ASTPluginWithDeps extends ASTPlugin {
30
+ /**
31
+ * If this method exists, it is called with the relative path to the current
32
+ * file just before processing starts. Use this method to reset the
33
+ * dependency tracking state associated with the file.
34
+ */
35
+ resetDependencies ?( relativePath : string ) : void ;
36
+ /**
37
+ * This method is called just as the template finishes being processed.
38
+ *
39
+ * @param relativePath A relative path to the file that may have dependencies.
40
+ * @return paths to files that are a dependency for the given
41
+ * file. Any relative paths returned by this method are taken to be relative
42
+ * to the file that was processed.
43
+ */
44
+ dependencies ( relativePath : string ) : string [ ] ;
45
+ }
46
+
47
+ export class GlimmerRewriter implements ASTPluginWithDeps {
30
48
template : ResolvedFile ;
31
49
analysis : GlimmerAnalysis ;
32
50
elementCount : number ;
33
51
syntax : Syntax ;
34
52
block : Block ;
35
53
styleMapping : GlimmerStyleMapping ;
36
54
cssBlocksOpts : CSSBlocksConfiguration ;
55
+ visitor : NodeVisitor ;
56
+ visitors : NodeVisitor ;
37
57
38
58
private elementAnalyzer : ElementAnalyzer ;
39
59
@@ -51,6 +71,16 @@ export class GlimmerRewriter implements ASTPlugin {
51
71
this . cssBlocksOpts = resolveConfiguration ( cssBlocksOpts ) ;
52
72
this . elementCount = 0 ;
53
73
this . elementAnalyzer = new ElementAnalyzer ( this . analysis , this . cssBlocksOpts ) ;
74
+ if ( this . block ) {
75
+ this . visitor = {
76
+ ElementNode : this . ElementNode . bind ( this ) ,
77
+ MustacheStatement : this . BuiltinStatement . bind ( this ) ,
78
+ BlockStatement : this . BuiltinStatement . bind ( this ) ,
79
+ } ;
80
+ } else {
81
+ this . visitor = { } ;
82
+ }
83
+ this . visitors = this . visitor ;
54
84
}
55
85
56
86
debug ( message : string , ...args : unknown [ ] ) : void {
@@ -59,15 +89,30 @@ export class GlimmerRewriter implements ASTPlugin {
59
89
60
90
get name ( ) : string { return this . block ? "css-blocks-glimmer-rewriter" : "css-blocks-noop" ; }
61
91
62
- // `visitors` is used by Ember < 3.0.0. `visitor` is used by Glimmer and Ember >= 3.0.0.
63
- get visitor ( ) : NodeVisitor { return this . visitors ; }
64
- get visitors ( ) : NodeVisitor {
65
- if ( ! this . block ) { return { } ; }
66
- return {
67
- ElementNode : this . ElementNode . bind ( this ) ,
68
- MustacheStatement : this . BuiltinStatement . bind ( this ) ,
69
- BlockStatement : this . BuiltinStatement . bind ( this ) ,
70
- } ;
92
+ /**
93
+ * @param _relativePath Unused in this implementation.
94
+ * @returns Files this template file depends on.
95
+ */
96
+ dependencies ( _relativePath : string ) : Array < string > {
97
+ this . debug ( "Getting dependencies for" , _relativePath ) ;
98
+ let deps : Set < string > = new Set ( ) ;
99
+
100
+ // let importer = this.cssBlocksOpts.importer;
101
+ for ( let block of this . analysis . transitiveBlockDependencies ( ) ) {
102
+ // TODO: Figure out why the importer is returning null here.
103
+ // let blockFile = importer.filesystemPath(block.identifier, this.cssBlocksOpts);
104
+ let blockFile = block . identifier ;
105
+ this . debug ( "block file path is" , blockFile ) ;
106
+ if ( blockFile ) {
107
+ deps . add ( blockFile ) ;
108
+ }
109
+ // These dependencies happen when additional files get involved via preprocessors.
110
+ for ( let additionalDep of block . dependencies ) {
111
+ deps . add ( additionalDep ) ;
112
+ }
113
+ }
114
+ let depArray = new Array ( ...deps ) ;
115
+ return depArray ;
71
116
}
72
117
73
118
BuiltinStatement ( node : AST . MustacheStatement | AST . BlockStatement ) {
0 commit comments