Skip to content

Commit e7c08b2

Browse files
authored
feat(build): components field support PascalCase (#143)
1 parent c4dfa54 commit e7c08b2

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

packages/mars-build/src/compiler/script/babel-plugin-script-post.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
/* eslint-disable fecs-camelcase */
77
/* eslint-disable babel/new-cap */
8+
const {hyphenate} = require('../../helper/util');
9+
810
module.exports = function getVisitor(options = {}) {
911
return ({types: t}) => {
1012
const {
@@ -33,6 +35,7 @@ module.exports = function getVisitor(options = {}) {
3335
: path.node.key.type === 'StringLiteral'
3436
? path.node.key.value
3537
: null;
38+
componentName = hyphenate(componentName);
3639
if (componentName && !componentsInUsed[componentName].using) {
3740
path.remove();
3841
}

packages/mars-build/src/compiler/script/babel-plugin-script.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
/* eslint-disable fecs-camelcase */
77
/* eslint-disable babel/new-cap */
8+
const {hyphenate} = require('../../helper/util');
9+
810
function getPlainObjectNodeValue(node, path, t) {
911
let result;
1012
if (t.isObjectExpression(node)) {
@@ -90,7 +92,9 @@ const getPropertyVisitor = (t, options) => {
9092
throw path.buildCodeFrameError(`cannot find binding for component "${p.value.name}"`);
9193
}
9294

93-
const keyName = t.isLiteral(p.key) ? p.key.value : p.key.name;
95+
let keyName = t.isLiteral(p.key) ? p.key.value : p.key.name;
96+
keyName = hyphenate(keyName);
97+
9498
const bindPath = binding.path;
9599
const bindParentNode = bindPath.parent;
96100
const bindNode = bindPath.node;

packages/mars-build/src/h5/transform/plugins/transformScriptPlugin.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
/* eslint-disable fecs-camelcase */
77

88
/* eslint-disable babel/new-cap */
9-
109
// 小程序page生命周期
1110
const PAGE_LIFECYCLE_HOOKS = {
1211
onLoad: true,
@@ -128,6 +127,7 @@ function getPlainObjectNodeValue(node, path, t) {
128127
return result;
129128
}
130129

130+
const {hyphenate} = require('../../../helper/util');
131131
/* eslint-disable */
132132
const Property = (t, options) => {
133133
return {
@@ -166,13 +166,16 @@ const Property = (t, options) => {
166166
let props = JSON.parse(JSON.stringify(path.node.value.properties));
167167
props.forEach(p => {
168168
if (t.isIdentifier(p.value)) {
169+
let keyName = t.isLiteral(p.key) ? p.key.value : p.key.name;
170+
keyName = hyphenate(keyName);
171+
p.key = t.stringLiteral(keyName);
172+
169173
const bindPath = path.scope.bindings[p.value.name].path;
170174
const bindParentNode = bindPath.parent;
171175
const bindVaule = bindParentNode.source;
172176
bindParentNode.source = t.stringLiteral(bindVaule.value + '.vue');
173177
p.value = bindParentNode.source;
174178
}
175-
176179
});
177180

178181
path.node.value.properties.push();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @file util
3+
* @author zhangwentao
4+
*/
5+
6+
/**
7+
* Create a cached version of a pure function.
8+
*/
9+
10+
function cached(fn) {
11+
const cache = Object.create(null);
12+
return function cachedFn(str) {
13+
const hit = cache[str];
14+
return hit || (cache[str] = fn(str));
15+
};
16+
}
17+
18+
/**
19+
* Camelize a hyphen-delimited string.
20+
*/
21+
22+
const camelizeRE = /-(\w)/g;
23+
const camelize = cached(str => {
24+
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '');
25+
});
26+
27+
/**
28+
* Capitalize a string.
29+
*/
30+
const capitalize = cached(str => {
31+
return str.charAt(0).toUpperCase() + str.slice(1);
32+
});
33+
34+
/**
35+
* Hyphenate a camelCase string.
36+
*/
37+
const hyphenateRE = /\B([A-Z])/g;
38+
const hyphenate = cached(str => {
39+
return str.replace(hyphenateRE, '-$1').toLowerCase();
40+
});
41+
42+
exports.camelize = camelize;
43+
exports.capitalize = capitalize;
44+
exports.hyphenate = hyphenate;

packages/mars-build/src/swan/transform/transform.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ function transAttrs(node, options) {
4747
attrs[key] = val;
4848
}
4949
}
50-
5150
});
5251

5352
if (null !== showAttr) {

0 commit comments

Comments
 (0)