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

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

View File

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

View File

@ -44,7 +44,7 @@ export async function compileFiles(
switch (it.ext) { switch (it.ext) {
case '.vue': case '.vue':
{ {
let code = compileVue(path, imports, options) let code = await compileVue(path, imports, options)
await Es.transform(code, { minify: true }).then(r => { await Es.transform(code, { minify: true }).then(r => {
fs.echo( fs.echo(
@ -61,8 +61,11 @@ export async function compileFiles(
code = parseJs(code + '', imports, options) code = parseJs(code + '', imports, options)
await Es.transform(code, { minify: true }).then(r => { await Es.transform(code, { minify: true }).then(async ({ code }) => {
fs.echo(r.code, join(dist, 'assets/', pageDir, it.name)) for (let fn of options.plugin) {
code = await fn('js', code)
}
fs.echo(code, join(dist, 'assets/', pageDir, it.name))
}) })
} }
break break
@ -91,6 +94,9 @@ export async function compileFiles(
fs.cp(path, target) fs.cp(path, target)
} else { } else {
let code = compileScss(path) let code = compileScss(path)
for (let fn of options.plugin) {
code = await fn('css', code)
}
fs.echo(code, target) 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 INJECT_SCSS = readFile(conf.inject?.scss)
const LEGACY_MODE = !!conf.legacy const LEGACY_MODE = !!conf.legacy
const { isCustomElement } = conf.compileOptions || {} const { ABS_CONFIG_FILEPATH } = conf
conf.inject = conf.inject || { scss: '' } conf.inject = conf.inject || { scss: '' }
@ -65,13 +65,7 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
DEPLOY_PATH, DEPLOY_PATH,
INJECT_SCSS, INJECT_SCSS,
LEGACY_MODE, LEGACY_MODE,
// 线程通讯无法传递函数类型, 需要转为字符串, 之后再转回来 ABS_CONFIG_FILEPATH
isCustomElement:
THREADS_NUM > 0
? isCustomElement
? isCustomElement.toString()
: null
: isCustomElement || defaultCustomElement
} }
fs.ls(SOURCE_DIR, true).forEach(path => { 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)) { 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( WORKER_POOL.add(
new Worker(join(__dirname, './thread.js'), { new Worker(join(__dirname, './thread.js'), {
workerData: { workerData: {
@ -202,7 +199,6 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
doJob() doJob()
} }
} else { } else {
options.isCustomElement = isCustomElement || defaultCustomElement
compileFiles(currentPage, page, list, options, { compileFiles(currentPage, page, list, options, {
verbose, verbose,
dist, dist,

View File

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