多线程编译

threads
yutent 2023-06-14 19:36:20 +08:00
parent 3c239fe721
commit c675b73f25
4 changed files with 217 additions and 114 deletions

View File

@ -317,8 +317,9 @@ export function compileVue(file, imports, options = {}) {
isCustomElement isCustomElement
}).code.replace('export function render', 'function render') }).code.replace('export function render', 'function render')
} catch (err) { } catch (err) {
// console.log(err)
let tmp = html[1].split('\n') 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('%s: %s', red('SyntaxError'), red(err.message))
console.log( console.log(

92
lib/compile.js Normal file
View File

@ -0,0 +1,92 @@
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @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
}
}
}

View File

@ -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 fs from 'iofs'
import Es from 'esbuild' // import Es from 'esbuild'
import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' // import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js'
import { isCustomElement } from './utils.js' import { isCustomElement } from './utils.js'
import { compileFiles } from './compile.js'
const IS_WIN = process.platform === 'win32' const IS_WIN = process.platform === 'win32'
const PREFIX = IS_WIN ? 'pages\\' : 'pages/' 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) { function readFile(file) {
return (file && fs.cat(file)?.toString()) || '' 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 SOURCE_DIR = join(root, 'src')
const PUBLIC_DIR = join(root, 'public') const PUBLIC_DIR = join(root, 'public')
const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署 const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署
const IS_MPA = Object.keys(conf.pages).length > 1 const PAGES_KEYS = Object.keys(conf.pages)
const PAGES_PREFIX = Object.keys(conf.pages).map(it => const IS_MPA = PAGES_KEYS.length > 1
const PAGES_PREFIX = PAGES_KEYS.map(it =>
IS_WIN ? `${PREFIX + it}\\` : `${PREFIX + it}/` IS_WIN ? `${PREFIX + it}\\` : `${PREFIX + it}/`
) )
const INJECT_SCSS = readFile(conf.inject?.scss) const INJECT_SCSS = readFile(conf.inject?.scss)
@ -27,7 +33,17 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
let timeStart = Date.now() let timeStart = Date.now()
let template = fs.cat(join(process.cwd(), 'index.html')).toString() 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 => { fs.ls(SOURCE_DIR, true).forEach(path => {
if (fs.isdir(path)) { if (fs.isdir(path)) {
@ -43,7 +59,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
if (it.ext !== '') { if (it.ext !== '') {
if (IS_MPA && it.name.startsWith(PREFIX)) { if (IS_MPA && it.name.startsWith(PREFIX)) {
if (PAGES_PREFIX.some(it => it.startsWith(it.name))) { if (PAGES_PREFIX.some(it => it.startsWith(it.name))) {
return (list[path] = it) return list.set(path, it)
} else { } else {
return return
} }
@ -53,94 +69,10 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
return 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)) { if (fs.isdir(PUBLIC_DIR)) {
console.log('\n正在处理静态资源 ...') console.log('\n正在处理静态资源 ...')
@ -160,35 +92,89 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
} }
console.log('') 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 page = conf.pages[currentPage]
let dir = dirname(page.entry)
let files = list list = [...list]
if (IS_MPA) {
files = {} for (let i = 0; i < THREADS_NUM; i++) {
fs.ls(dir, true).forEach(path => { let start = i * chunkSize
if (fs.isdir(path)) { let end = start + chunkSize
return 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) { if (IS_MPA) {
console.log('\n正在解析公共依赖 ...') console.log('\n正在解析公共依赖 ...')
compileFiles('', null, list) // compileFiles('', null, list)
} }
process.on('exit', _ => { process.on('exit', _ => {

24
lib/thread.js Normal file
View File

@ -0,0 +1,24 @@
/**
* 子线程
* @author yutent<yutent.io@gmail.com>
* @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 })
// }