Compare commits

..

No commits in common. "d84a59a5e5141c41d02a6a59ce0df210d0d609b9" and "a15bd9ea950f8c034035d2fe3dee1cdfbff69fc0" have entirely different histories.

5 changed files with 39 additions and 52 deletions

View File

@ -159,6 +159,7 @@ export function parseJs(
if (name.startsWith('@/')) {
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
}
let isJson = name.endsWith('.json')
if (!imports[name]) {
if (name.startsWith('./') || name.startsWith('../')) {
@ -178,6 +179,7 @@ export function parseJs(
if (
!name.endsWith('.js') &&
!isJson &&
!name.endsWith('.mjs') &&
!name.endsWith('.vue')
) {
@ -200,9 +202,14 @@ export function parseJs(
_alias = ' default ' + alias.replace('* as', '').trim()
}
_import = `import ${alias} from '${name}'`
if (LEGACY_MODE && isJson) {
_import = `const ${alias} = await __fite_import('${name}', import.meta.url)`
} else {
_import = `import ${alias} from '${name}'${
isJson ? ' assert { type: "json" }' : ''
}`
}
_import += t === 'export' ? `\nexport ${_alias}` : ''
return _import
}
)
@ -213,9 +220,6 @@ export function parseJs(
if (name.startsWith('@/')) {
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
}
if (name.endsWith('.json')) {
name += '.js'
}
return `import('${name}')`
})
.replace(/import (["'])(.*?)\1/g, function (m, q, name) {
@ -231,28 +235,20 @@ export function parseJs(
let _name = name.replace(/\\/g, '/').replace('@/', '')
let tmp = `style_${uuid()}`
// 因为esm语法的变更, 原先的 import xx from xx assets {type: css} 变为了 with
// 而这个语法的变化, 构建工具无法做版本判断, 故, 统一降级到fetch()加载
if (LEGACY_MODE) {
fixedStyle +=
`{\n` +
` let stylesheet = document.createElement('style');\n` +
` stylesheet.setAttribute('name', '${_name}');\n` +
` stylesheet.textContent = ${tmp};\n` +
` document.head.appendChild(stylesheet);\n` +
`}\n`
fixedStyle += `${tmp}.then(r => {
let stylesheet = document.createElement('style')
stylesheet.setAttribute('name', '${_name}')
stylesheet.textContent = r
document.head.appendChild(stylesheet)
})
`
return `const ${tmp} = __fite_import('${name}', import.meta.url)`
} else {
// 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`
fixedStyle += `${tmp}.path = '${_name}'\n__sheets__.push(${tmp})\n`
return `import ${tmp} from '${name}' assert { type: 'css' }`
}
return `const ${tmp} = await __fite_import('${name}', import.meta.url)`
} else {
if (name.startsWith('@/')) {
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
@ -276,6 +272,7 @@ export function parseJs(
if (
!name.endsWith('.js') &&
!name.endsWith('.json') &&
!name.endsWith('.mjs') &&
!name.endsWith('.vue')
) {
@ -413,13 +410,13 @@ function render(_ctx, _cache) {
`
} else {
output += `
{
{
let stylesheet = new CSSStyleSheet()
stylesheet.path = '${filename}'
stylesheet.replaceSync(\`${scss}\`)
__sheets__.push(stylesheet)
}
document.adoptedStyleSheets = __sheets__
}
document.adoptedStyleSheets = __sheets__
`
}
}
@ -442,7 +439,9 @@ export function parseHtml(html, { page, imports, entry, LEGACY_MODE }) {
`${
process.env.NODE_ENV === 'development'
? ` <script>${minify(createHmrScript(LEGACY_MODE))}</script>\n`
: ` <script>${minify(LEGACY_POLYFILL)}</script>\n`
: LEGACY_MODE
? ` <script>${minify(LEGACY_POLYFILL)}</script>\n`
: ''
}</head>`
)
.replace('{{title}}', page.title || '')

View File

@ -66,15 +66,14 @@ export async function compileFiles(
})
}
break
// es2024之后esm的语法的assets 变成了with, 对构建工具来说无法适配到具体的浏览器
// 故把json文件改成js文件
case '.json':
{
let code = fs.cat(path)
code = 'export default ' + JSON.stringify(JSON.parse(code + ''))
code = JSON.stringify(JSON.parse(code + ''))
fs.echo(code, join(dist, 'assets/', pageDir, it.name + '.js'))
fs.echo(code, join(dist, 'assets/', pageDir, it.name))
}
break

View File

@ -40,11 +40,12 @@ export const LEGACY_POLYFILL = `!(function(){
}
window.__fite_import = function(url,relPath){
let isJson = url.endsWith('.json')
let absPath = relPath.split('/').slice(0, -1).join('/')
let req
if(url.startsWith('./') || url.startsWith('../')) {
url = join(absPath, url)
}
return window.fetch(url).then(r => r.text())
return window.fetch(url).then(r => isJson ? r.json() : r.text())
}
})()`

View File

@ -255,16 +255,11 @@ export default async function createServer(root = '', conf = {}) {
}
break
case 'json':
case 'js':
{
let rpath = pathname.replace('@/', '')
let file
let isJson = false
if (rpath.endsWith('json.js')) {
isJson = true
rpath = rpath.slice(0, -3)
}
if (IS_MPA) {
// 判断前后2个值相等, 避免出现目录名和页面名字相同时走错逻辑
@ -288,14 +283,7 @@ export default async function createServer(root = '', conf = {}) {
res.end('')
return
}
if (isJson) {
try {
code =
'export default ' + JSON.stringify(JSON.parse(code + ''))
} catch (err) {
console.log('%s 语法错误: %s', rpath, red(err.message))
}
} else {
if (ext === 'js') {
code = parseJs(
code + '',
conf.imports,

View File

@ -1,7 +1,7 @@
{
"name": "fite",
"type": "module",
"version": "1.3.1",
"version": "1.3.0",
"bin": {
"fite": "index.js"
},