From 7ed5f81da228c4f9d77b591e61a63a387cbf5570 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 17 May 2023 19:15:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0legacy=E6=A8=A1=E5=BC=8F,=20?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E4=B8=8D=E6=94=AF=E6=8C=81import=20assertion?= =?UTF-8?q?s=E7=9A=84=E6=B5=8F=E8=A7=88=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile-vue.js | 32 +++++++++++++++++++++----------- lib/dev.js | 13 +++++++++---- lib/prod.js | 10 +++++++++- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index bbadd55..fe2742a 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -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 }) { '', `${ process.env.NODE_ENV === 'development' - ? ` \n` + ? ` \n` : '' }` ) diff --git a/lib/dev.js b/lib/dev.js index c2addd8..32bdc6f 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -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) { diff --git a/lib/prod.js b/lib/prod.js index 25accf8..1f13aed 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -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) { // 入口文件, 特殊处理