@@ -267,14 +267,8 @@ async function preprocessCSS(css) {
267267
268268 } ) ) ;
269269
270- // inject cssText
271- // TODO: inject cssText in final export script
272- var _cssDom = document . createElement ( 'style' ) ;
273- _cssDom . innerHTML = _cssText ;
274- document . body . appendChild ( _cssDom ) ;
275-
276270 tmp_iframe . remove ( ) ;
277- css . cssDom = _cssDom ;
271+ // css.cssDom = _cssDom;
278272 css . scopeId = _scopeId ;
279273 css . cssText = _cssText ;
280274 return css ;
@@ -362,20 +356,52 @@ async function transpileSFC(sfc_src) {
362356 let sfc_code = sfc_obj . js . jsText ;
363357 let sfc_var = `sfc_${ genScopeId ( ) . substr ( 7 ) } ` ;
364358 sfc_code = sfc_code . replace ( / e x p o r t \W + d e f a u l t / i, `let ${ sfc_var } =` ) ;
365- sfc_code += `${ sfc_var } .template = \`${ sfc_obj . html . templateText } \`;\n` ;
366- sfc_code += `${ sfc_var } ._scopeId = \`${ sfc_obj . css . scopeId } \`;\n` ;
359+
360+ if ( sfc_obj . css . scopeId ) {
361+ sfc_code += `${ sfc_var } ._scopeId = \`${ sfc_obj . css . scopeId } \`;\n` ;
362+ }
363+
364+ sfc_code += [
365+ `${ sfc_var } .template = \`${ sfc_obj . html . templateText } \`;` ,
366+
367+ // inject css tag
368+ `let dom = document.createElement('style');` ,
369+ `dom.innerHTML = \`${ sfc_obj . css . cssText } \`;` ,
370+ `document.body.appendChild(dom);` ,
371+
372+ // bind css dom to Vue instance
373+ `let _beforeCreate = ${ sfc_var } .beforeCreate;` ,
374+ `${ sfc_var } .beforeCreate = function() {` ,
375+ ` this.$cssDom = dom;` ,
376+ ` if (_beforeCreate) { _beforeCreate(); }` ,
377+ `}` ,
378+
379+ // remove css dom when Vue instance is destroyed
380+ `let _destroyed = ${ sfc_var } .destroyed;` ,
381+ `${ sfc_var } .destroyed = function() {` ,
382+ ` this.$cssDom = dom;` ,
383+ ` if (this.$cssDom) {` ,
384+ ` this.$cssDom.remove();` ,
385+ ` delete this.$cssDom;` ,
386+ ` }` ,
387+ ` if (_destroyed) { _destroyed(); }` ,
388+ `};` ,
389+ ] . join ( '\n' ) ;
390+
367391 sfc_code += `export default ${ sfc_var } ;\n` ;
368392
369393 return [ sfc_code , sfc_obj ] ;
370394}
371395
372- /**
373- * Init
374- */
396+ // TODO: enclose whole script, only export 'Vueify'.
375397var Vueify = {
376398 pendingSFCs : { } , // record unfinished downloads
377399 rootSFCs : [ ] ,
378400} ;
401+
402+ /**
403+ * Init
404+ */
379405function init ( ) {
380406
381407 if ( ! Vue ) {
@@ -432,7 +458,6 @@ function init() {
432458
433459 // finish registering async component
434460 var module = await import ( sfc_blob_url ) ;
435- console . log ( module ) ;
436461 Vueify . pendingSFCs [ sfc_name ] . resolve ( module . default ) ;
437462
438463 // clean up
0 commit comments