diff --git a/packages/webpack-uni-mp-loader/lib/plugin/generate-app.js b/packages/webpack-uni-mp-loader/lib/plugin/generate-app.js index 260c7f7c5ba335e2c5e10b76596bd463c4871501..6fd985c933f9b587cbe3db23e4238aaa1773dd7f 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-app.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-app.js @@ -54,25 +54,24 @@ module.exports = function generateApp (compilation) { } const runtimeJsPath = 'common/runtime.js' - + + const asset = compilation.assets[runtimeJsPath] if ( // app 和 baidu 不需要 process.env.UNI_PLATFORM !== 'app-plus' && process.env.UNI_PLATFORM !== 'mp-baidu' && - compilation.assets[runtimeJsPath] - ) { + asset && + !asset.source.__$wrappered + ) { const source = ` !function(){try{var a=Function("return this")();a&&!a.Math&&(Object.assign(a,{isFinite:isFinite,Array:Array,Date:Date,Error:Error,Function:Function,Math:Math,Object:Object,RegExp:RegExp,String:String,TypeError:TypeError,setTimeout:setTimeout,clearTimeout:clearTimeout,setInterval:setInterval,clearInterval:clearInterval}),"undefined"!=typeof Reflect&&(a.Reflect=Reflect))}catch(a){}}(); - ${compilation.assets[runtimeJsPath].source()} - ` - compilation.assets[runtimeJsPath] = { - size () { - return Buffer.byteLength(source, 'utf8') - }, - source () { - return source - } - } + ${asset.source()} + ` + const newSource = function () { + return source + } + newSource.__$wrappered = true + compilation.assets[runtimeJsPath].source = newSource } const specialMethods = getSpecialMethods() diff --git a/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js b/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js index 1f0a18a660b19ed2df773401474ea4e3dbce8621..2e26439c2a3f4eb74d084c03a5155b57fd4b45b0 100644 --- a/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js +++ b/packages/webpack-uni-mp-loader/lib/plugin/generate-component.js @@ -12,6 +12,8 @@ const { restoreNodeModules } = require('../shared') +const EMPTY_COMPONENT_LEN = 'Component({})'.length + const uniPath = normalizePath(require.resolve('@dcloudio/uni-' + process.env.UNI_PLATFORM)) function findModule (modules, resource, altResource) { @@ -66,6 +68,11 @@ module.exports = function generateComponent (compilation) { Object.keys(assets).forEach(name => { if (components.has(name.replace('.js', ''))) { curComponents.push(name.replace('.js', '')) + + if (assets[name].source.__$wrappered) { + return + } + const chunkName = name.replace('.js', '-create-component') let moduleId = '' @@ -80,7 +87,7 @@ module.exports = function generateComponent (compilation) { } const origSource = assets[name].source() - if (origSource.length !== 'Component({})'.length) { // 不是空组件 + if (origSource.length !== EMPTY_COMPONENT_LEN) { // 不是空组件 const globalVar = process.env.UNI_PLATFORM === 'mp-alipay' ? 'my' : 'global' // 主要是为了解决支付宝旧版本, Component 方法只在组件 js 里有,需要挂在 my.defineComponent let beforeCode = '' @@ -99,14 +106,11 @@ module.exports = function generateComponent (compilation) { [['${chunkName}']] ]); ` - assets[name] = { - size () { - return Buffer.byteLength(source, 'utf8') - }, - source () { - return source - } + const newSource = function () { + return source } + newSource.__$wrappered = true + assets[name].source = newSource } } }) @@ -118,11 +122,11 @@ module.exports = function generateComponent (compilation) { removeUnusedComponent(name) // 组件被移除 } } - } - for (const name of curComponents) { - if (!lastComponents.includes(name)) { - addComponent(name) // 新增组件 - } + } + for (const name of curComponents) { + if (!lastComponents.includes(name)) { + addComponent(name) // 新增组件 + } } lastComponents = curComponents }