完成多线程编译
parent
cca39d9156
commit
ea986f0a39
|
@ -20,9 +20,9 @@ export async function compileFiles(
|
||||||
) {
|
) {
|
||||||
options.currentPage = currentPage
|
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()
|
let entry = fs.cat(page.entry).toString()
|
||||||
|
|
||||||
entry = parseJs(entry, imports, { ...options, IS_ENTRY: true })
|
entry = parseJs(entry, imports, { ...options, IS_ENTRY: true })
|
||||||
|
@ -45,7 +45,7 @@ export async function compileFiles(
|
||||||
switch (it.ext) {
|
switch (it.ext) {
|
||||||
case '.vue':
|
case '.vue':
|
||||||
{
|
{
|
||||||
let code = compileVue(it.path, imports, options)
|
let code = compileVue(path, imports, options)
|
||||||
|
|
||||||
await Es.transform(code, { minify: true }).then(r => {
|
await Es.transform(code, { minify: true }).then(r => {
|
||||||
fs.echo(
|
fs.echo(
|
||||||
|
@ -58,7 +58,7 @@ export async function compileFiles(
|
||||||
|
|
||||||
case '.js':
|
case '.js':
|
||||||
{
|
{
|
||||||
let code = fs.cat(it.path)
|
let code = fs.cat(path)
|
||||||
|
|
||||||
code = parseJs(code + '', imports, options)
|
code = parseJs(code + '', imports, options)
|
||||||
|
|
||||||
|
@ -78,16 +78,16 @@ export async function compileFiles(
|
||||||
it.name.replace(/\.scss$/, '.css')
|
it.name.replace(/\.scss$/, '.css')
|
||||||
)
|
)
|
||||||
if (it.ext === '.css') {
|
if (it.ext === '.css') {
|
||||||
fs.cp(it.path, target)
|
fs.cp(path, target)
|
||||||
} else {
|
} else {
|
||||||
let code = compileScss(it.path)
|
let code = compileScss(path)
|
||||||
fs.echo(code, target)
|
fs.echo(code, target)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fs.cp(it.path, join(dist, 'assets/', pageDir, it.name))
|
fs.cp(path, join(dist, 'assets/', pageDir, it.name))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import socket from './ws.js'
|
||||||
import chokidar from 'chokidar'
|
import chokidar from 'chokidar'
|
||||||
import { red } from 'kolorist'
|
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'
|
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 INJECT_SCSS = readFile(conf.inject?.scss)
|
||||||
const LEGACY_MODE = !!conf.legacy
|
const LEGACY_MODE = !!conf.legacy
|
||||||
const ENABLE_GZIP = !!conf.devServer.gzip
|
const ENABLE_GZIP = !!conf.devServer.gzip
|
||||||
|
const { isCustomElement = defaultCustomElement } = conf.compileOptions || {}
|
||||||
|
|
||||||
if (conf.imports['vue-dev']) {
|
if (conf.imports['vue-dev']) {
|
||||||
conf.imports.vue = conf.imports['vue-dev']
|
conf.imports.vue = conf.imports['vue-dev']
|
||||||
|
@ -215,7 +216,7 @@ export default async function createServer(root = '', conf = {}) {
|
||||||
DEPLOY_PATH,
|
DEPLOY_PATH,
|
||||||
INJECT_SCSS,
|
INJECT_SCSS,
|
||||||
LEGACY_MODE,
|
LEGACY_MODE,
|
||||||
isCustomElement: conf.isCustomElement || isCustomElement
|
isCustomElement
|
||||||
})
|
})
|
||||||
|
|
||||||
res.setHeader('content-type', MIME_TYPES.js)
|
res.setHeader('content-type', MIME_TYPES.js)
|
||||||
|
@ -395,7 +396,7 @@ export default async function createServer(root = '', conf = {}) {
|
||||||
DEPLOY_PATH,
|
DEPLOY_PATH,
|
||||||
INJECT_SCSS,
|
INJECT_SCSS,
|
||||||
LEGACY_MODE,
|
LEGACY_MODE,
|
||||||
isCustomElement: conf.isCustomElement || isCustomElement
|
isCustomElement
|
||||||
})
|
})
|
||||||
let tmp = CACHE[filePath]
|
let tmp = CACHE[filePath]
|
||||||
if (tmp.changed) {
|
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 INJECT_SCSS = readFile(conf.inject?.scss)
|
||||||
const LEGACY_MODE = !!conf.legacy
|
const LEGACY_MODE = !!conf.legacy
|
||||||
|
const { isCustomElement } = conf.compileOptions || {}
|
||||||
|
|
||||||
conf.inject = conf.inject || { scss: '' }
|
conf.inject = conf.inject || { scss: '' }
|
||||||
|
|
||||||
|
@ -61,7 +62,21 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
|
||||||
INJECT_SCSS,
|
INJECT_SCSS,
|
||||||
LEGACY_MODE,
|
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 => {
|
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 name = path.slice(SOURCE_DIR.length + 1)
|
||||||
let it = {
|
let it = {
|
||||||
path,
|
|
||||||
name,
|
name,
|
||||||
ext: parse(name).ext
|
ext: parse(name).ext
|
||||||
}
|
}
|
||||||
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))) {
|
||||||
list.set(path, it)
|
list.set(path, it)
|
||||||
|
@ -97,11 +111,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
|
||||||
fs.ls(PUBLIC_DIR, true).forEach(it => {
|
fs.ls(PUBLIC_DIR, true).forEach(it => {
|
||||||
let ext = parse(it).ext
|
let ext = parse(it).ext
|
||||||
|
|
||||||
if (ext === '') {
|
if (ext && fs.isfile(it)) {
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fs.isfile(it)) {
|
|
||||||
let name = it.slice(PUBLIC_DIR.length + 1)
|
let name = it.slice(PUBLIC_DIR.length + 1)
|
||||||
verbose && console.log(' 复制 %s ...', name)
|
verbose && console.log(' 复制 %s ...', name)
|
||||||
fs.cp(it, join(dist, 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) {
|
if (IS_MPA) {
|
||||||
for (let currentPage of PAGES_KEYS) {
|
for (let currentPage of PAGES_KEYS) {
|
||||||
let page = conf.pages[currentPage]
|
let page = conf.pages[currentPage]
|
||||||
|
@ -143,18 +139,18 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
|
||||||
}
|
}
|
||||||
|
|
||||||
list.delete(path)
|
list.delete(path)
|
||||||
files.set(path, { name, path, ext })
|
files.set(path, { name, ext })
|
||||||
})
|
})
|
||||||
chunk.set(currentPage, { page, files })
|
chunk.set(currentPage, { page, files })
|
||||||
JOBS_QUEUE.push(chunk)
|
JOBS_QUEUE.push(chunk)
|
||||||
doJob()
|
doJob()
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
// 公共依赖
|
||||||
{
|
{
|
||||||
let chunk = new Map()
|
let chunk = new Map()
|
||||||
|
|
||||||
chunk.set('', { page: null, files: list })
|
chunk.set('', { page: null, files: list })
|
||||||
|
|
||||||
JOBS_QUEUE.push(chunk)
|
JOBS_QUEUE.push(chunk)
|
||||||
doJob()
|
doJob()
|
||||||
}
|
}
|
||||||
|
@ -166,6 +162,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
|
||||||
|
|
||||||
list = [...list]
|
list = [...list]
|
||||||
|
|
||||||
|
console.log(`正在生成 ${currentPage}.html ...`)
|
||||||
|
|
||||||
for (let i = 0; i < THREADS_NUM; i++) {
|
for (let i = 0; i < THREADS_NUM; i++) {
|
||||||
let start = i * chunkSize
|
let start = i * chunkSize
|
||||||
let end = start + 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) })
|
chunk.set(currentPage, { page, files: list.slice(start, end) })
|
||||||
|
|
||||||
new Worker(join(__dirname, './thread.js'), {
|
JOBS_QUEUE.push(chunk)
|
||||||
workerData: {
|
doJob()
|
||||||
options,
|
|
||||||
data: {
|
|
||||||
chunk,
|
|
||||||
verbose,
|
|
||||||
dist,
|
|
||||||
imports: conf.imports
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,18 +5,24 @@
|
||||||
*/
|
*/
|
||||||
import { parentPort, workerData } from 'node:worker_threads'
|
import { parentPort, workerData } from 'node:worker_threads'
|
||||||
import { compileFiles } from './compile.js'
|
import { compileFiles } from './compile.js'
|
||||||
import { isCustomElement } from './utils.js'
|
import { defaultCustomElement } from './utils.js'
|
||||||
|
|
||||||
const { options, verbose, dist, imports } = workerData
|
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) {
|
async function doJob(job) {
|
||||||
let [currentPage, { page, files }] = job.entries().next().value
|
let [currentPage, { page, files }] = job.entries().next().value
|
||||||
|
|
||||||
console.log(
|
options.IS_MPA &&
|
||||||
currentPage ? `正在生成 ${currentPage}.html ...` : '\n正在解析公共依赖 ...'
|
console.log(
|
||||||
)
|
currentPage
|
||||||
|
? `正在生成 ${currentPage}.html ...`
|
||||||
|
: '\n正在解析公共依赖 ...'
|
||||||
|
)
|
||||||
|
|
||||||
await compileFiles(currentPage, page, files, options, {
|
await compileFiles(currentPage, page, files, options, {
|
||||||
verbose,
|
verbose,
|
||||||
dist,
|
dist,
|
||||||
|
|
|
@ -94,6 +94,7 @@ export function createHmrScript(legacy, session = '') {
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isCustomElement(tag) {
|
// 默认的 web components 判断
|
||||||
|
export function defaultCustomElement(tag) {
|
||||||
return tag.startsWith('wc-')
|
return tag.startsWith('wc-')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue