diff --git a/index.js b/index.js index 1096cb2..fb7c317 100755 --- a/index.js +++ b/index.js @@ -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 => { diff --git a/lib/compile-vue.js b/lib/compile-vue.js index 6e35595..35b9c3c 100644 --- a/lib/compile-vue.js +++ b/lib/compile-vue.js @@ -303,7 +303,7 @@ export function parseJs( * @param file 文件路径 * @return 返回转换后的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 } diff --git a/lib/compile.js b/lib/compile.js index 85a041f..0208dca 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -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) } } diff --git a/lib/prod.js b/lib/prod.js index 3247c94..2027912 100644 --- a/lib/prod.js +++ b/lib/prod.js @@ -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, diff --git a/lib/thread.js b/lib/thread.js index 1f9fad3..fb469ee 100644 --- a/lib/thread.js +++ b/lib/thread.js @@ -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)