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 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": {