Skip to content
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

feat: 支持间接引用组件 #23

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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
33 changes: 28 additions & 5 deletions lib/mp-compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const {
getSlots,
htmlBeautify,
getBabelrc,
getPageSrc
getPageSrc,
getImportsMap,
getExportsMap
} = require('./util')

let slotsHookAdded = false
Expand Down Expand Up @@ -242,15 +244,36 @@ function compileMP (content, mpOptioins) {
return content
}

function resolveRealComSrc (realSrc, m, resolveFn) {
return new Promise((resolve, reject) => {
const isScript = /\.js$/.test(realSrc)
if (isScript) {
const scriptContent = fs.readFileSync(realSrc).toString()
const result = babel.transform(scriptContent)
const { importsMap } = getImportsMap(result.metadata)
const { exportsMap } = getExportsMap(result.metadata)
const source = importsMap[exportsMap[m] || m]
resolveFn(path.dirname(realSrc), source, (err, realComSrc) => {
if (err) return reject(err)
resolveRealComSrc(realComSrc, exportsMap[m], resolveFn).then(resolve).catch(reject)
})
} else {
resolve(realSrc)
}
})
}

function resolveSrc (originComponents, components, resolveFn, context) {
return Promise.all(Object.keys(originComponents).map(k => {
return new Promise((resolve, reject) => {
resolveFn(context, originComponents[k], (err, realSrc) => {
resolveFn(context, originComponents[k].src, (err, realSrc) => {
if (err) return reject(err)
const com = covertCCVar(k)
const comName = getCompNameBySrc(realSrc)
components[com] = { src: comName, name: comName }
resolve()
resolveRealComSrc(realSrc, originComponents[k].module, resolveFn).then(realComSrc => {
const comName = getCompNameBySrc(realComSrc)
components[com] = { src: comName, name: comName }
resolve()
}).catch(reject)
})
})
}))
Expand Down
24 changes: 6 additions & 18 deletions lib/mp-compiler/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,7 @@

const generate = require('babel-generator').default
const babelon = require('babelon')

function getImportsMap (metadata) {
let { importsMap } = metadata
const { imports } = metadata.modules

if (!importsMap) {
importsMap = {}
imports.forEach(m => {
m.specifiers.forEach(v => {
importsMap[v.local] = m.source
})
})
metadata.importsMap = importsMap
}

return metadata
}
const { getImportsMap } = require('./util')

// 解析 config
const traverseConfigVisitor = {
Expand Down Expand Up @@ -86,7 +70,11 @@ const traverseComponentsVisitor = {
const k = p.key.name || p.key.value
const v = p.value.name || p.value.value

components[k] = importsMap[v]
// 保留组件引用路径及模块名
components[k] = {
src: importsMap[v],
module: v
}
})

metadata.components = components
Expand Down
36 changes: 35 additions & 1 deletion lib/mp-compiler/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,38 @@ function getPathPrefix (src) {
return `${'../'.repeat(length)}`
}

function getImportsMap (metadata) {
let { importsMap } = metadata
const { imports } = metadata.modules

if (!importsMap) {
importsMap = {}
imports.forEach(m => {
m.specifiers.forEach(v => {
importsMap[v.local] = m.source
})
})
metadata.importsMap = importsMap
}

return metadata
}

function getExportsMap (metadata) {
let { exportsMap } = metadata
const { exports } = metadata.modules

if (!exportsMap) {
exportsMap = {}
exports.specifiers.forEach(m => {
exportsMap[m.exported] = m.local
})
metadata.exportsMap = exportsMap
}

return metadata
}

const defaultStylePart = {
type: 'style',
content: '\n',
Expand Down Expand Up @@ -146,5 +178,7 @@ module.exports = {
htmlBeautify,
getBabelrc,
getPathPrefix,
getPageSrc
getPageSrc,
getImportsMap,
getExportsMap
}