Skip to content

Develop #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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