Skip to content
Closed
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
3 changes: 2 additions & 1 deletion lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ module.exports = function (content) {
// for mp
// 针对 entry 的 main.js 处理 page 和 app 的入口文件和配置等
const mpOptions = loaderUtils.getOptions(this) || {}

if (!mpPages.length) {
const { entry = {}} = this.options
Object.keys(entry).forEach(k => {
mpInfo[entry[k]] = k
if (k !== 'app') {
mpPages.push(`pages/${k}/${k}`)
mpPages.push(k)
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions lib/mp-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function createWxml (emitWarning, emitError, emitFile, resourcePath, rootCompone

if (rootComponent) {
const componentName = getCompNameBySrc(rootComponent)
wxmlContent = genPageWxml(componentName)
wxmlContent = genPageWxml(componentName, src)
wxmlSrc = src
} else {
// TODO, 这儿传 options 进去
Expand Down Expand Up @@ -166,10 +166,10 @@ function compileMP (content, optioins) {
}

// 生成入口 js
emitFile(`${src}.js`, genScript(name, isPage))
emitFile(`${src}.js`, genScript(name, isPage, src))

// 生成入口 wxss
emitFile(`${src}.wxss`, genStyle(name, isPage))
emitFile(`${src}.wxss`, genStyle(name, isPage, src))

// 这儿应该异步在所有的模块都清晰后再生成
// 生成入口 wxml
Expand Down
22 changes: 12 additions & 10 deletions lib/mp-compiler/templates.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
function genScript (name, isPage) {
const prefix = isPage ? '../..' : '.'
const { getPathPrefix } = require('./util')

function genScript (name, isPage, src) {
const prefix = isPage ? getPathPrefix(src) : './'

return `
import '${prefix}/static/js/manifest'
import '${prefix}/static/js/vendor'
import '${prefix}/static/js/${name}'
import '${prefix}static/js/manifest'
import '${prefix}static/js/vendor'
import '${prefix}static/js/${name}'
`
}

function genStyle (name, isPage) {
const prefix = isPage ? '../..' : '.'
return `@import "${prefix}/static/css/${name}.wxss";`
function genStyle (name, isPage, src) {
const prefix = isPage ? getPathPrefix(src) : './'
return `@import "${prefix}static/css/${name}.wxss";`
}

function genPageWxml (templateName) {
return `<import src="../../components/${templateName}" /><template is="${templateName}" data="{{ ...$root['0'], $root }}"/>`
function genPageWxml (templateName, src) {
return `<import src="${getPathPrefix(src)}components/${templateName}" /><template is="${templateName}" data="{{ ...$root['0'], $root }}"/>`
}

module.exports = { genScript, genStyle, genPageWxml }
18 changes: 16 additions & 2 deletions lib/mp-compiler/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function resolveTarget (dir, mpInfo = {}) {
const isPage = pageType === 'page'

// components 目录暂时无用
const src = isApp ? 'app' : isPage ? `pages/${name}/${name}` : `components/${name}`
const src = isApp ? 'app' : isPage ? name : `components/${name}`

return { pageType, src, name, isApp, isPage }
}
Expand All @@ -63,4 +63,18 @@ function getSlots (slotName) {
return slotName ? slotsCache[slotName] : slotsCache
}

module.exports = { cacheFileInfo, getFileInfo, getCompNameBySrc, resolveTarget, covertCCVar, cacheSlots, getSlots }
function getPathPrefix (src) {
const length = src.split('/').length - 1
return `${'../'.repeat(length)}`
}

module.exports = {
cacheFileInfo,
getFileInfo,
getCompNameBySrc,
resolveTarget,
covertCCVar,
cacheSlots,
getSlots,
getPathPrefix
}
Loading