增加legacy模式, 兼容不支持import assertions的浏览器

pull/1/head
yutent 2023-05-17 19:15:51 +08:00
parent 5cd075c46e
commit 7ed5f81da2
3 changed files with 39 additions and 16 deletions

View File

@ -123,7 +123,7 @@ export function compileScss(file, inject = '') {
export function parseJs(
code = '',
imports,
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH } = {},
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE } = {},
filename
) {
let fixedStyle = '\n\n'
@ -215,14 +215,24 @@ export function parseJs(
if (isBuild) {
name = name.replace(/\.scss/, '.css')
}
let tmp = `style_${uuid()}`
fixedStyle += `__sheets__.push(${tmp})\n`
// 修正那反人类的windows路径
return `import ${tmp} from '${name}' assert { type: 'css' }\n${tmp}.path = '${name.replace(
/\\/g,
'/'
)}'`
let _name = name.replace(/\\/g, '/')
let tmp = `style_${uuid()}`
if (LEGACY_MODE) {
fixedStyle += `${tmp}.then(r => {
let stylesheet = new CSSStyleSheet()
stylesheet.path = '${_name}'
stylesheet.replaceSync(r)
__sheets__.push(stylesheet)
})
`
return `${tmp} = window.fetch('${name}').then(r => r.text())`
} else {
fixedStyle += `${tmp}.path = '${_name}'\n__sheets__.push(${tmp})\n`
return `import ${tmp} from '${name}' assert { type: 'css' }`
}
} else {
if (name.startsWith('@/')) {
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
@ -385,9 +395,9 @@ export function parseHtml(html, { page, imports, entry }) {
'</head>',
`${
process.env.NODE_ENV === 'development'
? ` <script>${
Es.transformSync(HMR_SCRIPT, { minify: true }).code.trim()
}</script>\n`
? ` <script>${Es.transformSync(HMR_SCRIPT, {
minify: true
}).code.trim()}</script>\n`
: ''
}</head>`
)

View File

@ -31,6 +31,7 @@ export default async function createServer(root = '', conf = {}) {
const USE_HTTPS = conf.devServer.https
const DOMAIN = conf.devServer.domain || 'localhost'
const INJECT_SCSS = readFile(conf.inject?.scss)
const LEGACY_MODE = !!conf.legacy
if (USE_HTTPS) {
Object.assign(SERVER_OPTIONS, conf.devServer.ssl)
@ -159,7 +160,8 @@ export default async function createServer(root = '', conf = {}) {
IS_MPA,
currentPage,
IS_ENTRY: true,
DEPLOY_PATH
DEPLOY_PATH,
LEGACY_MODE
})
code = parseHtml(html, { page, imports: conf.imports, entry })
@ -195,7 +197,8 @@ export default async function createServer(root = '', conf = {}) {
SOURCE_DIR,
CACHE,
DEPLOY_PATH,
INJECT_SCSS
INJECT_SCSS,
LEGACY_MODE
})
res.setHeader('content-type', MIME_TYPES.js)
@ -256,7 +259,8 @@ export default async function createServer(root = '', conf = {}) {
code = parseJs(code + '', conf.imports, {
IS_MPA,
currentPage,
DEPLOY_PATH
DEPLOY_PATH,
LEGACY_MODE
})
res.setHeader('content-type', MIME_TYPES.js)
}
@ -365,7 +369,8 @@ export default async function createServer(root = '', conf = {}) {
SOURCE_DIR,
CACHE,
DEPLOY_PATH,
INJECT_SCSS
INJECT_SCSS,
LEGACY_MODE
})
let tmp = CACHE[filePath]
if (tmp.changed) {

View File

@ -15,6 +15,7 @@ export default function compile(root = '', dist = '', conf = {}) {
const IS_MPA = Object.keys(conf.pages).length > 1
const PAGES_PREFIX = Object.keys(conf.pages).map(it => `pages/${it}/`)
const INJECT_SCSS = readFile(conf.inject?.scss)
const LEGACY_MODE = !!conf.legacy
let timeStart = Date.now()
let template = fs.cat(join(process.env.PWD, 'index.html')).toString()
@ -43,7 +44,14 @@ export default function compile(root = '', dist = '', conf = {}) {
})
let compileFiles = function (currentPage, page, files) {
let options = { IS_MPA, currentPage, SOURCE_DIR, DEPLOY_PATH, INJECT_SCSS }
let options = {
IS_MPA,
currentPage,
SOURCE_DIR,
DEPLOY_PATH,
INJECT_SCSS,
LEGACY_MODE
}
for (let it of files) {
// 入口文件, 特殊处理