From 7ea611aef094abbe74d4488281a9e24dd32f19aa Mon Sep 17 00:00:00 2001 From: anchengjian Date: Mon, 9 Apr 2018 00:59:30 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20slot=20=E4=B8=AD=E5=B5=8C=E5=A5=97?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=92=8Cevent=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mp-compiler/index.js | 11 +++++------ lib/mp-compiler/util.js | 11 ++++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index 9931079..69076b7 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -22,10 +22,9 @@ const { getPageSrc } = require('./util') -function createSlotsWxml (emitFile, slots) { - cacheSlots(slots) - const allSlots = getSlots() - const content = Object.keys(allSlots).map(v => allSlots[v].code).join('\n') +function createSlotsWxml (emitFile, slots, importCode) { + cacheSlots(slots, importCode) + const content = getSlots() if (content.trim()) { emitFile('components/slots.wxml', htmlBeautify(content)) } @@ -34,11 +33,11 @@ function createSlotsWxml (emitFile, slots) { // 调用 compiler 生成 wxml function genComponentWxml (compiled, options, emitFile, emitError, emitWarning) { options.components['slots'] = { src: 'slots', name: 'slots' } - const { code: wxmlCodeStr, compiled: cp, slots } = compiler.compileToWxml(compiled, options) + const { code: wxmlCodeStr, compiled: cp, slots, importCode } = compiler.compileToWxml(compiled, options) const { mpErrors, mpTips } = cp // 缓存 slots,延迟编译 - createSlotsWxml(emitFile, slots) + createSlotsWxml(emitFile, slots, importCode) if (mpErrors && mpErrors.length) { emitError( diff --git a/lib/mp-compiler/util.js b/lib/mp-compiler/util.js index 80b064a..657cd1f 100644 --- a/lib/mp-compiler/util.js +++ b/lib/mp-compiler/util.js @@ -70,13 +70,18 @@ function covertCCVar (str) { // 缓存所有的 slots 节点,生成一个文件 const slotsCache = Object.create(null) -function cacheSlots (slots) { +const importCodeCache = Object.create(null) + +function cacheSlots (slots, importCode) { Object.keys(slots).forEach(k => { slotsCache[k] = slots[k] }) + importCodeCache[importCode] = importCode } -function getSlots (slotName) { - return slotName ? slotsCache[slotName] : slotsCache +function getSlots () { + const allImportCode = Object.keys(importCodeCache).map(v => importCodeCache[v]).join('\n').replace('', '') + const allSlots = Object.keys(slotsCache).map(v => slotsCache[v].code).join('\n') + return allImportCode + allSlots } // 包大小优化: build 模式不需要美化 wxml From 3e9a505cf264a914350352ba074072c6f284cb03 Mon Sep 17 00:00:00 2001 From: anchengjian Date: Mon, 9 Apr 2018 00:59:30 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20slot=20=E4=B8=AD=E5=B5=8C=E5=A5=97?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=92=8Cevent=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mp-compiler/index.js | 11 +++++------ lib/mp-compiler/util.js | 11 ++++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index 9931079..69076b7 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -22,10 +22,9 @@ const { getPageSrc } = require('./util') -function createSlotsWxml (emitFile, slots) { - cacheSlots(slots) - const allSlots = getSlots() - const content = Object.keys(allSlots).map(v => allSlots[v].code).join('\n') +function createSlotsWxml (emitFile, slots, importCode) { + cacheSlots(slots, importCode) + const content = getSlots() if (content.trim()) { emitFile('components/slots.wxml', htmlBeautify(content)) } @@ -34,11 +33,11 @@ function createSlotsWxml (emitFile, slots) { // 调用 compiler 生成 wxml function genComponentWxml (compiled, options, emitFile, emitError, emitWarning) { options.components['slots'] = { src: 'slots', name: 'slots' } - const { code: wxmlCodeStr, compiled: cp, slots } = compiler.compileToWxml(compiled, options) + const { code: wxmlCodeStr, compiled: cp, slots, importCode } = compiler.compileToWxml(compiled, options) const { mpErrors, mpTips } = cp // 缓存 slots,延迟编译 - createSlotsWxml(emitFile, slots) + createSlotsWxml(emitFile, slots, importCode) if (mpErrors && mpErrors.length) { emitError( diff --git a/lib/mp-compiler/util.js b/lib/mp-compiler/util.js index 80b064a..657cd1f 100644 --- a/lib/mp-compiler/util.js +++ b/lib/mp-compiler/util.js @@ -70,13 +70,18 @@ function covertCCVar (str) { // 缓存所有的 slots 节点,生成一个文件 const slotsCache = Object.create(null) -function cacheSlots (slots) { +const importCodeCache = Object.create(null) + +function cacheSlots (slots, importCode) { Object.keys(slots).forEach(k => { slotsCache[k] = slots[k] }) + importCodeCache[importCode] = importCode } -function getSlots (slotName) { - return slotName ? slotsCache[slotName] : slotsCache +function getSlots () { + const allImportCode = Object.keys(importCodeCache).map(v => importCodeCache[v]).join('\n').replace('', '') + const allSlots = Object.keys(slotsCache).map(v => slotsCache[v].code).join('\n') + return allImportCode + allSlots } // 包大小优化: build 模式不需要美化 wxml From 2905ef500e88ce09ac80245449ca683766bb5e40 Mon Sep 17 00:00:00 2001 From: anchengjian Date: Fri, 13 Apr 2018 14:23:05 +0800 Subject: [PATCH 3/3] new version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2851c72..dc82f46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mpvue-loader", - "version": "1.0.10", + "version": "1.0.11", "description": "mpvue single-file component loader for Webpack", "main": "index.js", "repository": {