From 0225e1e499afbeb97278b49353b6a75e210ac802 Mon Sep 17 00:00:00 2001 From: yutent Date: Sun, 25 Jun 2023 11:59:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E5=A4=84?= =?UTF-8?q?=E7=AC=94=E8=AF=AF;=20=E4=BF=AE=E6=AD=A3js=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E8=A1=8C=E5=8F=B7;=E4=BF=AE=E5=A4=8Djs=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E6=97=B6=E6=96=87=E4=BB=B6=E5=90=8D=E6=9C=AA=E4=BC=A0=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile-vue.js | 2 +- lib/dev.js | 40 +++++++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/compile-vue.js b/lib/compile-vue.js index 3fae656..ca0927f 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -112,7 +112,7 @@ export function parseJs( imports, { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE } = {}, filename, - linePatch = 0 + linePatch = 1 ) { let fixedStyle = '' let ASSETS_DIR = '/@/' diff --git a/lib/dev.js b/lib/dev.js index 2ab47dd..1bc2b4b 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -166,13 +166,18 @@ export default async function createServer(root = '', conf = {}) { let entry = fs.cat(page.entry)?.toString() let html = fs.cat(join(process.cwd(), 'index.html')).toString() - entry = parseJs(entry, conf.imports, { - IS_MPA, - currentPage, - IS_ENTRY: true, - DEPLOY_PATH, - LEGACY_MODE - }) + entry = parseJs( + entry, + conf.imports, + { + IS_MPA, + currentPage, + IS_ENTRY: true, + DEPLOY_PATH, + LEGACY_MODE + }, + page.entry + ) code = parseHtml(html, { page, @@ -198,7 +203,6 @@ export default async function createServer(root = '', conf = {}) { file = join(SOURCE_DIR, rpath) } } else { - ndex.html file = join(SOURCE_DIR, rpath) } if (!fs.isfile(file)) { @@ -267,19 +271,25 @@ export default async function createServer(root = '', conf = {}) { if (fs.isfile(file)) { code = fs.cat(file) } else if (fs.isfile(join(PUBLIC_DIR, rpath))) { - code = fs.cat(join(PUBLIC_DIR, rpath)) + file = join(PUBLIC_DIR, rpath) + code = fs.cat(file) } else { friendlyErrors(rpath, ext) res.writeHead(404, USE_HTTPS ? void 0 : 'Not Found') res.end('') return } - code = parseJs(code + '', conf.imports, { - IS_MPA, - currentPage, - DEPLOY_PATH, - LEGACY_MODE - }) + code = parseJs( + code + '', + conf.imports, + { + IS_MPA, + currentPage, + DEPLOY_PATH, + LEGACY_MODE + }, + file + ) res.setHeader('content-type', MIME_TYPES.js) } -- 2.40.1 From da7649d3620aea42c2517d7a92c6f666799695a4 Mon Sep 17 00:00:00 2001 From: yutent Date: Sun, 25 Jun 2023 12:27:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=95=E9=A1=B5?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E7=9A=84=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile.js | 3 +-- lib/prod.js | 42 +++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/compile.js b/lib/compile.js index 5b3509c..f982d35 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -18,6 +18,7 @@ export async function compileFiles( options, { verbose, imports, dist } = {} ) { + let pageDir = options.IS_MPA && currentPage ? `pages/${currentPage}` : '' options.currentPage = currentPage for (let [path, it] of files) { @@ -40,8 +41,6 @@ export async function compileFiles( verbose && console.log(' 解析 %s ...', it.name) - let pageDir = options.IS_MPA && currentPage ? `pages/${currentPage}` : '' - switch (it.ext) { case '.vue': { diff --git a/lib/prod.js b/lib/prod.js index 6991cdc..3247c94 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -74,22 +74,6 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { : isCustomElement || defaultCustomElement } - // 创建线程池 - if (THREADS_NUM > 0) { - for (let i = 0; i < THREADS_NUM; i++) { - WORKER_POOL.add( - new Worker(join(__dirname, './thread.js'), { - workerData: { - options, - verbose, - dist, - imports: conf.imports - } - }) - ) - } - } - fs.ls(SOURCE_DIR, true).forEach(path => { if (fs.isdir(path)) { return @@ -108,14 +92,29 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { return } - if (it.path === conf.inject.scss) { + if (path === conf.inject.scss) { return } - list.set(path, it) } }) + // 创建线程池 + if (THREADS_NUM > 0 && (IS_MPA || list.size > THREADS_NUM * 10)) { + for (let i = 0; i < THREADS_NUM; i++) { + WORKER_POOL.add( + new Worker(join(__dirname, './thread.js'), { + workerData: { + options, + verbose, + dist, + imports: conf.imports + } + }) + ) + } + } + // 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件 if (fs.isdir(PUBLIC_DIR)) { console.log('\n正在处理静态资源 ...') @@ -152,6 +151,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { list.delete(path) files.set(path, { name, ext }) }) + if (THREADS_NUM > 0) { chunk.set(currentPage, { page, files }) JOBS_QUEUE.push(chunk) @@ -187,11 +187,10 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { let currentPage = PAGES_KEYS[0] let page = conf.pages[currentPage] - list = [...list] - console.log(`正在生成 ${currentPage}.html ...`) - if (THREADS_NUM > 0) { + if (THREADS_NUM > 0 && list.size > THREADS_NUM * 10) { + list = [...list] for (let i = 0; i < THREADS_NUM; i++) { let start = i * chunkSize let end = start + chunkSize @@ -203,6 +202,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { doJob() } } else { + options.isCustomElement = isCustomElement || defaultCustomElement compileFiles(currentPage, page, list, options, { verbose, dist, -- 2.40.1