增加legacy模式, 兼容不支持import assertions的浏览器
parent
5cd075c46e
commit
7ed5f81da2
|
@ -123,7 +123,7 @@ export function compileScss(file, inject = '') {
|
||||||
export function parseJs(
|
export function parseJs(
|
||||||
code = '',
|
code = '',
|
||||||
imports,
|
imports,
|
||||||
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH } = {},
|
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE } = {},
|
||||||
filename
|
filename
|
||||||
) {
|
) {
|
||||||
let fixedStyle = '\n\n'
|
let fixedStyle = '\n\n'
|
||||||
|
@ -215,14 +215,24 @@ export function parseJs(
|
||||||
if (isBuild) {
|
if (isBuild) {
|
||||||
name = name.replace(/\.scss/, '.css')
|
name = name.replace(/\.scss/, '.css')
|
||||||
}
|
}
|
||||||
let tmp = `style_${uuid()}`
|
|
||||||
fixedStyle += `__sheets__.push(${tmp})\n`
|
|
||||||
|
|
||||||
// 修正那反人类的windows路径
|
// 修正那反人类的windows路径
|
||||||
return `import ${tmp} from '${name}' assert { type: 'css' }\n${tmp}.path = '${name.replace(
|
let _name = name.replace(/\\/g, '/')
|
||||||
/\\/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 {
|
} else {
|
||||||
if (name.startsWith('@/')) {
|
if (name.startsWith('@/')) {
|
||||||
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
|
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
|
||||||
|
@ -385,9 +395,9 @@ export function parseHtml(html, { page, imports, entry }) {
|
||||||
'</head>',
|
'</head>',
|
||||||
`${
|
`${
|
||||||
process.env.NODE_ENV === 'development'
|
process.env.NODE_ENV === 'development'
|
||||||
? ` <script>${
|
? ` <script>${Es.transformSync(HMR_SCRIPT, {
|
||||||
Es.transformSync(HMR_SCRIPT, { minify: true }).code.trim()
|
minify: true
|
||||||
}</script>\n`
|
}).code.trim()}</script>\n`
|
||||||
: ''
|
: ''
|
||||||
}</head>`
|
}</head>`
|
||||||
)
|
)
|
||||||
|
|
13
lib/dev.js
13
lib/dev.js
|
@ -31,6 +31,7 @@ export default async function createServer(root = '', conf = {}) {
|
||||||
const USE_HTTPS = conf.devServer.https
|
const USE_HTTPS = conf.devServer.https
|
||||||
const DOMAIN = conf.devServer.domain || 'localhost'
|
const DOMAIN = conf.devServer.domain || 'localhost'
|
||||||
const INJECT_SCSS = readFile(conf.inject?.scss)
|
const INJECT_SCSS = readFile(conf.inject?.scss)
|
||||||
|
const LEGACY_MODE = !!conf.legacy
|
||||||
|
|
||||||
if (USE_HTTPS) {
|
if (USE_HTTPS) {
|
||||||
Object.assign(SERVER_OPTIONS, conf.devServer.ssl)
|
Object.assign(SERVER_OPTIONS, conf.devServer.ssl)
|
||||||
|
@ -159,7 +160,8 @@ export default async function createServer(root = '', conf = {}) {
|
||||||
IS_MPA,
|
IS_MPA,
|
||||||
currentPage,
|
currentPage,
|
||||||
IS_ENTRY: true,
|
IS_ENTRY: true,
|
||||||
DEPLOY_PATH
|
DEPLOY_PATH,
|
||||||
|
LEGACY_MODE
|
||||||
})
|
})
|
||||||
|
|
||||||
code = parseHtml(html, { page, imports: conf.imports, entry })
|
code = parseHtml(html, { page, imports: conf.imports, entry })
|
||||||
|
@ -195,7 +197,8 @@ export default async function createServer(root = '', conf = {}) {
|
||||||
SOURCE_DIR,
|
SOURCE_DIR,
|
||||||
CACHE,
|
CACHE,
|
||||||
DEPLOY_PATH,
|
DEPLOY_PATH,
|
||||||
INJECT_SCSS
|
INJECT_SCSS,
|
||||||
|
LEGACY_MODE
|
||||||
})
|
})
|
||||||
|
|
||||||
res.setHeader('content-type', MIME_TYPES.js)
|
res.setHeader('content-type', MIME_TYPES.js)
|
||||||
|
@ -256,7 +259,8 @@ export default async function createServer(root = '', conf = {}) {
|
||||||
code = parseJs(code + '', conf.imports, {
|
code = parseJs(code + '', conf.imports, {
|
||||||
IS_MPA,
|
IS_MPA,
|
||||||
currentPage,
|
currentPage,
|
||||||
DEPLOY_PATH
|
DEPLOY_PATH,
|
||||||
|
LEGACY_MODE
|
||||||
})
|
})
|
||||||
res.setHeader('content-type', MIME_TYPES.js)
|
res.setHeader('content-type', MIME_TYPES.js)
|
||||||
}
|
}
|
||||||
|
@ -365,7 +369,8 @@ export default async function createServer(root = '', conf = {}) {
|
||||||
SOURCE_DIR,
|
SOURCE_DIR,
|
||||||
CACHE,
|
CACHE,
|
||||||
DEPLOY_PATH,
|
DEPLOY_PATH,
|
||||||
INJECT_SCSS
|
INJECT_SCSS,
|
||||||
|
LEGACY_MODE
|
||||||
})
|
})
|
||||||
let tmp = CACHE[filePath]
|
let tmp = CACHE[filePath]
|
||||||
if (tmp.changed) {
|
if (tmp.changed) {
|
||||||
|
|
10
lib/prod.js
10
lib/prod.js
|
@ -15,6 +15,7 @@ export default function compile(root = '', dist = '', conf = {}) {
|
||||||
const IS_MPA = Object.keys(conf.pages).length > 1
|
const IS_MPA = Object.keys(conf.pages).length > 1
|
||||||
const PAGES_PREFIX = Object.keys(conf.pages).map(it => `pages/${it}/`)
|
const PAGES_PREFIX = Object.keys(conf.pages).map(it => `pages/${it}/`)
|
||||||
const INJECT_SCSS = readFile(conf.inject?.scss)
|
const INJECT_SCSS = readFile(conf.inject?.scss)
|
||||||
|
const LEGACY_MODE = !!conf.legacy
|
||||||
|
|
||||||
let timeStart = Date.now()
|
let timeStart = Date.now()
|
||||||
let template = fs.cat(join(process.env.PWD, 'index.html')).toString()
|
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 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) {
|
for (let it of files) {
|
||||||
// 入口文件, 特殊处理
|
// 入口文件, 特殊处理
|
||||||
|
|
Loading…
Reference in New Issue