From c675b73f251982f80c6fa25d39c3f03eaec71e05 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 14 Jun 2023 19:36:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=BA=BF=E7=A8=8B=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compile-vue.js | 3 +- lib/compile.js | 92 ++++++++++++++++++++ lib/prod.js | 212 +++++++++++++++++++++------------------------ lib/thread.js | 24 +++++ 4 files changed, 217 insertions(+), 114 deletions(-) create mode 100644 lib/compile.js create mode 100644 lib/thread.js diff --git a/lib/compile-vue.js b/lib/compile-vue.js index f7deb8e..844ca43 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -317,8 +317,9 @@ export function compileVue(file, imports, options = {}) { isCustomElement }).code.replace('export function render', 'function render') } catch (err) { + // console.log(err) let tmp = html[1].split('\n') - let line = tmp[err.loc.start.line - 1] + let line = tmp[err.loc?.start.line - 1] console.log('%s: %s', red('SyntaxError'), red(err.message)) console.log( diff --git a/lib/compile.js b/lib/compile.js new file mode 100644 index 0000000..6f45b21 --- /dev/null +++ b/lib/compile.js @@ -0,0 +1,92 @@ +/** + * {} + * @author yutent + * @date 2023/06/14 18:36:15 + */ + +import { join, dirname, parse, normalize } from 'node:path' +import fs from 'iofs' +import Es from 'esbuild' +import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' + +const template = fs.cat(join(process.cwd(), 'index.html')).toString() + +export function compileFiles( + currentPage, + page, + files, + options, + { verbose, imports, dist } = {} +) { + options.currentPage = currentPage + + console.log('正在生成 %s ...', `${currentPage}.html`) + + for (let [k, it] of files) { + // let it = files[k] + // 入口文件, 特殊处理 + if (page && it.path === page.entry) { + let entry = fs.cat(page.entry).toString() + + entry = parseJs(entry, imports, { ...options, IS_ENTRY: true }) + + let code = parseHtml(template, { + page, + imports, + entry, + LEGACY_MODE: options.LEGACY_MODE + }) + + fs.echo(code, join(dist, `${currentPage}.html`)) + continue + } + + verbose && console.log(' 解析 %s ...', it.name) + + let pageDir = options.IS_MPA && currentPage ? `pages/${currentPage}` : '' + + switch (it.ext) { + case '.vue': + { + let code = compileVue(it.path, imports, options) + + Es.transform(code, { minify: true }).then(r => { + fs.echo( + r.code, + join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js')) + ) + }) + } + break + + case '.js': + { + let code = fs.cat(it.path) + + code = parseJs(code + '', imports, options) + + Es.transform(code, { minify: true }).then(r => { + fs.echo(r.code, join(dist, 'assets/', pageDir, it.name)) + }) + } + break + + case '.scss': + case '.css': + { + let target = join(dist, 'assets/', it.name.replace(/\.scss$/, '.css')) + if (it.ext === '.css') { + fs.cp(it.path, target) + } else { + let code = compileScss(it.path) + fs.echo(code, target) + } + } + break + + default: + fs.cp(it.path, join(dist, it.name)) + break + } + } +} diff --git a/lib/prod.js b/lib/prod.js index fda4a0c..fc5cfc2 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -1,11 +1,16 @@ -import { join, dirname, parse } from 'node:path' +import { join, dirname, parse, normalize } from 'node:path' +import { Worker } from 'node:worker_threads' +import os from 'node:os' import fs from 'iofs' -import Es from 'esbuild' -import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' +// import Es from 'esbuild' +// import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' import { isCustomElement } from './utils.js' +import { compileFiles } from './compile.js' const IS_WIN = process.platform === 'win32' const PREFIX = IS_WIN ? 'pages\\' : 'pages/' +const THREADS_NUM = os.cpus().length +const __dirname = normalize(dirname(import.meta.url.slice(IS_WIN ? 8 : 7))) function readFile(file) { return (file && fs.cat(file)?.toString()) || '' @@ -16,8 +21,9 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { const SOURCE_DIR = join(root, 'src') const PUBLIC_DIR = join(root, 'public') const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署 - const IS_MPA = Object.keys(conf.pages).length > 1 - const PAGES_PREFIX = Object.keys(conf.pages).map(it => + const PAGES_KEYS = Object.keys(conf.pages) + const IS_MPA = PAGES_KEYS.length > 1 + const PAGES_PREFIX = PAGES_KEYS.map(it => IS_WIN ? `${PREFIX + it}\\` : `${PREFIX + it}/` ) const INJECT_SCSS = readFile(conf.inject?.scss) @@ -27,7 +33,17 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { let timeStart = Date.now() let template = fs.cat(join(process.cwd(), 'index.html')).toString() - let list = {} + let list = new Map() + let options = { + IS_MPA, + SOURCE_DIR, + DEPLOY_PATH, + INJECT_SCSS, + LEGACY_MODE, + // 线程通讯无法传递函数类型, 需要转为字符串, 之后再转回来 + // isCustomElement: conf.isCustomElement || isCustomElement + isCustomElement: (conf.isCustomElement || isCustomElement).toString() + } fs.ls(SOURCE_DIR, true).forEach(path => { if (fs.isdir(path)) { @@ -43,7 +59,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { if (it.ext !== '') { if (IS_MPA && it.name.startsWith(PREFIX)) { if (PAGES_PREFIX.some(it => it.startsWith(it.name))) { - return (list[path] = it) + return list.set(path, it) } else { return } @@ -53,94 +69,10 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { return } - list[path] = it + list.set(path, it) } }) - let compileFiles = function (currentPage, page, files) { - let options = { - IS_MPA, - currentPage, - SOURCE_DIR, - DEPLOY_PATH, - INJECT_SCSS, - LEGACY_MODE, - isCustomElement: conf.isCustomElement || isCustomElement - } - - for (let k in files) { - let it = files[k] - // 入口文件, 特殊处理 - if (page && it.path === page.entry) { - let entry = fs.cat(page.entry).toString() - - entry = parseJs(entry, conf.imports, { ...options, IS_ENTRY: true }) - - let code = parseHtml(template, { - page, - imports: conf.imports, - entry, - LEGACY_MODE - }) - - fs.echo(code, join(dist, `${currentPage}.html`)) - continue - } - - verbose && console.log(' 解析 %s ...', it.name) - - let pageDir = IS_MPA && currentPage ? `pages/${currentPage}` : '' - - switch (it.ext) { - case '.vue': - { - let code = compileVue(it.path, conf.imports, options) - - Es.transform(code, { minify: true }).then(r => { - fs.echo( - r.code, - join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js')) - ) - }) - } - break - - case '.js': - { - let code = fs.cat(it.path) - - code = parseJs(code + '', conf.imports, options) - - Es.transform(code, { minify: true }).then(r => { - fs.echo(r.code, join(dist, 'assets/', pageDir, it.name)) - }) - } - break - - case '.scss': - case '.css': - { - let target = join( - dist, - 'assets/', - it.name.replace(/\.scss$/, '.css') - ) - if (it.ext === '.css') { - fs.cp(it.path, target) - } else { - let code = compileScss(it.path) - fs.echo(code, target) - } - } - break - - default: - fs.cp(it.path, join(dist, it.name)) - break - } - } - } - // 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件 if (fs.isdir(PUBLIC_DIR)) { console.log('\n正在处理静态资源 ...') @@ -160,35 +92,89 @@ export default function compile(root = '', dist = '', conf = {}, verbose) { } console.log('') - for (let currentPage in conf.pages) { + if (IS_MPA) { + // 电脑线程数比页面数量还多时, 取小 + let num = Math.min(PAGES_KEYS.length, THREADS_NUM) + let chunkSize = Math.ceil(PAGES_KEYS.length / num) + + for (let i = 0; i < num; i++) { + let start = i * chunkSize + let end = start + chunkSize + let pages = PAGES_KEYS.slice(start, end) + let chunk = new Map() + + for (let currentPage of pages) { + let page = conf.pages[currentPage] + let dir = dirname(page.entry) + let files = new Map() + + fs.ls(dir, true).forEach(path => { + if (fs.isdir(path)) { + return + } + + let name = path.slice(dir.length + 1) + let ext = parse(name).ext + + if (ext === '') { + return + } + + list.delete(path) + files.set(path, { name, path, ext }) + }) + + chunk.set(currentPage, { page, files }) + } + new Worker(join(__dirname, './thread.js'), { + workerData: { + options, + data: { + chunk, + verbose, + dist, + imports: conf.imports + } + } + }) + // compileFiles(currentPage, page, chunk, options, { + // verbose, + // dist, + // imports: conf.imports + // }) + } + } else { + // 每个线程处理的文件数 + let chunkSize = Math.ceil(list.size / THREADS_NUM) + let currentPage = PAGES_KEYS[0] let page = conf.pages[currentPage] - let dir = dirname(page.entry) - let files = list - if (IS_MPA) { - files = {} - fs.ls(dir, true).forEach(path => { - if (fs.isdir(path)) { - return + + list = [...list] + + for (let i = 0; i < THREADS_NUM; i++) { + let start = i * chunkSize + let end = start + chunkSize + let chunk = [list.slice(start, end)] + + new Worker(join(__dirname, './thread.js'), { + workerData: { + options, + data: { + currentPage, + page, + chunk, + verbose, + dist, + imports: conf.imports + } } - - let name = path.slice(dir.length + 1) - let ext = parse(name).ext - - if (ext === '') { - return - } - - delete list[path] - files[path] = { name, path, ext } }) } - console.log('正在生成 %s ...', `${currentPage}.html`) - compileFiles(currentPage, page, files) } if (IS_MPA) { console.log('\n正在解析公共依赖 ...') - compileFiles('', null, list) + // compileFiles('', null, list) } process.on('exit', _ => { diff --git a/lib/thread.js b/lib/thread.js new file mode 100644 index 0000000..6e84968 --- /dev/null +++ b/lib/thread.js @@ -0,0 +1,24 @@ +/** + * 子线程 + * @author yutent + * @date 2023/06/14 16:15:39 + */ +import { workerData } from 'node:worker_threads' +import { compileFiles } from './compile.js' + +const { options, data } = workerData + +options.isCustomElement = Function('return ' + options.isCustomElement)() + +// console.log(options) + +let chunk = data.chunk + +delete data.chunk + +chunk.forEach(([a, b]) => { + console.log(a, b) +}) +// for (let [currentPage, { page, files }] of chunk.entries()) { +// compileFiles(currentPage, page, files, options, { ...data }) +// }