@@ -15,6 +15,7 @@ import { CompileResult } from './interfaces';
15
15
import RollupResult from './RollupResult' ;
16
16
17
17
const stderr = console . error . bind ( console ) ;
18
+ const INJECT_STYLES_ID = 'inject_styles' ;
18
19
19
20
let rollup : any ;
20
21
@@ -123,6 +124,25 @@ export default class RollupCompiler {
123
124
}
124
125
mod . plugins . push ( {
125
126
name : 'sapper-internal' ,
127
+ buildStart ( this : PluginContext ) : void {
128
+ this . emitFile ( {
129
+ type : 'chunk' ,
130
+ id : INJECT_STYLES_ID ,
131
+ name : INJECT_STYLES_ID ,
132
+ preserveSignature : 'strict'
133
+ } ) ;
134
+ } ,
135
+ load ( id : string ) {
136
+ return id === INJECT_STYLES_ID ? { code : inject_styles , moduleSideEffects : 'no-treeshake' } : null ;
137
+ } ,
138
+ resolveId ( importee : string , importer : string ) {
139
+ return importee === INJECT_STYLES_ID ? INJECT_STYLES_ID : null ;
140
+ } ,
141
+ transform ( code : string , id : string ) {
142
+ if ( id === INJECT_STYLES_ID ) {
143
+ return { code, moduleSideEffects : 'no-treeshake' } ;
144
+ }
145
+ } ,
126
146
renderChunk ( code : string , chunk : RenderedChunk ) {
127
147
that . chunks . push ( chunk ) ;
128
148
} ,
@@ -166,6 +186,8 @@ export default class RollupCompiler {
166
186
return Array . from ( css_files ) ;
167
187
} ;
168
188
189
+ const inject_styles_file = Object . keys ( bundle ) . find ( f => f . startsWith ( 'inject_styles' ) ) ;
190
+
169
191
let has_css = false ;
170
192
for ( const name in bundle ) {
171
193
const chunk = < OutputChunk > bundle [ name ] ;
@@ -190,17 +212,13 @@ export default class RollupCompiler {
190
212
191
213
if ( chunk_has_css ) {
192
214
has_css = true ;
193
- chunk . code += `\nimport __inject_styles from './inject_styles.js ';` ;
215
+ chunk . code += `\nimport __inject_styles from './${ inject_styles_file } ';` ;
194
216
}
195
217
}
196
218
}
197
219
198
- if ( has_css ) {
199
- this . emitFile ( {
200
- type : 'asset' ,
201
- fileName : 'inject_styles.js' ,
202
- source : inject_styles
203
- } ) ;
220
+ if ( ! has_css ) {
221
+ delete bundle [ inject_styles_file ] ;
204
222
}
205
223
206
224
// Store the build dependencies so that we can create build.json
0 commit comments