From 43446e8f67b988d1f506e032f2e6c6db192702ef Mon Sep 17 00:00:00 2001 From: F-loat <945852046@qq.com> Date: Sat, 19 May 2018 16:04:51 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E9=97=B4?= =?UTF-8?q?=E6=8E=A5=E5=BC=95=E7=94=A8=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mp-compiler/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index 345a8b8..4413afb 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -248,7 +248,10 @@ function resolveSrc (originComponents, components, resolveFn, context) { resolveFn(context, originComponents[k], (err, realSrc) => { if (err) return reject(err) const com = covertCCVar(k) - const comName = getCompNameBySrc(realSrc) + const realComSrc = /\.js$/.test(realSrc) + ? path.join(path.dirname(realSrc), `${com}.vue`) + : realSrc + const comName = getCompNameBySrc(realComSrc) components[com] = { src: comName, name: comName } resolve() }) From c420cdc7db9ae52420f58476443a414145915f66 Mon Sep 17 00:00:00 2001 From: F-loat <945852046@qq.com> Date: Sun, 20 May 2018 15:10:28 +0800 Subject: [PATCH 2/6] =?UTF-8?q?refactor:=20=E9=80=9A=E8=BF=87=20babel=20?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=AE=9E=E9=99=85=E5=BC=95=E7=94=A8=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mp-compiler/index.js | 26 +++++++++++++++++++------- lib/mp-compiler/parse.js | 18 +----------------- lib/mp-compiler/util.js | 20 +++++++++++++++++++- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index 4413afb..90caf5d 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -20,7 +20,8 @@ const { getSlots, htmlBeautify, getBabelrc, - getPageSrc + getPageSrc, + getImportsMap } = require('./util') let slotsHookAdded = false @@ -248,12 +249,23 @@ function resolveSrc (originComponents, components, resolveFn, context) { resolveFn(context, originComponents[k], (err, realSrc) => { if (err) return reject(err) const com = covertCCVar(k) - const realComSrc = /\.js$/.test(realSrc) - ? path.join(path.dirname(realSrc), `${com}.vue`) - : realSrc - const comName = getCompNameBySrc(realComSrc) - components[com] = { src: comName, name: comName } - resolve() + const isScript = /\.js$/.test(realSrc) + if (isScript) { + const scriptContent = fs.readFileSync(realSrc).toString() + const result = babel.transform(scriptContent) + const { importsMap } = getImportsMap(result.metadata) + const source = importsMap[k] + resolveFn(path.dirname(realSrc), source, (err, realComSrc) => { + if (err) return reject(err) + const comName = getCompNameBySrc(realComSrc) + components[com] = { src: comName, name: comName } + resolve() + }) + } else { + const comName = getCompNameBySrc(realSrc) + components[com] = { src: comName, name: comName } + resolve() + } }) }) })) diff --git a/lib/mp-compiler/parse.js b/lib/mp-compiler/parse.js index c15cd7e..209ce0a 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 = { diff --git a/lib/mp-compiler/util.js b/lib/mp-compiler/util.js index 657cd1f..814a95a 100644 --- a/lib/mp-compiler/util.js +++ b/lib/mp-compiler/util.js @@ -119,6 +119,23 @@ 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 +} + const defaultStylePart = { type: 'style', content: '\n', @@ -146,5 +163,6 @@ module.exports = { htmlBeautify, getBabelrc, getPathPrefix, - getPageSrc + getPageSrc, + getImportsMap } From 84a9b7be7208588356e20c47de1b2f5a3fe1a7b0 Mon Sep 17 00:00:00 2001 From: F-loat <945852046@qq.com> Date: Sun, 20 May 2018 16:13:47 +0800 Subject: [PATCH 3/6] =?UTF-8?q?optimize:=20=E9=80=92=E5=BD=92=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E5=AE=9E=E9=99=85=E5=BC=95=E7=94=A8=E6=96=87=E4=BB=B6?= =?UTF-8?q?=20=20-=20=E6=94=AF=E6=8C=81=E5=A4=9A=E7=BA=A7=E9=97=B4?= =?UTF-8?q?=E6=8E=A5=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mp-compiler/index.js | 42 ++++++++++++++++++++++++---------------- lib/mp-compiler/parse.js | 5 ++++- lib/mp-compiler/util.js | 18 ++++++++++++++++- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index 90caf5d..bc5e674 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -21,7 +21,8 @@ const { htmlBeautify, getBabelrc, getPageSrc, - getImportsMap + getImportsMap, + getExportsMap } = require('./util') let slotsHookAdded = false @@ -243,29 +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]] + resolveFn(path.dirname(realSrc), source, (err, realComSrc) => { + if (err) return reject(err) + resolve(resolveRealComSrc(realComSrc, exportsMap[m], resolveFn)) + }) + } 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 isScript = /\.js$/.test(realSrc) - if (isScript) { - const scriptContent = fs.readFileSync(realSrc).toString() - const result = babel.transform(scriptContent) - const { importsMap } = getImportsMap(result.metadata) - const source = importsMap[k] - resolveFn(path.dirname(realSrc), source, (err, realComSrc) => { - if (err) return reject(err) - const comName = getCompNameBySrc(realComSrc) - components[com] = { src: comName, name: comName } - resolve() - }) - } else { - const comName = getCompNameBySrc(realSrc) + resolveRealComSrc(realSrc, originComponents[k].module, resolveFn).then(realComSrc => { + const comName = getCompNameBySrc(realComSrc) components[com] = { src: comName, name: comName } resolve() - } + }) }) }) })) diff --git a/lib/mp-compiler/parse.js b/lib/mp-compiler/parse.js index 209ce0a..9114787 100644 --- a/lib/mp-compiler/parse.js +++ b/lib/mp-compiler/parse.js @@ -70,7 +70,10 @@ 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 814a95a..c7aa691 100644 --- a/lib/mp-compiler/util.js +++ b/lib/mp-compiler/util.js @@ -136,6 +136,21 @@ function getImportsMap (metadata) { 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', @@ -164,5 +179,6 @@ module.exports = { getBabelrc, getPathPrefix, getPageSrc, - getImportsMap + getImportsMap, + getExportsMap } From 0fc4375fa53043f5704f6313e8e04888f3c7408e Mon Sep 17 00:00:00 2001 From: F-loat <945852046@qq.com> Date: Sun, 20 May 2018 16:29:06 +0800 Subject: [PATCH 4/6] =?UTF-8?q?chore:=20=E6=B7=BB=E5=8A=A0=E9=83=A8?= =?UTF-8?q?=E5=88=86=E7=BB=84=E4=BB=B6=E8=A7=A3=E6=9E=90=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mp-compiler/parse.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mp-compiler/parse.js b/lib/mp-compiler/parse.js index 9114787..e53ff80 100644 --- a/lib/mp-compiler/parse.js +++ b/lib/mp-compiler/parse.js @@ -70,6 +70,7 @@ const traverseComponentsVisitor = { const k = p.key.name || p.key.value const v = p.value.name || p.value.value + // 保留组件引用路径及模块名 components[k] = { src: importsMap[v], module: v From b86e03569319987a3799b64458099d80ddfa8cbe Mon Sep 17 00:00:00 2001 From: F-loat <945852046@qq.com> Date: Sun, 20 May 2018 16:34:27 +0800 Subject: [PATCH 5/6] =?UTF-8?q?fix:=20=E6=94=AF=E6=8C=81=E9=97=B4=E6=8E=A5?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E9=BB=98=E8=AE=A4=E5=AF=BC=E5=87=BA=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mp-compiler/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index bc5e674..92a1edb 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -252,7 +252,7 @@ function resolveRealComSrc (realSrc, m, resolveFn) { const result = babel.transform(scriptContent) const { importsMap } = getImportsMap(result.metadata) const { exportsMap } = getExportsMap(result.metadata) - const source = importsMap[exportsMap[m]] + const source = importsMap[exportsMap[m] || m] resolveFn(path.dirname(realSrc), source, (err, realComSrc) => { if (err) return reject(err) resolve(resolveRealComSrc(realComSrc, exportsMap[m], resolveFn)) From 3d9f94907bec5eb70bb31ad9338e1c6922fb3af5 Mon Sep 17 00:00:00 2001 From: F-loat <945852046@qq.com> Date: Sun, 20 May 2018 17:26:41 +0800 Subject: [PATCH 6/6] =?UTF-8?q?optimize:=20=E6=B7=BB=E5=8A=A0=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E8=A7=A3=E6=9E=90=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mp-compiler/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mp-compiler/index.js b/lib/mp-compiler/index.js index 92a1edb..07bfbc8 100644 --- a/lib/mp-compiler/index.js +++ b/lib/mp-compiler/index.js @@ -255,7 +255,7 @@ function resolveRealComSrc (realSrc, m, resolveFn) { const source = importsMap[exportsMap[m] || m] resolveFn(path.dirname(realSrc), source, (err, realComSrc) => { if (err) return reject(err) - resolve(resolveRealComSrc(realComSrc, exportsMap[m], resolveFn)) + resolveRealComSrc(realComSrc, exportsMap[m], resolveFn).then(resolve).catch(reject) }) } else { resolve(realSrc) @@ -273,7 +273,7 @@ function resolveSrc (originComponents, components, resolveFn, context) { const comName = getCompNameBySrc(realComSrc) components[com] = { src: comName, name: comName } resolve() - }) + }).catch(reject) }) }) }))