提交 9cabb543 编写于 作者: fxy060608's avatar fxy060608

feat(app): copy only changed files

上级 34a106c7
......@@ -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,
]
......
import { Plugin, ResolvedConfig } from 'vite'
import { hash } from '@dcloudio/uni-cli-shared'
const emittedHashMap = new WeakMap<ResolvedConfig, Map<string, string>>()
export function uniStatsPlugin(): Plugin {
let resolvedConfig: ResolvedConfig
return {
name: 'vite:uni-app-stats',
enforce: 'post',
configResolved(config) {
resolvedConfig = config
emittedHashMap.set(resolvedConfig, new Map<string, string>())
},
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)
: ''
},
}
}
......@@ -23,9 +23,6 @@ export default class WatchPlugin {
})
compiler.hooks.done.tap('WatchPlugin', (stats) => {
isCompiling = false
if (isFirst) {
return (isFirst = false)
}
const changedFiles: Set<string> = new Set<string>()
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'])
}
})
......
......@@ -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'])
}
})
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册