完成多线程编译
parent
cca39d9156
commit
ea986f0a39
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
61
lib/prod.js
61
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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -94,6 +94,7 @@ export function createHmrScript(legacy, session = '') {
|
|||
`
|
||||
}
|
||||
|
||||
export function isCustomElement(tag) {
|
||||
// 默认的 web components 判断
|
||||
export function defaultCustomElement(tag) {
|
||||
return tag.startsWith('wc-')
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue