Skip to content

Commit 4d5764c

Browse files
author
v_zhihzhou
committed
Webpack4 and Babel@7(@babel) Support
1 parent 15e7455 commit 4d5764c

File tree

3 files changed

+92
-26
lines changed

3 files changed

+92
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
>Webpack4 loader for mpvue components
44
5-
本仓库是 fork 自 [mpvue-loader](http://mpvue.com/build/mpvue-loader) 修改而来,主要为 webpack4 打包 mpvue components 提供能力。
5+
本仓库是 fork 自 [mpvue-loader](http://mpvue.com/build/mpvue-loader) 修改而来,主要为 webpack4 和 babel@7(@babel) 打包 mpvue components 提供能力。
66

77
更多详细文档请查阅 [mpvue-loader](http://mpvue.com/build/mpvue-loader)
88

lib/mp-compiler/parse.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
// babel-plugin-parse-mp-info.js
22

3-
const generate = require('babel-generator').default
3+
const generate = require('@babel/generator').default
44
const babelon = require('babelon')
5-
6-
function getImportsMap (metadata) {
7-
let { importsMap } = metadata
8-
const { imports } = metadata.modules
9-
10-
if (!importsMap) {
11-
importsMap = {}
12-
imports.forEach(m => {
13-
m.specifiers.forEach(v => {
14-
importsMap[v.local] = m.source
15-
})
16-
})
17-
metadata.importsMap = importsMap
18-
}
19-
20-
return metadata
21-
}
5+
const { getImportsMap } = require('./util')
226

237
// 解析 config
248
const traverseConfigVisitor = {
@@ -44,8 +28,14 @@ const configVisitor = {
4428
path.remove()
4529
},
4630
NewExpression: function (path) {
47-
const { metadata } = path.hub.file
48-
const { importsMap } = getImportsMap(metadata)
31+
const file = path.hub.file
32+
const { metadata } = file
33+
// enhance: 修复在babel@7(@babel)下获取以下参数的方式
34+
const importsData = getImportsMap(file)
35+
let importsMap = {}
36+
importsData.forEach(item => {
37+
importsMap = Object.assign(importsMap, item.importsMap)
38+
})
4939

5040
const calleeName = path.node.callee.name
5141
const isVue = /vue$/.test(importsMap[calleeName])
@@ -76,8 +66,10 @@ const traverseComponentsVisitor = {
7666
}
7767
path.stop()
7868

79-
const { metadata } = path.hub.file
80-
const { importsMap } = getImportsMap(metadata)
69+
const file = path.hub.file
70+
const { metadata } = file
71+
// enhance: 修复在babel@7(@babel)下获取以下参数的方式
72+
const importsMap = getImportsMap(file)
8173

8274
// 找到所有的 imports
8375
const { properties } = path.node.value
@@ -108,7 +100,8 @@ let globalComponents = {}
108100
const globalComponentsVisitor = {
109101
CallExpression (path) {
110102
const { callee, arguments: args } = path.node
111-
const { metadata } = path.hub.file
103+
const file = path.hub.file
104+
const { metadata } = file
112105
if (!callee.object || !callee.property) {
113106
return
114107
}
@@ -119,7 +112,8 @@ const globalComponentsVisitor = {
119112
if (!args[1]) {
120113
throw new Error('Vue.component()需要两个参数')
121114
}
122-
const { importsMap } = getImportsMap(metadata)
115+
// enhance: 修复在babel@7(@babel)下获取以下参数的方式
116+
const { importsMap } = getImportsMap(file)
123117
globalComponents[args[0].value] = importsMap[args[1].name]
124118
}
125119
metadata.globalComponents = globalComponents

lib/mp-compiler/util.js

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22
const fs = require('fs')
3+
const { isModuleDeclaration } = require('babel-types')
34
const resolveSrc = require('../utils/resolve-src')
45
const pagesNameMap = Object.create(null)
56

@@ -11,6 +12,76 @@ function getFileInfo (resourcePath) {
1112
return pagesNameMap[resourcePath] || {}
1213
}
1314

15+
// enhance: 修复在babel@7(@babel)下获取以下参数的方式
16+
function getImportsMap (file) {
17+
const imports = []
18+
if (!file.ast) return imports
19+
20+
let isModule = false
21+
22+
for (const node of file.ast.program.body) {
23+
if (isModuleDeclaration(node)) {
24+
isModule = true
25+
break
26+
}
27+
}
28+
29+
if (isModule) {
30+
file.path.traverse({
31+
ImportDeclaration: {
32+
exit (path) {
33+
const { node } = path
34+
const imported = []
35+
const importsMap = {}
36+
const specifiers = []
37+
38+
for (const specifier of path.get('specifiers')) {
39+
const local = specifier.node.local.name
40+
41+
importsMap[local] = node.source.value
42+
43+
if (specifier.isImportDefaultSpecifier()) {
44+
imported.push('default')
45+
specifiers.push({
46+
kind: 'named',
47+
imported: 'default',
48+
local
49+
})
50+
}
51+
52+
if (specifier.isImportSpecifier()) {
53+
const importedName = specifier.node.imported.name
54+
imported.push(importedName)
55+
specifiers.push({
56+
kind: 'named',
57+
imported: importedName,
58+
local
59+
})
60+
}
61+
62+
if (specifier.isImportNamespaceSpecifier()) {
63+
imported.push('*')
64+
specifiers.push({
65+
kind: 'namespace',
66+
local
67+
})
68+
}
69+
}
70+
71+
imports.push({
72+
source: node.source.value,
73+
imported,
74+
specifiers,
75+
importsMap
76+
})
77+
}
78+
}
79+
})
80+
}
81+
82+
return imports
83+
}
84+
1485
// 单文件的名字+hash
1586
// TODO: 调试时取个全名
1687
var hash = require('hash-sum')
@@ -149,5 +220,6 @@ module.exports = {
149220
getSlots,
150221
htmlBeautify,
151222
getBabelrc,
152-
getPageSrc
223+
getPageSrc,
224+
getImportsMap
153225
}

0 commit comments

Comments
 (0)