Skip to content

Commit

Permalink
Merge pull request #11 from mpvue/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
anchengjian authored Apr 13, 2018
2 parents 3d977b9 + 2905ef5 commit 6f18500
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
11 changes: 5 additions & 6 deletions lib/mp-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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(
Expand Down
11 changes: 8 additions & 3 deletions lib/mp-compiler/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<import src="slots" />', '')
const allSlots = Object.keys(slotsCache).map(v => slotsCache[v].code).join('\n')
return allImportCode + allSlots
}

// 包大小优化: build 模式不需要美化 wxml
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 6f18500

Please sign in to comment.