11import * as Vue from 'vue'
2- import { parse , compileScript , compileTemplate } from 'vue/compiler-sfc'
2+ import { parse , compileScript , compileStyle , compileTemplate } from 'vue/compiler-sfc'
33import esModuleShims from './es-module-shims-txt.js'
44import { transform } from "sucrase" ;
55
@@ -39,23 +39,42 @@ function patchCompiledTemplateCode(code) {
3939 ] . join ( '\n' ) ;
4040}
4141
42+ function hashSfcId ( source ) {
43+ let hash = 2166136261 ;
44+ for ( let i = 0 ; i < source . length ; i += 1 ) {
45+ hash ^= source . charCodeAt ( i ) ;
46+ hash = Math . imul ( hash , 16777619 ) ;
47+ }
48+ return `ipyvue-${ ( hash >>> 0 ) . toString ( 36 ) } ` ;
49+ }
50+
4251export async function compileSfc ( sfcStr , mixin ) {
4352 await init ( )
4453 const parsedTemplate = parse ( sfcStr )
4554 const { descriptor : { script, scriptSetup, template, styles} } = parsedTemplate ;
55+ const scopeId = hashSfcId ( sfcStr ) ;
56+ const filename = `${ scopeId } .vue` ;
4657
47- styles && styles . forEach ( ( { content, attrs} ) => {
48- const prefixedCssId = attrs . id && `ipyvue-${ attrs . id } ` ;
49- let style = prefixedCssId && document . getElementById ( prefixedCssId ) ;
58+ styles && styles . forEach ( ( { content, scoped , attrs = { } , lang } , index ) => {
59+ const styleDomId = `ipyvue-style- ${ scopeId } - ${ index } ` ;
60+ let style = document . getElementById ( styleDomId ) ;
5061 if ( ! style ) {
5162 style = document . createElement ( 'style' ) ;
52- if ( prefixedCssId ) {
53- style . id = prefixedCssId ;
54- }
63+ style . id = styleDomId ;
5564 document . head . appendChild ( style ) ;
5665 }
57- if ( style . innerHTML !== content ) {
58- style . innerHTML = content ;
66+ const compiledStyle = compileStyle ( {
67+ filename,
68+ id : scopeId ,
69+ preprocessLang : lang ,
70+ scoped,
71+ source : content ,
72+ } ) ;
73+ if ( compiledStyle . errors . length ) {
74+ console . warn ( compiledStyle . errors ) ;
75+ }
76+ if ( style . innerHTML !== compiledStyle . code ) {
77+ style . innerHTML = compiledStyle . code ;
5978 }
6079 } ) ;
6180
@@ -67,7 +86,7 @@ export async function compileSfc(sfcStr, mixin) {
6786 script . content = script . content . replace ( / ^ [ ^ { ] + (? = { ) / , "export default " ) ;
6887 }
6988 }
70- let compiledScript = ( script || scriptSetup ) && compileScript ( parsedTemplate . descriptor , { id : "abc" } ) ;
89+ let compiledScript = ( script || scriptSetup ) && compileScript ( parsedTemplate . descriptor , { id : scopeId } ) ;
7190
7291 const code = compiledScript && ( compiledScript . lang === "ts"
7392 ? transform ( compiledScript . content , { transforms : [ "typescript" ] } ) . code
@@ -76,6 +95,9 @@ export async function compileSfc(sfcStr, mixin) {
7695 let { setup, ...rest } = code ? ( await toModule ( code ) ) . default : { }
7796
7897 const compiledTemplate = template && compileTemplate ( {
98+ filename,
99+ id : scopeId ,
100+ scoped : styles ? styles . some ( ( { scoped } ) => scoped ) : false ,
79101 source : template . content ,
80102 compilerOptions : {
81103 bindingMetadata : compiledScript ? compiledScript . bindings : { } ,
0 commit comments