调整isCustomElement的设定

pull/1/head
yutent 2023-05-22 15:06:33 +08:00
parent 877ba7c8bf
commit f09e6199ac
6 changed files with 102 additions and 73 deletions

View File

@ -7,7 +7,7 @@
*/ */
import fs from 'iofs' import fs from 'iofs'
import { join, normalize } from 'path' import { join, normalize } from 'node:path'
import { red, blue } from 'kolorist' import { red, blue } from 'kolorist'
import createServer from './lib/dev.js' import createServer from './lib/dev.js'

View File

@ -6,9 +6,9 @@
import fs from 'iofs' import fs from 'iofs'
import scss from '@bytedo/sass' import scss from '@bytedo/sass'
import { createHash, randomUUID } from 'crypto' import { createHash, randomUUID } from 'node:crypto'
import Es from 'esbuild' import Es from 'esbuild'
import { join } from 'path' import { join } from 'node:path'
import { compile } from '@vue/compiler-dom' import { compile } from '@vue/compiler-dom'
import { red, cyan, blue } from 'kolorist' import { red, cyan, blue } from 'kolorist'
@ -23,9 +23,9 @@ import {
CSS_SHEET_EXP, CSS_SHEET_EXP,
V_DEEP, V_DEEP,
PERCENT_EXP, PERCENT_EXP,
SHEETS_DEF, SHEETS_DEF
createHmrScript
} from './constants.js' } from './constants.js'
import {createHmrScript} from './utils.js'
const OPTIONS = { const OPTIONS = {
style: 'compressed' style: 'compressed'
@ -282,6 +282,7 @@ export function compileVue(file, imports, options = {}) {
let filename = file.slice(options.SOURCE_DIR.length).replace(/\\/g, '/') let filename = file.slice(options.SOURCE_DIR.length).replace(/\\/g, '/')
let code = (fs.cat(file) || '').toString().replace(/\r\n/g, '\n') let code = (fs.cat(file) || '').toString().replace(/\r\n/g, '\n')
let CACHE = options.CACHE || {} let CACHE = options.CACHE || {}
let { isCustomElement } = options
let output = '', let output = '',
scoped = false scoped = false
@ -319,7 +320,7 @@ export function compileVue(file, imports, options = {}) {
cacheHandlers: true, cacheHandlers: true,
scopeId: scoped ? scopeId : void 0, scopeId: scoped ? scopeId : void 0,
sourceMap: false, sourceMap: false,
isCustomElement: tag => tag.startsWith('wc-') isCustomElement
}).code.replace('export function render', 'function render') }).code.replace('export function render', 'function render')
} catch (err) { } catch (err) {
let tmp = html[1].split('\n') let tmp = html[1].split('\n')

View File

@ -18,61 +18,3 @@ export const COMMON_HEADERS = {
export const SHEETS_DEF = export const SHEETS_DEF =
'const __sheets__ = [...document.adoptedStyleSheets];\n' 'const __sheets__ = [...document.adoptedStyleSheets];\n'
export function createHmrScript(legacy) {
return `
!(function vue_live_hmr(){
var ws = new WebSocket(\`ws\${location.protocol === 'https:' ? 's' : ''}://\${location.host}/ws-vue-live\`)
ws.addEventListener('open', function (r) {
if(vue_live_hmr.closed){
delete vue_live_hmr.closed
location.reload()
}
console.log('vue-live hmr ready...')
})
ws.addEventListener('close', function(){
vue_live_hmr.closed = true
setTimeout(vue_live_hmr, 2000)
})
ws.addEventListener('message', function (ev) {
var { action, data } = JSON.parse(ev.data)
switch (action) {
case 'reload':
location.reload()
break
case 'render':
{
${
legacy
? `
let stylesheet = document.head.children.namedItem(data.path)
if (stylesheet) {
stylesheet.textContent = data.content
}
`
: `
let tmp = [...document.adoptedStyleSheets]
for (let i = -1, it; (it = tmp[++i]); ) {
if (it.path === data.path) {
let stylesheet = new CSSStyleSheet()
stylesheet.path = data.path
stylesheet.replaceSync(data.content)
tmp[i] = stylesheet
document.adoptedStyleSheets = tmp
break
}
}
`
}
}
break
}
})
})()
`
}

View File

@ -1,13 +1,13 @@
import http from 'http' import http from 'node:http'
import https from 'https' import https from 'node:https'
import fs from 'iofs' import fs from 'iofs'
import { join, dirname } from 'path' import { join, dirname } from 'node:path'
import { parse } from 'url' import { parse } from 'node:url'
import socket from './ws.js' import socket from './ws.js'
import chokidar from 'chokidar' import chokidar from 'chokidar'
import { red } from 'kolorist' import { red } from 'kolorist'
import { friendlyErrors } from './utils.js' import { friendlyErrors , isCustomElement} from './utils.js'
import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js'
@ -210,7 +210,8 @@ export default async function createServer(root = '', conf = {}) {
CACHE, CACHE,
DEPLOY_PATH, DEPLOY_PATH,
INJECT_SCSS, INJECT_SCSS,
LEGACY_MODE LEGACY_MODE,
isCustomElement: conf.isCustomElement || isCustomElement
}) })
res.setHeader('content-type', MIME_TYPES.js) res.setHeader('content-type', MIME_TYPES.js)
@ -382,7 +383,8 @@ export default async function createServer(root = '', conf = {}) {
CACHE, CACHE,
DEPLOY_PATH, DEPLOY_PATH,
INJECT_SCSS, INJECT_SCSS,
LEGACY_MODE LEGACY_MODE,
isCustomElement: conf.isCustomElement || isCustomElement
}) })
let tmp = CACHE[filePath] let tmp = CACHE[filePath]
if (tmp.changed) { if (tmp.changed) {

View File

@ -1,7 +1,8 @@
import { join, dirname, parse } from 'node:path'
import fs from 'iofs' import fs from 'iofs'
import { join, dirname, parse } from 'path'
import Es from 'esbuild' import Es from 'esbuild'
import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js'
import { isCustomElement } from './utils.js'
function readFile(file) { function readFile(file) {
return (file && fs.cat(file)?.toString()) || '' return (file && fs.cat(file)?.toString()) || ''
@ -50,7 +51,8 @@ export default function compile(root = '', dist = '', conf = {}) {
SOURCE_DIR, SOURCE_DIR,
DEPLOY_PATH, DEPLOY_PATH,
INJECT_SCSS, INJECT_SCSS,
LEGACY_MODE LEGACY_MODE,
isCustomElement: conf.isCustomElement || isCustomElement
} }
for (let it of files) { for (let it of files) {

View File

@ -1,4 +1,11 @@
/**
* {一些工具类函数}
* @author yutent<yutent.io@gmail.com>
* @date 2023/05/22 14:52:00
*/
import { red, cyan, blue } from 'kolorist' import { red, cyan, blue } from 'kolorist'
import { Worker } from 'node:worker_threads'
export function friendlyErrors(pathname, ext = '') { export function friendlyErrors(pathname, ext = '') {
console.log(cyan(pathname), red(`not found!!!`)) console.log(cyan(pathname), red(`not found!!!`))
@ -7,3 +14,78 @@ export function friendlyErrors(pathname, ext = '') {
blue(`/index.${ext}`) blue(`/index.${ext}`)
) )
} }
export function createHmrScript(legacy) {
return `
!(function vue_live_hmr(){
var ws = new WebSocket(\`ws\${location.protocol === 'https:' ? 's' : ''}://\${location.host}/ws-vue-live\`)
ws.addEventListener('open', function (r) {
if(vue_live_hmr.closed){
delete vue_live_hmr.closed
location.reload()
}
console.log('vue-live hmr ready...')
})
ws.addEventListener('close', function(){
vue_live_hmr.closed = true
setTimeout(vue_live_hmr, 2000)
})
ws.addEventListener('message', function (ev) {
var { action, data } = JSON.parse(ev.data)
switch (action) {
case 'reload':
location.reload()
break
case 'render':
{
${
legacy
? `
let stylesheet = document.head.children.namedItem(data.path)
if (stylesheet) {
stylesheet.textContent = data.content
}
`
: `
let tmp = [...document.adoptedStyleSheets]
for (let i = -1, it; (it = tmp[++i]); ) {
if (it.path === data.path) {
let stylesheet = new CSSStyleSheet()
stylesheet.path = data.path
stylesheet.replaceSync(data.content)
tmp[i] = stylesheet
document.adoptedStyleSheets = tmp
break
}
}
`
}
}
break
}
})
})()
`
}
export function isCustomElement(tag) {
return tag.startsWith('wc-')
}
export function startWorker(file) {
return new Promise((resolve, reject) => {
let worker = new Worker(file)
worker.on('message', resolve)
worker.on('error', reject)
worker.on('exit', code => {
if (code !== 0) {
reject(new Error(`Worker stopped with exit code ${code}`))
}
})
})
}