diff --git a/packages/vue-cli-plugin-hbuilderx/build/webpack.nvue.conf.js b/packages/vue-cli-plugin-hbuilderx/build/webpack.nvue.conf.js index b8d11271a4d1aba447da14a6162cd61edcdf9c5f..a82c9fbdacbe1a244d24214fea2c240a4981052a 100644 --- a/packages/vue-cli-plugin-hbuilderx/build/webpack.nvue.conf.js +++ b/packages/vue-cli-plugin-hbuilderx/build/webpack.nvue.conf.js @@ -12,7 +12,10 @@ const { getTemplatePath } = require('@dcloudio/uni-cli-shared') -const WebpackAppPlusNVuePlugin = require('../packages/webpack-app-plus-nvue-plugin') +const WebpackAppPlusNVuePlugin = process.env.UNI_USING_V3 + ? require('../packages/webpack-app-plus-plugin') + : require('../packages/webpack-app-plus-nvue-plugin') + const WebpackErrorsPlugin = require('@dcloudio/vue-cli-plugin-uni/packages/webpack-errors-plugin') const WebpackUniMPPlugin = require('@dcloudio/webpack-uni-mp-loader/lib/plugin/index-new') @@ -72,8 +75,8 @@ const plugins = [ new webpack.DefinePlugin({ 'process.env': { 'NODE_ENV': JSON.stringify(process.env.NODE_ENV), - 'VUE_APP_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM), - 'UNI_CLOUD_PROVIDER': process.env.UNI_CLOUD_PROVIDER, + 'VUE_APP_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM), + 'UNI_CLOUD_PROVIDER': process.env.UNI_CLOUD_PROVIDER, 'HBX_USER_TOKEN': JSON.stringify(process.env.HBX_USER_TOKEN || '') } }), diff --git a/packages/vue-cli-plugin-hbuilderx/packages/webpack-app-plus-plugin/index.js b/packages/vue-cli-plugin-hbuilderx/packages/webpack-app-plus-plugin/index.js index a46173f7ea4b74ed9bdfbd4dc050247cde26f6c9..4297c46ca0ca3ec37cbc724c854808c08fb5fca7 100644 --- a/packages/vue-cli-plugin-hbuilderx/packages/webpack-app-plus-plugin/index.js +++ b/packages/vue-cli-plugin-hbuilderx/packages/webpack-app-plus-plugin/index.js @@ -6,14 +6,18 @@ const { done } = require('@vue/cli-shared-utils') +let nvueCompiled = true let serviceCompiled = true let viewCompiled = true +const nvueChangedFiles = [] const serviceChangedFiles = [] const viewChangedFiles = [] let isFirst = true + +let compiling = false class WebpackAppPlusPlugin { apply(compiler) { if (process.env.UNI_USING_V3) { @@ -21,20 +25,27 @@ class WebpackAppPlusPlugin { const chunkVersions = {} const entry = compiler.options.entry() - const isAppService = entry['app-service'] - const isAppView = entry['app-view'] + const isAppService = !!entry['app-service'] + const isAppView = !!entry['app-view'] + + const isAppNVue = !isAppService && !isAppView + + + compiler.hooks.invalid.tap('WebpackAppPlusPlugin', (fileName, changeTime) => { + if (!compiling) { + compiling = true + console.log('开始差量编译...') + } + }) compiler.hooks.beforeCompile.tapAsync('WebpackAppPlusPlugin', (params, callback) => { + isAppNVue && (nvueCompiled = false) isAppService && (serviceCompiled = false) isAppView && (viewCompiled = false) callback() }) compiler.hooks.emit.tapAsync('WebpackAppPlusPlugin', (compilation, callback) => { - - isAppService && (serviceChangedFiles.length = 0) - isAppView && (viewChangedFiles.length = 0) - const changedChunks = compilation.chunks.filter(chunk => { const oldVersion = chunkVersions[chunk.name] chunkVersions[chunk.name] = chunk.hash @@ -47,6 +58,8 @@ class WebpackAppPlusPlugin { !serviceChangedFiles.includes(file) && (serviceChangedFiles.push(file)) } else if (isAppView) { !viewChangedFiles.includes(file) && (viewChangedFiles.push(file)) + } else if (isAppNVue) { + !nvueChangedFiles.includes(file) && (nvueChangedFiles.push(file)) } }) } @@ -56,14 +69,23 @@ class WebpackAppPlusPlugin { compiler.hooks.done.tapPromise('WebpackAppPlusPlugin', compilation => { return new Promise((resolve, reject) => { + isAppNVue && (nvueCompiled = true) isAppService && (serviceCompiled = true) isAppView && (viewCompiled = true) - - if (serviceCompiled && viewCompiled) { - const changedFiles = [...new Set([...serviceChangedFiles, ...viewChangedFiles])] + if (serviceCompiled && viewCompiled && nvueCompiled) { if (process.env.NODE_ENV === 'development') { + const changedFiles = [...new Set([ + ...serviceChangedFiles, + ...viewChangedFiles, + ...nvueChangedFiles + ])] if (!isFirst && changedFiles.length > 0) { - done(`Build complete. FILES:` + JSON.stringify(changedFiles)) + if (serviceChangedFiles.length === 0 && viewChangedFiles.length === 0) { + // 仅 nvue 页面发生变化 + done(`Build complete. PAGES:` + JSON.stringify(changedFiles)) + } else { + done(`Build complete. FILES:` + JSON.stringify(changedFiles)) + } } else { done(`Build complete. Watching for changes...`) } @@ -71,8 +93,11 @@ class WebpackAppPlusPlugin { } else { done(`Build complete. `) } + nvueChangedFiles.length = 0 + serviceChangedFiles.length = 0 + viewChangedFiles.length = 0 + compiling = false } - resolve() }) }) diff --git a/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/plugin/index.js b/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/plugin/index.js index 73892e102d17c8437c87b20eff92fd1a5b44a2e2..2d7654b1699386a38ca515abfaed612fbd207900 100644 --- a/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/plugin/index.js +++ b/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/plugin/index.js @@ -8,6 +8,8 @@ const { initAutoImportScanComponents } = require('@dcloudio/uni-cli-shared/lib/pages') +let compiling = false + class WebpackUniAppPlugin { apply(compiler) { if (process.UNI_CONFUSION) { @@ -32,6 +34,12 @@ class WebpackUniAppPlugin { } compiler.hooks.invalid.tap('webpack-uni-app-invalid', (fileName, changeTime) => { + if (!process.env.UNI_USING_V3) { + if (!compiling) { + compiling = true + console.log('开始差量编译...') + } + } if (fileName && typeof fileName === 'string') { if (fileName.indexOf('.vue') !== -1 || fileName.indexOf('.nvue') !== -1) { if (process.UNI_AUTO_SCAN_COMPONENTS) { @@ -40,6 +48,15 @@ class WebpackUniAppPlugin { } } }) + // v3 版本在webpack-app-plus-plugin/index.js中处理,目前太乱了。后续要整理 + if (!process.env.UNI_USING_V3) { + compiler.hooks.done.tapPromise('webpack-uni-app-done', compilation => { + return new Promise((resolve, reject) => { + compiling = false + resolve() + }) + }) + } } }