From 9cabb5439b4ea49237ea66bb5e0fabded335a7c1 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 16 Aug 2021 12:05:57 +0800 Subject: [PATCH] feat(app): copy only changed files --- packages/uni-app-vite/src/index.ts | 2 + packages/uni-app-vite/src/plugins/stats.ts | 40 +++++++++++++++++++ .../src/webpack/plugin/WatchPlugin.ts | 8 ++-- packages/vite-plugin-uni/src/cli/action.ts | 25 ++++++++++-- 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 packages/uni-app-vite/src/plugins/stats.ts diff --git a/packages/uni-app-vite/src/index.ts b/packages/uni-app-vite/src/index.ts index 865456ff0..2a71f3df7 100644 --- a/packages/uni-app-vite/src/index.ts +++ b/packages/uni-app-vite/src/index.ts @@ -12,6 +12,7 @@ import { uniManifestJsonPlugin } from './plugins/manifestJson' import { uniPagesJsonPlugin } from './plugins/pagesJson' // import { uniResolveIdPlugin } from './plugins/resolveId' import { uniRenderjsPlugin } from './plugins/renderjs' +import { uniStatsPlugin } from './plugins/stats' function initUniCssScopedPluginOptions() { const styleIsolation = getAppStyleIsolation( @@ -36,6 +37,7 @@ const plugins = [ uniViteInjectPlugin(initProvide()), uniRenderjsPlugin(), uniTemplatePlugin(), + uniStatsPlugin(), UniAppPlugin, ] diff --git a/packages/uni-app-vite/src/plugins/stats.ts b/packages/uni-app-vite/src/plugins/stats.ts new file mode 100644 index 000000000..a463a90da --- /dev/null +++ b/packages/uni-app-vite/src/plugins/stats.ts @@ -0,0 +1,40 @@ +import { Plugin, ResolvedConfig } from 'vite' +import { hash } from '@dcloudio/uni-cli-shared' + +const emittedHashMap = new WeakMap>() + +export function uniStatsPlugin(): Plugin { + let resolvedConfig: ResolvedConfig + return { + name: 'vite:uni-app-stats', + enforce: 'post', + configResolved(config) { + resolvedConfig = config + emittedHashMap.set(resolvedConfig, new Map()) + }, + writeBundle(_, bundle) { + if (resolvedConfig.isProduction) { + // 仅dev生效 + return + } + const emittedHash = emittedHashMap.get(resolvedConfig)! + const changedFiles: string[] = [] + Object.keys(bundle).forEach((filename) => { + const outputFile = bundle[filename] + let outputFileHash = '' + if (outputFile.type === 'asset') { + outputFileHash = hash(outputFile.source) + } else { + outputFileHash = hash(outputFile.code) + } + if (emittedHash.get(filename) !== outputFileHash) { + emittedHash.set(filename, outputFileHash) + changedFiles.push(filename) + } + }) + process.env.UNI_APP_CHANGED_FILES = changedFiles.length + ? JSON.stringify(changedFiles) + : '' + }, + } +} diff --git a/packages/uni-cli-nvue/src/webpack/plugin/WatchPlugin.ts b/packages/uni-cli-nvue/src/webpack/plugin/WatchPlugin.ts index 2720b8010..a7cfe4735 100644 --- a/packages/uni-cli-nvue/src/webpack/plugin/WatchPlugin.ts +++ b/packages/uni-cli-nvue/src/webpack/plugin/WatchPlugin.ts @@ -23,9 +23,6 @@ export default class WatchPlugin { }) compiler.hooks.done.tap('WatchPlugin', (stats) => { isCompiling = false - if (isFirst) { - return (isFirst = false) - } const changedFiles: Set = new Set() stats.compilation.chunks.forEach(({ name, hash, files }) => { if (!hash) { @@ -38,7 +35,7 @@ export default class WatchPlugin { } files.forEach((file) => changedFiles.add(file)) }) - if (changedFiles.size) { + if (!isFirst && changedFiles.size) { console.log( M['dev.watching.end.pages'].replace( '{pages}', @@ -46,6 +43,9 @@ export default class WatchPlugin { ) ) } else { + if (isFirst) { + return (isFirst = false) + } console.log(M['dev.watching.end']) } }) diff --git a/packages/vite-plugin-uni/src/cli/action.ts b/packages/vite-plugin-uni/src/cli/action.ts index 53b127c6a..09fedead8 100644 --- a/packages/vite-plugin-uni/src/cli/action.ts +++ b/packages/vite-plugin-uni/src/cli/action.ts @@ -15,16 +15,33 @@ export async function runDev(options: CliOptions & ServerOptions) { await (options.ssr ? createSSRServer(options) : createServer(options)) } else { const watcher = (await build(options)) as RollupWatcher - let isFirst = true + let isFirstStart = true + let isFirstEnd = true watcher.on('event', (event) => { if (event.code === 'BUNDLE_START') { - if (isFirst) { - return (isFirst = false) + if (isFirstStart) { + return (isFirstStart = false) } console.log(M['dev.watching.start']) } else if (event.code === 'BUNDLE_END') { event.result.close() - console.log(M['dev.watching.end']) + if (options.platform !== 'app') { + // 非App平台无需处理增量同步 + return console.log(M['dev.watching.end']) + } + if (isFirstEnd) { + // 首次全量同步 + return (isFirstEnd = false), console.log(M['dev.watching.end']) + } + if (process.env.UNI_APP_CHANGED_FILES) { + return console.log( + M['dev.watching.end.files'].replace( + '{files}', + process.env.UNI_APP_CHANGED_FILES + ) + ) + } + return console.log(M['dev.watching.end']) } }) } -- GitLab