修复线程创建; 调整编译配置处理;增加插件的支持

master
yutent 2024-07-25 15:24:06 +08:00
parent d84a59a5e5
commit c3dd7a843c
5 changed files with 39 additions and 20 deletions

View File

@ -19,6 +19,7 @@ const IS_WINDOWS = process.platform === 'win32'
const CONFIG_FILE = normalize(join(WORK_SPACE, 'fite.config.js'))
const PROTOCOL = IS_WINDOWS ? 'file://' : ''
const NODE_VERSION = process.versions.node.split('.').map(n => +n)
const ABS_CONFIG_FILEPATH = PROTOCOL + CONFIG_FILE
let args = process.argv.slice(2)
let mode = args.shift() || 'prod'
@ -40,8 +41,9 @@ switch (mode) {
case 'dev':
process.env.NODE_ENV = 'development'
import(PROTOCOL + CONFIG_FILE)
import(ABS_CONFIG_FILEPATH)
.then(function (conf) {
conf.default.ABS_CONFIG_FILEPATH = ABS_CONFIG_FILEPATH
createServer(WORK_SPACE, conf.default)
})
.catch(err => {
@ -52,13 +54,14 @@ switch (mode) {
case 'build':
process.env.NODE_ENV = 'production'
import(PROTOCOL + CONFIG_FILE)
import(ABS_CONFIG_FILEPATH)
.then(function (conf) {
let dist = conf.buildDir || 'dist'
if (clean && fs.isdir(dist)) {
console.log('清除dist目录...')
fs.rm(dist)
}
conf.default.ABS_CONFIG_FILEPATH = ABS_CONFIG_FILEPATH
compile(WORK_SPACE, dist, conf.default, verbose)
})
.catch(err => {

View File

@ -303,7 +303,7 @@ export function parseJs(
* @param file <String> 文件路径
* @return <String> 返回转换后的js代码
*/
export function compileVue(file, imports, options = {}) {
export async function compileVue(file, imports, options = {}) {
// 修正那反人类的windows路径
let filename = file.slice(options.SOURCE_DIR.length).replace(/\\/g, '/')
let code = (fs.cat(file) || '').toString().replace(/\r\n/g, '\n')
@ -333,10 +333,15 @@ export function compileVue(file, imports, options = {}) {
} else {
css = compileScss(it[1], options.INJECT_SCSS)
}
return css
})
.join(' ')
for (let fn of options.plugin) {
scss = await fn('css', scss)
}
js = js ? js[1] : 'export default {}'
try {
@ -428,6 +433,9 @@ document.adoptedStyleSheets = __sheets__
}
output += `__sfc__.__file = '${filename}'\nexport default __sfc__`
for (let fn of options.plugin) {
output = await fn('js', output)
}
return output
}

View File

@ -44,7 +44,7 @@ export async function compileFiles(
switch (it.ext) {
case '.vue':
{
let code = compileVue(path, imports, options)
let code = await compileVue(path, imports, options)
await Es.transform(code, { minify: true }).then(r => {
fs.echo(
@ -61,8 +61,11 @@ export async function compileFiles(
code = parseJs(code + '', imports, options)
await Es.transform(code, { minify: true }).then(r => {
fs.echo(r.code, join(dist, 'assets/', pageDir, it.name))
await Es.transform(code, { minify: true }).then(async ({ code }) => {
for (let fn of options.plugin) {
code = await fn('js', code)
}
fs.echo(code, join(dist, 'assets/', pageDir, it.name))
})
}
break
@ -91,6 +94,9 @@ export async function compileFiles(
fs.cp(path, target)
} else {
let code = compileScss(path)
for (let fn of options.plugin) {
code = await fn('css', code)
}
fs.echo(code, target)
}
}

View File

@ -52,7 +52,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 || {}
const { ABS_CONFIG_FILEPATH } = conf
conf.inject = conf.inject || { scss: '' }
@ -65,13 +65,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
DEPLOY_PATH,
INJECT_SCSS,
LEGACY_MODE,
// 线程通讯无法传递函数类型, 需要转为字符串, 之后再转回来
isCustomElement:
THREADS_NUM > 0
? isCustomElement
? isCustomElement.toString()
: null
: isCustomElement || defaultCustomElement
ABS_CONFIG_FILEPATH
}
fs.ls(SOURCE_DIR, true).forEach(path => {
@ -101,7 +95,10 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
// 创建线程池
if (THREADS_NUM > 0 && (IS_MPA || list.size > THREADS_NUM * 10)) {
for (let i = 0; i < THREADS_NUM; i++) {
// 页面数过少时, 线程数量不能比页面数多
let max = Math.min(THREADS_NUM, PAGES_KEYS.length)
for (let i = 0; i < max; i++) {
WORKER_POOL.add(
new Worker(join(__dirname, './thread.js'), {
workerData: {
@ -202,7 +199,6 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
doJob()
}
} else {
options.isCustomElement = isCustomElement || defaultCustomElement
compileFiles(currentPage, page, list, options, {
verbose,
dist,

View File

@ -8,11 +8,17 @@ import { compileFiles } from './compile.js'
import { defaultCustomElement } from './utils.js'
const { options, verbose, dist, imports } = workerData
const { ABS_CONFIG_FILEPATH } = options
options.isCustomElement = options.isCustomElement
? Function('return ' + options.isCustomElement)()
: defaultCustomElement
const { compileOptions = {}, plugin = [] } = await import(
ABS_CONFIG_FILEPATH
).then(r => r.default)
const { isCustomElement = defaultCustomElement } = compileOptions
options.isCustomElement = isCustomElement
options.plugin = plugin
//
async function doJob(job) {
let [currentPage, { page, files }] = job.entries().next().value
@ -28,7 +34,7 @@ async function doJob(job) {
dist,
imports
})
parentPort.postMessage('ok')
parentPort.postMessage(true)
}
parentPort.on('message', doJob)