From ea986f0a392e6ba4edd511d0df6fbc5e1e2c565f Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 16 Jun 2023 14:19:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile.js | 14 ++++++------ lib/dev.js | 7 +++--- lib/prod.js | 61 +++++++++++++++++++++----------------------------- lib/thread.js | 16 ++++++++----- lib/utils.js | 3 ++- 5 files changed, 49 insertions(+), 52 deletions(-) diff --git a/lib/compile.js b/lib/compile.js index 5c5b560..5b3509c 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -20,9 +20,9 @@ export async function compileFiles( ) { options.currentPage = currentPage - for (let [k, it] of files) { + for (let [path, it] of files) { // 入口文件, 特殊处理 - if (page && it.path === page.entry) { + if (page && path === page.entry) { let entry = fs.cat(page.entry).toString() entry = parseJs(entry, imports, { ...options, IS_ENTRY: true }) @@ -45,7 +45,7 @@ export async function compileFiles( switch (it.ext) { case '.vue': { - let code = compileVue(it.path, imports, options) + let code = compileVue(path, imports, options) await Es.transform(code, { minify: true }).then(r => { fs.echo( @@ -58,7 +58,7 @@ export async function compileFiles( case '.js': { - let code = fs.cat(it.path) + let code = fs.cat(path) code = parseJs(code + '', imports, options) @@ -78,16 +78,16 @@ export async function compileFiles( it.name.replace(/\.scss$/, '.css') ) if (it.ext === '.css') { - fs.cp(it.path, target) + fs.cp(path, target) } else { - let code = compileScss(it.path) + let code = compileScss(path) fs.echo(code, target) } } break default: - fs.cp(it.path, join(dist, 'assets/', pageDir, it.name)) + fs.cp(path, join(dist, 'assets/', pageDir, it.name)) break } } diff --git a/lib/dev.js b/lib/dev.js index 1756197..2ab47dd 100644 --- a/lib/dev.js +++ b/lib/dev.js @@ -7,7 +7,7 @@ import socket from './ws.js' import chokidar from 'chokidar' import { red } from 'kolorist' -import { friendlyErrors, isCustomElement, md5, gzip } from './utils.js' +import { friendlyErrors, defaultCustomElement, md5, gzip } from './utils.js' import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' @@ -33,6 +33,7 @@ export default async function createServer(root = '', conf = {}) { const INJECT_SCSS = readFile(conf.inject?.scss) const LEGACY_MODE = !!conf.legacy const ENABLE_GZIP = !!conf.devServer.gzip + const { isCustomElement = defaultCustomElement } = conf.compileOptions || {} if (conf.imports['vue-dev']) { conf.imports.vue = conf.imports['vue-dev'] @@ -215,7 +216,7 @@ export default async function createServer(root = '', conf = {}) { DEPLOY_PATH, INJECT_SCSS, LEGACY_MODE, - isCustomElement: conf.isCustomElement || isCustomElement + isCustomElement }) res.setHeader('content-type', MIME_TYPES.js) @@ -395,7 +396,7 @@ export default async function createServer(root = '', conf = {}) { DEPLOY_PATH, INJECT_SCSS, LEGACY_MODE, - isCustomElement: conf.isCustomElement || isCustomElement + isCustomElement }) let tmp = CACHE[filePath] if (tmp.changed) { diff --git a/lib/prod.js b/lib/prod.js index 48bbae0..1c1151b 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -48,6 +48,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { ) const INJECT_SCSS = readFile(conf.inject?.scss) const LEGACY_MODE = !!conf.legacy + const { isCustomElement } = conf.compileOptions || {} conf.inject = conf.inject || { scss: '' } @@ -61,7 +62,21 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { INJECT_SCSS, LEGACY_MODE, // 线程通讯无法传递函数类型, 需要转为字符串, 之后再转回来 - isCustomElement: conf.isCustomElement + isCustomElement: isCustomElement ? isCustomElement.toString() : null + } + + // 创建线程池 + 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 => { @@ -71,11 +86,10 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { let name = path.slice(SOURCE_DIR.length + 1) let it = { - path, name, ext: parse(name).ext } - if (it.ext !== '') { + if (it.ext) { if (IS_MPA && it.name.startsWith(PREFIX)) { if (PAGES_PREFIX.some(it => it.startsWith(it.name))) { list.set(path, it) @@ -97,11 +111,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { fs.ls(PUBLIC_DIR, true).forEach(it => { let ext = parse(it).ext - if (ext === '') { - return - } - - if (fs.isfile(it)) { + if (ext && fs.isfile(it)) { let name = it.slice(PUBLIC_DIR.length + 1) verbose && console.log(' 复制 %s ...', name) fs.cp(it, join(dist, name)) @@ -109,20 +119,6 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { }) } - // 创建线程池 - 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 (IS_MPA) { for (let currentPage of PAGES_KEYS) { let page = conf.pages[currentPage] @@ -143,18 +139,18 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { } list.delete(path) - files.set(path, { name, path, ext }) + files.set(path, { name, ext }) }) chunk.set(currentPage, { page, files }) JOBS_QUEUE.push(chunk) doJob() } - // + // 公共依赖 { let chunk = new Map() - chunk.set('', { page: null, files: list }) + JOBS_QUEUE.push(chunk) doJob() } @@ -166,6 +162,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { list = [...list] + console.log(`正在生成 ${currentPage}.html ...`) + for (let i = 0; i < THREADS_NUM; i++) { let start = i * chunkSize let end = start + chunkSize @@ -173,17 +171,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { chunk.set(currentPage, { page, files: list.slice(start, end) }) - new Worker(join(__dirname, './thread.js'), { - workerData: { - options, - data: { - chunk, - verbose, - dist, - imports: conf.imports - } - } - }) + JOBS_QUEUE.push(chunk) + doJob() } } diff --git a/lib/thread.js b/lib/thread.js index 43f35e4..1f9fad3 100644 --- a/lib/thread.js +++ b/lib/thread.js @@ -5,18 +5,24 @@ */ import { parentPort, workerData } from 'node:worker_threads' import { compileFiles } from './compile.js' -import { isCustomElement } from './utils.js' +import { defaultCustomElement } from './utils.js' const { options, verbose, dist, imports } = workerData -options.isCustomElement = Function('return ' + options.isCustomElement)() +options.isCustomElement = options.isCustomElement + ? Function('return ' + options.isCustomElement)() + : defaultCustomElement async function doJob(job) { let [currentPage, { page, files }] = job.entries().next().value - console.log( - currentPage ? `正在生成 ${currentPage}.html ...` : '\n正在解析公共依赖 ...' - ) + options.IS_MPA && + console.log( + currentPage + ? `正在生成 ${currentPage}.html ...` + : '\n正在解析公共依赖 ...' + ) + await compileFiles(currentPage, page, files, options, { verbose, dist, diff --git a/lib/utils.js b/lib/utils.js index 9919451..b2c8808 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -94,6 +94,7 @@ export function createHmrScript(legacy, session = '') { ` } -export function isCustomElement(tag) { +// 默认的 web components 判断 +export function defaultCustomElement(tag) { return tag.startsWith('wc-') }