From 3e6ecc5e5f72b555ecbf9489f5431ffdbc0bb76e Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 11 May 2023 11:37:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=95=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E4=B8=AD=E5=90=8C=E6=97=B6=E5=BC=95=E5=85=A5?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E6=A0=B7=E5=BC=8F=E6=96=87=E4=BB=B6=E6=97=B6?= =?UTF-8?q?=E9=87=8D=E5=90=8D=E7=9A=84bug;=20=E4=BC=98=E5=8C=96vue?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile-vue.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index cd7e016..a1eee3b 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -6,12 +6,16 @@ import fs from 'iofs' import scss from '@bytedo/sass' -import { createHash } from 'crypto' +import { createHash, randomUUID } from 'crypto' import Es from 'esbuild' import { join } from 'path' import { compile } from '@vue/compiler-dom' import { red, cyan, blue } from 'kolorist' +function uuid() { + return randomUUID().slice(-8) +} + import { JS_EXP, STYLE_EXP, @@ -208,12 +212,8 @@ export function parseJs( if (isBuild) { name = name.replace(/\.scss/, '.css') } - let tmp = `style${Date.now()}` - fixedStyle += ` - let __sheets__ = [...document.adoptedStyleSheets] - __sheets__.push(${tmp}) - document.adoptedStyleSheets = __sheets__ - ` + let tmp = `style_${uuid()}` + fixedStyle += `__sheets__.push(${tmp})\n` // 修正那反人类的windows路径 return `import ${tmp} from '${name}' assert { type: 'css' }\n${tmp}.path = '${name.replace( @@ -300,7 +300,12 @@ export function compileVue(file, imports, options = {}, isBuild) { isCustomElement: tag => tag.startsWith('wc-') }).code.replace('export function render', 'function render') - output += html + '\n\n' + html = html + .replace(/import .* from "vue"/, str => { + output += str + '\n' + return '' + }) + .trim() if (CACHE[file]) { CACHE[file] = { @@ -314,7 +319,7 @@ export function compileVue(file, imports, options = {}, isBuild) { output += parseJs(js, imports, options, isBuild, file).replace( 'export default {', - 'const __sfc__ = {\n render,\n' + `\nconst __sheets__ = [...document.adoptedStyleSheets]\n${html}\n\nconst __sfc__ = {\n render,\n` ) if (scss) { @@ -322,11 +327,12 @@ export function compileVue(file, imports, options = {}, isBuild) { // 修正那反人类的windows路径 output += ` -let stylesheet = new CSSStyleSheet() -let __sheets__ = [...document.adoptedStyleSheets] -stylesheet.path = '${filename}' -stylesheet.replaceSync(\`${scss}\`) -__sheets__.push(stylesheet) +{ + let stylesheet = new CSSStyleSheet() + stylesheet.path = '${filename}' + stylesheet.replaceSync(\`${scss}\`) + __sheets__.push(stylesheet) +} document.adoptedStyleSheets = __sheets__ ` }