1
1
import BroccoliDebug = require( "broccoli-debug" ) ;
2
2
import funnel = require( "broccoli-funnel" ) ;
3
+ import mergeTrees = require( "broccoli-merge-trees" ) ;
3
4
import EmberApp from "ember-cli/lib/broccoli/ember-app" ;
4
5
import type Addon from "ember-cli/lib/models/addon" ;
5
6
import type { AddonImplementation , ThisAddon } from "ember-cli/lib/models/addon" ;
6
7
import Project from "ember-cli/lib/models/project" ;
7
8
8
- import { CSSBlocksApplicationPlugin } from "./brocolli-plugin" ;
9
+ import { CSSBlocksApplicationPlugin , CSSBlocksStylesProcessorPlugin } from "./brocolli-plugin" ;
9
10
10
11
interface AddonEnvironment {
11
12
parent : Addon | EmberApp ;
@@ -19,6 +20,7 @@ interface CSSBlocksApplicationAddon {
19
20
_modulePrefix ( ) : string ;
20
21
env : AddonEnvironment | undefined ;
21
22
getEnv ( parent ) : AddonEnvironment ;
23
+ broccoliAppPluginInstance : CSSBlocksApplicationPlugin | undefined ;
22
24
}
23
25
24
26
/**
@@ -60,6 +62,12 @@ const EMBER_ADDON: AddonImplementation<CSSBlocksApplicationAddon> = {
60
62
61
63
env : undefined ,
62
64
65
+ /**
66
+ * The instance of the CSSBlocksApplicationPlugin. This instance is
67
+ * generated during the JS tree and is needed for the CSS tree.
68
+ */
69
+ broccoliAppPluginInstance : undefined ,
70
+
63
71
/**
64
72
* Initalizes this addon instance for use.
65
73
* @param parent - The project or addon that directly depends on this addon.
@@ -128,14 +136,28 @@ const EMBER_ADDON: AddonImplementation<CSSBlocksApplicationAddon> = {
128
136
preprocessTree ( type , tree ) {
129
137
// tslint:disable-next-line:prefer-unknown-to-any
130
138
let env = this . env ! ;
139
+
131
140
if ( type === "js" ) {
132
141
if ( env . isApp ) {
133
- let appAndAddonTree = new CSSBlocksApplicationPlugin ( env . modulePrefix , [ env . app . addonTree ( ) , tree ] , { } ) ;
134
- let debugTree = new BroccoliDebug ( appAndAddonTree , `css-blocks:optimized` ) ;
142
+ this . broccoliAppPluginInstance = new CSSBlocksApplicationPlugin ( env . modulePrefix , [ env . app . addonTree ( ) , tree ] , { } ) ;
143
+ let debugTree = new BroccoliDebug ( this . broccoliAppPluginInstance , `css-blocks:optimized` ) ;
135
144
return funnel ( debugTree , { srcDir : env . modulePrefix , destDir : env . modulePrefix } ) ;
136
145
} else {
137
146
return tree ;
138
147
}
148
+ } else if ( type === "css" ) {
149
+ // We can't do much if we don't have the result from CSSBlocksApplicationPlugin.
150
+ // This should never happen because the JS tree is processed before the CSS tree,
151
+ // but just in case....
152
+ if ( ! env . isApp ) {
153
+ return tree ;
154
+ }
155
+ if ( ! this . broccoliAppPluginInstance ) {
156
+ throw new Error ( "[css-blocks/ember-app] The CSS tree ran before the JS tree, so the CSS tree doesn't have the contents for CSS Blocks files. This shouldn't ever happen, but if it does, please file an issue with us!" ) ;
157
+ }
158
+ // Get the combined CSS file
159
+ const cssBlocksContentsTree = new CSSBlocksStylesProcessorPlugin ( [ this . broccoliAppPluginInstance , tree ] ) ;
160
+ return new BroccoliDebug ( mergeTrees ( [ tree , cssBlocksContentsTree ] , { overwrite : true } ) , "css-blocks:css-preprocess" ) ;
139
161
} else {
140
162
return tree ;
141
163
}
0 commit comments