提交 4ab48920 编写于 作者: fxy060608's avatar fxy060608

refactor(mp): generate asset source

上级 ef345f14
......@@ -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()
......
......@@ -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
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册