diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index 345a8b8..07bfbc8 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -20,7 +20,9 @@ const { getSlots, htmlBeautify, getBabelrc, - getPageSrc + getPageSrc, + getImportsMap, + getExportsMap } = require('./util') let slotsHookAdded = false @@ -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) }) }) })) diff --git a/lib/mp-compiler/parse.js b/lib/mp-compiler/parse.js index c15cd7e..e53ff80 100644 --- a/lib/mp-compiler/parse.js +++ b/lib/mp-compiler/parse.js @@ -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 = { @@ -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 diff --git a/lib/mp-compiler/util.js b/lib/mp-compiler/util.js index 657cd1f..c7aa691 100644 --- a/lib/mp-compiler/util.js +++ b/lib/mp-compiler/util.js @@ -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', @@ -146,5 +178,7 @@ module.exports = { htmlBeautify, getBabelrc, getPathPrefix, - getPageSrc + getPageSrc, + getImportsMap, + getExportsMap }