Compare commits

...

2 Commits

Author SHA1 Message Date
yutent d84a59a5e5 1.3.4 2024-04-01 18:26:19 +08:00
yutent 66925dfbf2 适配es2024搞出来的ESM语法变化 2024-04-01 18:26:00 +08:00
5 changed files with 52 additions and 39 deletions

View File

@ -159,7 +159,6 @@ 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('../')) {
@ -179,7 +178,6 @@ export function parseJs(
if (
!name.endsWith('.js') &&
!isJson &&
!name.endsWith('.mjs') &&
!name.endsWith('.vue')
) {
@ -202,14 +200,9 @@ export function parseJs(
_alias = ' default ' + alias.replace('* as', '').trim()
}
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 = `import ${alias} from '${name}'`
_import += t === 'export' ? `\nexport ${_alias}` : ''
return _import
}
)
@ -220,6 +213,9 @@ 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) {
@ -235,20 +231,28 @@ 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 += `${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)`
fixedStyle +=
`{\n` +
` let stylesheet = document.createElement('style');\n` +
` stylesheet.setAttribute('name', '${_name}');\n` +
` stylesheet.textContent = ${tmp};\n` +
` document.head.appendChild(stylesheet);\n` +
`}\n`
} else {
fixedStyle += `${tmp}.path = '${_name}'\n__sheets__.push(${tmp})\n`
return `import ${tmp} from '${name}' assert { type: 'css' }`
// 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`
}
return `const ${tmp} = await __fite_import('${name}', import.meta.url)`
} else {
if (name.startsWith('@/')) {
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
@ -272,7 +276,6 @@ export function parseJs(
if (
!name.endsWith('.js') &&
!name.endsWith('.json') &&
!name.endsWith('.mjs') &&
!name.endsWith('.vue')
) {
@ -439,9 +442,7 @@ export function parseHtml(html, { page, imports, entry, LEGACY_MODE }) {
`${
process.env.NODE_ENV === 'development'
? ` <script>${minify(createHmrScript(LEGACY_MODE))}</script>\n`
: LEGACY_MODE
? ` <script>${minify(LEGACY_POLYFILL)}</script>\n`
: ''
: ` <script>${minify(LEGACY_POLYFILL)}</script>\n`
}</head>`
)
.replace('{{title}}', page.title || '')

View File

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

View File

@ -40,12 +40,11 @@ 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 => isJson ? r.json() : r.text())
return window.fetch(url).then(r => r.text())
}
})()`

View File

@ -255,11 +255,16 @@ 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个值相等, 避免出现目录名和页面名字相同时走错逻辑
@ -283,7 +288,14 @@ export default async function createServer(root = '', conf = {}) {
res.end('')
return
}
if (ext === 'js') {
if (isJson) {
try {
code =
'export default ' + JSON.stringify(JSON.parse(code + ''))
} catch (err) {
console.log('%s 语法错误: %s', rpath, red(err.message))
}
} else {
code = parseJs(
code + '',
conf.imports,

View File

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