2022-09-09 10:52:27 +08:00
|
|
|
/**
|
|
|
|
* {}
|
|
|
|
* @author yutent<yutent.io@gmail.com>
|
|
|
|
* @date 2022/09/06 14:43:01
|
|
|
|
*/
|
|
|
|
|
|
|
|
import fs from 'iofs'
|
|
|
|
import scss from '@bytedo/sass'
|
2023-02-01 10:51:33 +08:00
|
|
|
import Es from 'esbuild'
|
2023-06-13 15:32:19 +08:00
|
|
|
|
2023-03-01 00:30:07 +08:00
|
|
|
import { compile } from '@vue/compiler-dom'
|
2023-04-26 17:16:43 +08:00
|
|
|
import { red, cyan, blue } from 'kolorist'
|
2022-09-09 10:52:27 +08:00
|
|
|
|
2023-02-01 10:51:33 +08:00
|
|
|
import {
|
|
|
|
JS_EXP,
|
|
|
|
STYLE_EXP,
|
|
|
|
HTML_EXP,
|
|
|
|
CSS_SHEET_EXP,
|
2023-04-26 15:10:16 +08:00
|
|
|
V_DEEP,
|
2023-05-18 12:04:24 +08:00
|
|
|
PERCENT_EXP,
|
2024-03-01 18:39:39 +08:00
|
|
|
SHEETS_DEF,
|
|
|
|
LEGACY_POLYFILL
|
2023-02-01 10:51:33 +08:00
|
|
|
} from './constants.js'
|
2023-06-13 15:32:19 +08:00
|
|
|
import { createHmrScript, md5, uuid, urlJoin } from './utils.js'
|
2022-09-09 10:52:27 +08:00
|
|
|
|
|
|
|
const OPTIONS = {
|
2023-05-16 14:17:40 +08:00
|
|
|
style: 'compressed'
|
2022-09-09 10:52:27 +08:00
|
|
|
}
|
|
|
|
|
2024-03-01 18:39:39 +08:00
|
|
|
function minify(code) {
|
|
|
|
return Es.transformSync(code, { minify: true }).code.trim()
|
|
|
|
}
|
|
|
|
|
2023-06-13 15:32:19 +08:00
|
|
|
// 处理css中的 :deep()
|
2023-04-27 17:13:58 +08:00
|
|
|
function parseVDeep(curr, val, scoped) {
|
|
|
|
let res = V_DEEP.exec(curr)
|
|
|
|
if (res) {
|
|
|
|
scoped && (val = val.replace(/\[data\-[^\]]+\]/g, ''))
|
|
|
|
return `${res[1] + res[2]} ${val}`
|
|
|
|
} else {
|
|
|
|
return `${curr} ${val}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-13 15:32:19 +08:00
|
|
|
// 处理style中的scoped属性
|
2022-11-03 18:22:42 +08:00
|
|
|
function scopeCss(css = '', hash) {
|
2023-02-20 00:41:58 +08:00
|
|
|
return css.replace(CSS_SHEET_EXP, (m, selector) => {
|
|
|
|
if (!selector.startsWith('@')) {
|
2022-10-18 16:02:29 +08:00
|
|
|
selector = selector.split(',')
|
|
|
|
|
|
|
|
selector = selector
|
|
|
|
.map(s => {
|
2023-04-26 15:10:16 +08:00
|
|
|
// 针对 @keyframe的处理
|
|
|
|
if (s === 'from' || s === 'to' || PERCENT_EXP.test(s)) {
|
|
|
|
return s
|
|
|
|
}
|
2022-10-18 16:02:29 +08:00
|
|
|
let tmp = s.split(' ')
|
2023-04-27 17:13:58 +08:00
|
|
|
let output = ''
|
|
|
|
let last
|
|
|
|
let scoped = false
|
|
|
|
|
|
|
|
while ((last = tmp.pop())) {
|
|
|
|
if (scoped) {
|
|
|
|
if (last.startsWith(':')) {
|
|
|
|
output = parseVDeep(last, output, true)
|
2023-04-27 15:46:28 +08:00
|
|
|
} else {
|
2024-09-27 18:43:14 +08:00
|
|
|
output = `${last}[data-${hash}] ${output}`
|
2023-04-27 15:46:28 +08:00
|
|
|
}
|
2023-03-28 11:39:34 +08:00
|
|
|
} else {
|
2023-05-04 14:58:03 +08:00
|
|
|
if (last.includes(':')) {
|
|
|
|
if (last.startsWith(':')) {
|
|
|
|
output = parseVDeep(last, output)
|
|
|
|
} else {
|
|
|
|
scoped = true
|
|
|
|
last = last.replace(':', `[data-${hash}]:`)
|
|
|
|
output = `${last} ${output}`
|
|
|
|
}
|
2023-03-28 11:39:34 +08:00
|
|
|
} else {
|
2023-04-27 17:13:58 +08:00
|
|
|
scoped = true
|
|
|
|
output = `${last}[data-${hash}] ${output}`
|
2023-03-28 11:39:34 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-18 16:02:29 +08:00
|
|
|
}
|
2023-04-27 17:13:58 +08:00
|
|
|
|
|
|
|
return output
|
2022-10-18 16:02:29 +08:00
|
|
|
})
|
|
|
|
.join(', ')
|
2023-02-20 00:41:58 +08:00
|
|
|
}
|
|
|
|
return selector + '{'
|
|
|
|
})
|
2022-10-18 16:02:29 +08:00
|
|
|
}
|
|
|
|
|
2022-09-09 10:52:27 +08:00
|
|
|
/**
|
|
|
|
* 编译scss为css
|
|
|
|
* @param file <String> 文件路径或scss代码
|
2023-05-16 14:17:40 +08:00
|
|
|
* @param inject <String> 要注入的scss代码
|
2022-09-09 10:52:27 +08:00
|
|
|
*/
|
2023-05-16 14:17:40 +08:00
|
|
|
export function compileScss(file, inject = '') {
|
2022-09-09 10:52:27 +08:00
|
|
|
try {
|
|
|
|
if (fs.isfile(file)) {
|
2023-05-16 14:17:40 +08:00
|
|
|
return scss.compile(file, OPTIONS).css.trim()
|
2022-09-09 10:52:27 +08:00
|
|
|
} else {
|
2023-05-16 14:17:40 +08:00
|
|
|
return scss.compileString(inject + file, OPTIONS).css.trim()
|
2022-09-09 10:52:27 +08:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 解析js
|
|
|
|
* 主要是处理js的依赖引用
|
|
|
|
* @param code <String> js代码
|
|
|
|
*/
|
2023-01-13 11:40:53 +08:00
|
|
|
export function parseJs(
|
|
|
|
code = '',
|
|
|
|
imports,
|
2024-07-25 16:30:30 +08:00
|
|
|
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE, define } = {},
|
2023-06-21 17:10:32 +08:00
|
|
|
filename,
|
2023-06-25 11:59:20 +08:00
|
|
|
linePatch = 1
|
2023-01-13 11:40:53 +08:00
|
|
|
) {
|
2023-05-18 12:04:24 +08:00
|
|
|
let fixedStyle = ''
|
2023-03-17 17:56:05 +08:00
|
|
|
let ASSETS_DIR = '/@/'
|
2023-05-11 12:25:07 +08:00
|
|
|
let isBuild = process.env.NODE_ENV === 'production'
|
2023-03-02 16:55:52 +08:00
|
|
|
|
|
|
|
if (isBuild) {
|
2023-05-11 12:25:07 +08:00
|
|
|
ASSETS_DIR = '/assets/'
|
2023-03-02 16:55:52 +08:00
|
|
|
}
|
2023-05-11 18:17:28 +08:00
|
|
|
|
2023-04-26 17:16:43 +08:00
|
|
|
try {
|
|
|
|
code = Es.transformSync(code).code || ''
|
|
|
|
} catch (e) {
|
|
|
|
let err = e.errors.pop()
|
2023-06-21 17:10:32 +08:00
|
|
|
let lines = code.split('\n')
|
|
|
|
|
|
|
|
console.log('%s: %s', red('Uncaught SyntaxError'), red(err.text))
|
|
|
|
// 将上下文几行都打印出来
|
|
|
|
for (let i = err.location.line - 3; i <= err.location.line + 1; i++) {
|
|
|
|
console.log(
|
|
|
|
' @ line %d: %s',
|
|
|
|
i + linePatch,
|
|
|
|
err.location.line === i + 1 ? red(lines[i]) : lines[i]
|
|
|
|
)
|
|
|
|
}
|
2023-04-26 17:16:43 +08:00
|
|
|
console.log(
|
|
|
|
' @ %s:%d:%d',
|
|
|
|
blue(filename),
|
2023-06-21 17:10:32 +08:00
|
|
|
err.location.line + linePatch - 1,
|
2023-04-26 17:16:43 +08:00
|
|
|
err.location.column
|
|
|
|
)
|
|
|
|
}
|
2022-09-09 10:52:27 +08:00
|
|
|
|
2023-05-18 12:04:24 +08:00
|
|
|
code = code
|
|
|
|
.replace(/\r\n/g, '\n')
|
|
|
|
.replace(/process\.env\.NODE_ENV/g, `'${process.env.NODE_ENV}'`)
|
|
|
|
.replace(
|
2024-03-01 18:39:39 +08:00
|
|
|
/(import|export) ([^'"]*?) from (["'])(.*?)\3/g,
|
2023-05-18 15:50:29 +08:00
|
|
|
function (m, t, alias, q, name) {
|
2023-05-18 12:04:24 +08:00
|
|
|
if (name.startsWith('@/')) {
|
|
|
|
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
|
|
|
|
}
|
2022-09-09 10:52:27 +08:00
|
|
|
|
2023-05-18 12:04:24 +08:00
|
|
|
if (!imports[name]) {
|
|
|
|
if (name.startsWith('./') || name.startsWith('../')) {
|
|
|
|
if (IS_ENTRY) {
|
|
|
|
if (IS_MPA) {
|
|
|
|
name = `pages/${currentPage}/` + name
|
2023-01-13 11:40:53 +08:00
|
|
|
}
|
2023-05-18 12:04:24 +08:00
|
|
|
name = urlJoin(DEPLOY_PATH, ASSETS_DIR, name)
|
2022-10-10 15:05:30 +08:00
|
|
|
}
|
2023-05-18 12:04:24 +08:00
|
|
|
} else if (
|
|
|
|
name.startsWith('/') &&
|
|
|
|
!name.startsWith('//') &&
|
|
|
|
!name.startsWith(urlJoin(DEPLOY_PATH, ASSETS_DIR))
|
|
|
|
) {
|
|
|
|
name = name.replace(/^\//, urlJoin(DEPLOY_PATH, ASSETS_DIR))
|
|
|
|
}
|
2022-09-09 10:52:27 +08:00
|
|
|
|
2023-05-30 10:19:41 +08:00
|
|
|
if (
|
|
|
|
!name.endsWith('.js') &&
|
|
|
|
!name.endsWith('.mjs') &&
|
|
|
|
!name.endsWith('.vue')
|
|
|
|
) {
|
2023-05-18 12:04:24 +08:00
|
|
|
if (name.includes('components')) {
|
|
|
|
name += '.vue'
|
|
|
|
} else {
|
|
|
|
name += '.js'
|
2022-09-09 10:52:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 19:31:04 +08:00
|
|
|
if (isBuild) {
|
|
|
|
name = name.replace(/\.vue$/, '.js')
|
|
|
|
}
|
2023-11-06 19:07:08 +08:00
|
|
|
if (alias.trim() === '*') {
|
|
|
|
return `${t} ${alias} from '${name}'`
|
|
|
|
}
|
|
|
|
let _alias = alias
|
2024-03-01 18:39:39 +08:00
|
|
|
let _import = ''
|
2023-11-06 19:07:08 +08:00
|
|
|
if (alias.includes('* as')) {
|
|
|
|
_alias = ' default ' + alias.replace('* as', '').trim()
|
|
|
|
}
|
|
|
|
|
2024-04-01 18:26:00 +08:00
|
|
|
_import = `import ${alias} from '${name}'`
|
2024-03-01 18:39:39 +08:00
|
|
|
_import += t === 'export' ? `\nexport ${_alias}` : ''
|
2024-04-01 18:26:00 +08:00
|
|
|
|
2024-03-01 18:39:39 +08:00
|
|
|
return _import
|
2023-05-18 12:04:24 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.replace(/import\((['"])(.*?)\1\)/g, function (m, q, name) {
|
|
|
|
if (isBuild) {
|
|
|
|
name = name.replace(/\.vue$/, '.js')
|
|
|
|
}
|
|
|
|
if (name.startsWith('@/')) {
|
|
|
|
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
|
|
|
|
}
|
2024-04-01 18:26:00 +08:00
|
|
|
if (name.endsWith('.json')) {
|
|
|
|
name += '.js'
|
|
|
|
}
|
2023-05-18 12:04:24 +08:00
|
|
|
return `import('${name}')`
|
|
|
|
})
|
|
|
|
.replace(/import (["'])(.*?)\1/g, function (m, q, name) {
|
|
|
|
if (name.endsWith('.css') || name.endsWith('.scss')) {
|
2023-03-02 16:55:52 +08:00
|
|
|
if (name.startsWith('@/')) {
|
2023-03-17 17:56:05 +08:00
|
|
|
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
|
2023-03-02 16:55:52 +08:00
|
|
|
}
|
2023-01-31 19:17:38 +08:00
|
|
|
|
2023-05-18 12:04:24 +08:00
|
|
|
if (isBuild) {
|
|
|
|
name = name.replace(/\.scss/, '.css')
|
|
|
|
}
|
|
|
|
// 修正那反人类的windows路径
|
|
|
|
let _name = name.replace(/\\/g, '/').replace('@/', '')
|
|
|
|
let tmp = `style_${uuid()}`
|
|
|
|
|
2024-04-01 18:26:00 +08:00
|
|
|
// 因为esm语法的变更, 原先的 import xx from xx assets {type: css} 变为了 with
|
|
|
|
// 而这个语法的变化, 构建工具无法做版本判断, 故, 统一降级到fetch()加载
|
2023-05-18 12:04:24 +08:00
|
|
|
if (LEGACY_MODE) {
|
2024-04-01 18:26:00 +08:00
|
|
|
fixedStyle +=
|
|
|
|
`{\n` +
|
|
|
|
` let stylesheet = document.createElement('style');\n` +
|
|
|
|
` stylesheet.setAttribute('name', '${_name}');\n` +
|
|
|
|
` stylesheet.textContent = ${tmp};\n` +
|
|
|
|
` document.head.appendChild(stylesheet);\n` +
|
|
|
|
`}\n`
|
2024-09-13 18:36:44 +08:00
|
|
|
|
|
|
|
return `let ${tmp};\n!(async function(){\n ${tmp} = await __fite_import('${name}', import.meta.url);\n})()`
|
2022-09-09 10:52:27 +08:00
|
|
|
} else {
|
2024-04-01 18:26:00 +08:00
|
|
|
// CSSStyleSheet.replaceSync 需要FF v101, Safari 16.4才支持
|
|
|
|
fixedStyle +=
|
|
|
|
`{\n` +
|
|
|
|
` let stylesheet = new CSSStyleSheet();\n` +
|
|
|
|
` stylesheet.path = '${_name}';\n` +
|
|
|
|
` stylesheet.replaceSync(${tmp} );\n` +
|
|
|
|
` __sheets__.push(stylesheet);\n` +
|
|
|
|
` document.adoptedStyleSheets = __sheets__;\n` +
|
|
|
|
`}\n`
|
2024-09-13 18:36:44 +08:00
|
|
|
|
|
|
|
return `const ${tmp} = await __fite_import('${name}', import.meta.url)`
|
2023-05-18 12:04:24 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (name.startsWith('@/')) {
|
|
|
|
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
|
|
|
|
}
|
|
|
|
//
|
|
|
|
if (!imports[name]) {
|
|
|
|
if (name.startsWith('./') || name.startsWith('../')) {
|
|
|
|
if (IS_ENTRY) {
|
|
|
|
if (IS_MPA) {
|
|
|
|
name = `${currentPage}/` + name
|
|
|
|
}
|
|
|
|
name = urlJoin(DEPLOY_PATH, ASSETS_DIR, name)
|
2022-09-09 10:52:27 +08:00
|
|
|
}
|
2023-05-18 12:04:24 +08:00
|
|
|
} else if (
|
|
|
|
name.startsWith('/') &&
|
|
|
|
!name.startsWith('//') &&
|
|
|
|
!name.startsWith(urlJoin(DEPLOY_PATH, ASSETS_DIR))
|
|
|
|
) {
|
|
|
|
name = name.replace(/^\//, urlJoin(DEPLOY_PATH, ASSETS_DIR))
|
2022-09-09 10:52:27 +08:00
|
|
|
}
|
2023-02-12 23:01:57 +08:00
|
|
|
|
2023-05-30 10:19:41 +08:00
|
|
|
if (
|
|
|
|
!name.endsWith('.js') &&
|
|
|
|
!name.endsWith('.mjs') &&
|
|
|
|
!name.endsWith('.vue')
|
|
|
|
) {
|
2023-05-18 12:04:24 +08:00
|
|
|
name += '.js'
|
|
|
|
}
|
2022-09-09 10:52:27 +08:00
|
|
|
}
|
2023-05-18 12:04:24 +08:00
|
|
|
|
|
|
|
return `import '${name}'`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-07-25 16:30:30 +08:00
|
|
|
for (let key in define) {
|
|
|
|
code = code.replaceAll(key, define[key])
|
|
|
|
}
|
|
|
|
|
2023-05-18 12:04:24 +08:00
|
|
|
if (fixedStyle) {
|
2023-06-08 16:03:05 +08:00
|
|
|
code += '\n\n' + (IS_ENTRY ? SHEETS_DEF : '') + fixedStyle
|
2023-05-18 12:04:24 +08:00
|
|
|
|
|
|
|
if (IS_ENTRY && !LEGACY_MODE) {
|
|
|
|
code += '\ndocument.adoptedStyleSheets = __sheets__'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return code
|
2022-09-09 10:52:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 将vue转为js
|
|
|
|
* @param file <String> 文件路径
|
|
|
|
* @return <String> 返回转换后的js代码
|
|
|
|
*/
|
2024-07-25 15:24:06 +08:00
|
|
|
export async function compileVue(file, imports, options = {}) {
|
2023-05-18 12:04:24 +08:00
|
|
|
// 修正那反人类的windows路径
|
2023-03-01 00:30:07 +08:00
|
|
|
let filename = file.slice(options.SOURCE_DIR.length).replace(/\\/g, '/')
|
2023-02-23 00:00:10 +08:00
|
|
|
let code = (fs.cat(file) || '').toString().replace(/\r\n/g, '\n')
|
2023-01-31 19:17:38 +08:00
|
|
|
let CACHE = options.CACHE || {}
|
2023-05-22 15:06:33 +08:00
|
|
|
let { isCustomElement } = options
|
2023-02-24 15:45:55 +08:00
|
|
|
let output = '',
|
|
|
|
scoped = false
|
2022-09-09 10:52:27 +08:00
|
|
|
|
|
|
|
let js = code.match(JS_EXP)
|
2023-02-24 15:45:55 +08:00
|
|
|
let scss = [...code.matchAll(STYLE_EXP)]
|
2023-03-02 16:55:52 +08:00
|
|
|
let html = code.match(HTML_EXP) || ['', '']
|
2023-06-21 19:06:55 +08:00
|
|
|
let linePatch = code.slice(0, js?.index || 0).split('\n').length // js起始行数修正
|
2022-09-09 10:52:27 +08:00
|
|
|
|
2022-10-18 16:02:29 +08:00
|
|
|
let hash = md5(file)
|
2023-03-01 00:30:07 +08:00
|
|
|
let scopeId = 'data-' + hash
|
2022-09-09 10:52:27 +08:00
|
|
|
|
2023-02-24 15:45:55 +08:00
|
|
|
scss = scss
|
|
|
|
.map(it => {
|
|
|
|
let css
|
|
|
|
if (it.length > 2) {
|
2023-05-16 14:17:40 +08:00
|
|
|
css = compileScss(it[2], options.INJECT_SCSS)
|
2023-02-24 15:45:55 +08:00
|
|
|
|
|
|
|
if (it[1].includes('scoped')) {
|
|
|
|
scoped = true
|
|
|
|
css = scopeCss(css, hash)
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-16 14:17:40 +08:00
|
|
|
css = compileScss(it[1], options.INJECT_SCSS)
|
2023-02-24 15:45:55 +08:00
|
|
|
}
|
2024-07-25 15:24:06 +08:00
|
|
|
|
2023-02-24 15:45:55 +08:00
|
|
|
return css
|
|
|
|
})
|
|
|
|
.join(' ')
|
|
|
|
|
2024-07-25 15:24:06 +08:00
|
|
|
for (let fn of options.plugin) {
|
|
|
|
scss = await fn('css', scss)
|
|
|
|
}
|
|
|
|
|
2023-05-18 18:02:15 +08:00
|
|
|
js = js ? js[1] : 'export default {}'
|
2023-01-31 19:17:38 +08:00
|
|
|
|
2023-05-15 10:54:00 +08:00
|
|
|
try {
|
|
|
|
html = compile(html[1], {
|
|
|
|
mode: 'module',
|
|
|
|
prefixIdentifiers: true,
|
|
|
|
hoistStatic: true,
|
|
|
|
cacheHandlers: true,
|
|
|
|
scopeId: scoped ? scopeId : void 0,
|
|
|
|
sourceMap: false,
|
2023-05-22 15:06:33 +08:00
|
|
|
isCustomElement
|
2023-05-15 10:54:00 +08:00
|
|
|
}).code.replace('export function render', 'function render')
|
|
|
|
} catch (err) {
|
2023-06-21 17:10:32 +08:00
|
|
|
let lines = html[1].split('\n')
|
|
|
|
let line = lines[err.loc?.start.line - 1]
|
2023-05-15 10:54:00 +08:00
|
|
|
|
|
|
|
console.log('%s: %s', red('SyntaxError'), red(err.message))
|
|
|
|
console.log(
|
|
|
|
' @ %s%s%s',
|
|
|
|
line.slice(0, err.loc.start.column - 1),
|
|
|
|
red(err.loc.source),
|
|
|
|
line.slice(err.loc.end.column - 1)
|
|
|
|
)
|
|
|
|
console.log(
|
|
|
|
' @ (%s:%d:%d)\n',
|
|
|
|
file,
|
|
|
|
err.loc.start.line,
|
|
|
|
err.loc.start.column
|
|
|
|
)
|
|
|
|
html = `
|
|
|
|
import { openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
|
|
|
|
|
|
|
|
function render(_ctx, _cache) {
|
|
|
|
return (_openBlock(), _createElementBlock("div", null, "SyntaxError: ${err.message}"))
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
2023-02-24 15:45:55 +08:00
|
|
|
|
2023-05-11 11:37:50 +08:00
|
|
|
html = html
|
|
|
|
.replace(/import .* from "vue"/, str => {
|
|
|
|
output += str + '\n'
|
|
|
|
return ''
|
|
|
|
})
|
|
|
|
.trim()
|
2022-09-09 10:52:27 +08:00
|
|
|
|
2023-01-31 19:17:38 +08:00
|
|
|
if (CACHE[file]) {
|
|
|
|
CACHE[file] = {
|
|
|
|
changed: CACHE[file].js !== js || CACHE[file].html !== html,
|
|
|
|
js,
|
|
|
|
html
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CACHE[file] = { changed: false, js, html }
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:10:32 +08:00
|
|
|
output += parseJs(js, imports, options, file, linePatch).replace(
|
2022-09-09 10:52:27 +08:00
|
|
|
'export default {',
|
2023-05-18 12:04:24 +08:00
|
|
|
`\n${
|
|
|
|
options.LEGACY_MODE ? '' : SHEETS_DEF
|
|
|
|
}${html}\n\nconst __sfc__ = {\n render,\n`
|
2022-09-09 10:52:27 +08:00
|
|
|
)
|
|
|
|
|
2023-02-24 15:45:55 +08:00
|
|
|
if (scss) {
|
|
|
|
CACHE[file].css = scss
|
2022-10-18 16:02:29 +08:00
|
|
|
|
2023-05-18 12:04:24 +08:00
|
|
|
if (options.LEGACY_MODE) {
|
|
|
|
output += `
|
2023-05-11 11:37:50 +08:00
|
|
|
{
|
2023-05-18 12:04:24 +08:00
|
|
|
let stylesheet = document.createElement('style')
|
|
|
|
stylesheet.setAttribute('name', '${filename}')
|
|
|
|
stylesheet.textContent = \`${scss}\`
|
|
|
|
document.head.appendChild(stylesheet)
|
2023-05-11 11:37:50 +08:00
|
|
|
}
|
2023-03-01 00:30:07 +08:00
|
|
|
`
|
2023-05-18 12:04:24 +08:00
|
|
|
} else {
|
|
|
|
output += `
|
2024-04-01 18:26:00 +08:00
|
|
|
{
|
|
|
|
let stylesheet = new CSSStyleSheet()
|
|
|
|
stylesheet.path = '${filename}'
|
|
|
|
stylesheet.replaceSync(\`${scss}\`)
|
|
|
|
__sheets__.push(stylesheet)
|
|
|
|
}
|
|
|
|
document.adoptedStyleSheets = __sheets__
|
2023-05-18 12:04:24 +08:00
|
|
|
`
|
|
|
|
}
|
2023-03-01 00:30:07 +08:00
|
|
|
}
|
|
|
|
if (scoped) {
|
|
|
|
output += `__sfc__.__scopeId = '${scopeId}'\n`
|
2022-09-09 10:52:27 +08:00
|
|
|
}
|
2023-03-01 00:30:07 +08:00
|
|
|
output += `__sfc__.__file = '${filename}'\nexport default __sfc__`
|
2022-09-09 10:52:27 +08:00
|
|
|
|
2023-02-24 15:45:55 +08:00
|
|
|
return output
|
2022-09-09 10:52:27 +08:00
|
|
|
}
|
2022-10-11 19:31:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 解析模板html
|
|
|
|
*/
|
2023-06-26 16:39:45 +08:00
|
|
|
export function parseHtml(html, { page, imports, entry, LEGACY_MODE }) {
|
2022-10-11 19:31:04 +08:00
|
|
|
return html
|
2023-02-23 00:00:10 +08:00
|
|
|
.replace(/\r\n/g, '\n')
|
2022-10-11 19:31:04 +08:00
|
|
|
.replace(
|
|
|
|
'</head>',
|
2023-05-11 12:25:07 +08:00
|
|
|
`${
|
|
|
|
process.env.NODE_ENV === 'development'
|
2024-03-01 18:39:39 +08:00
|
|
|
? ` <script>${minify(createHmrScript(LEGACY_MODE))}</script>\n`
|
2024-04-01 18:26:00 +08:00
|
|
|
: ` <script>${minify(LEGACY_POLYFILL)}</script>\n`
|
2023-01-31 19:17:38 +08:00
|
|
|
}</head>`
|
2022-10-11 19:31:04 +08:00
|
|
|
)
|
|
|
|
.replace('{{title}}', page.title || '')
|
|
|
|
.replace('{{keywords}}', page.keywords || '')
|
|
|
|
.replace('{{description}}', page.description || '')
|
|
|
|
.replace('{{importmap}}', JSON.stringify({ imports }))
|
2023-01-13 11:40:53 +08:00
|
|
|
.replace(
|
|
|
|
'<script src="main.js"></script>',
|
|
|
|
`<script type="module">\n${entry}\n</script>`
|
|
|
|
)
|
2023-05-11 12:25:07 +08:00
|
|
|
.replace(/\{\{#if(.*?)\}\}([\w\W]*?)\{\{#\/if\}\}/g, (m, c, code) => {
|
|
|
|
let res = Function('return ' + c)()
|
|
|
|
return res ? code : ''
|
|
|
|
})
|
|
|
|
.replace(/process\.env\.NODE_ENV/g, `'${process.env.NODE_ENV}'`)
|
2022-10-11 19:31:04 +08:00
|
|
|
}
|