Skip to content

Commit d363c09

Browse files
committed
add: option for names
1 parent 4d5506f commit d363c09

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

index.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// Babel plugin to transform class names into cssobj localized
22

3+
// imports
34
var util = require('util')
45
var converter = require('cssobj-converter')
56
var yaml = require('js-yaml')
7+
var objutil = require('objutil')
8+
var syntaxJsx = require('babel-plugin-syntax-jsx')
9+
10+
// constants
611
var templateDelimiter = '_cssobj_template_delimiter_'
712

8-
function transformObjecctToFunction (babel, code) {
13+
// helpers
14+
function transformObjecctToFunction (babel, code, option) {
915
var result = babel.transform('!' + code, {
10-
plugins: ['./transform-plugins']
16+
plugins: [['./transform-plugins', option]]
1117
})
1218
// console.log(result.code)
1319
// result.ast.program have: .cssobjConfig, .cssobjImports
@@ -17,15 +23,17 @@ function transformObjecctToFunction (babel, code) {
1723
module.exports = function (babel) {
1824
var t = babel.types
1925
return {
20-
inherits: require('babel-plugin-syntax-jsx'),
26+
inherits: syntaxJsx,
2127
visitor: {
22-
Program (path) {
23-
// console.log(util.inspect(path, {showHidden: false, depth: 5}))
24-
},
2528
TaggedTemplateExpression (path, state) {
2629
var root = path.hub.file
2730
var source = root.code
28-
var cssobjName = state.cssobjName || (state.opts && state.opts.cssobjName)
31+
var option = state.opts // babel5: state===opts
32+
option = objutil.defaults(option, {
33+
names: {
34+
cssobj: {name: 'cssobj', path: 'cssobj'}
35+
}
36+
})
2937
var node = path.node
3038
// console.log(node)
3139
var yamlRe = /\n\s*---\s*\n/
@@ -61,7 +69,7 @@ module.exports = function (babel) {
6169
return v + exps.shift()
6270
})
6371
.join('')
64-
cssobjConfigNode = transformObjecctToFunction(babel, cssobjConfig)
72+
cssobjConfigNode = transformObjecctToFunction(babel, cssobjConfig, option)
6573
root.path.unshiftContainer('body', cssobjConfigNode.ast.program.cssobjImports)
6674
config = cssobjConfigNode.code.substr(1).replace(/;+$/, '')
6775
}
@@ -80,12 +88,14 @@ module.exports = function (babel) {
8088
.join('')
8189
// got css object
8290
// console.log(objStr)
91+
var cssobjNS = option.names['cssobj']
92+
var cssobjName = cssobjNS.name || 'cssobj'
8393
root.path.unshiftContainer('body', t.importDeclaration(
84-
[t.importDefaultSpecifier(t.identifier('cssobj'))],
85-
t.stringLiteral('cssobj')
94+
[t.importDefaultSpecifier(t.identifier(cssobjName))],
95+
t.stringLiteral(cssobjNS.path || 'cssobj')
8696
))
8797

88-
path.replaceWithSourceString(`cssobj(${config}, ${objStr})`)
98+
path.replaceWithSourceString(`${cssobjName} (${config}, ${objStr})`)
8999
}
90100
},
91101
CallExpression (path, state) {

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"index.js"
1111
],
1212
"scripts": {
13+
"objutil": "rollup -c ./node_modules/objutil/rollup.config.js --api defaults",
14+
"build": "npm run objutil",
15+
"pretest": "npm run build",
1316
"test": "istanbul cover _mocha",
1417
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
1518
},
@@ -42,6 +45,7 @@
4245
},
4346
"dependencies": {
4447
"babel-plugin-syntax-jsx": "^6.18.0",
45-
"cssobj-converter": "^2.1.6"
48+
"cssobj-converter": "*",
49+
"objutil": "^2.0.6"
4650
}
4751
}

transform-plugins.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// transform from object into cssobj config, with plugins transformed
2+
var objutil = require('objutil')
23

34
module.exports = function (babel) {
45
var t = babel.types
@@ -7,17 +8,21 @@ module.exports = function (babel) {
78
visitor: {
89
Program (path, state) {
910
var firstExp = path.node.body[0]
10-
11+
var option = state.opts
12+
option = objutil.defaults(
13+
option,
14+
{ names: {} }
15+
)
1116
// only transform !{plugins:[]}
12-
if(!t.isExpressionStatement(firstExp)
13-
|| !t.isUnaryExpression(firstExp.expression, {operator: '!'})
14-
|| !t.isObjectExpression(firstExp.expression.argument)) return
17+
if (!t.isExpressionStatement(firstExp)
18+
|| !t.isUnaryExpression(firstExp.expression, {operator: '!'})
19+
|| !t.isObjectExpression(firstExp.expression.argument)) return
1520

1621
// get target expression
1722
var node = firstExp.expression.argument
1823
// get "plugins" prop, it's from JSON.stringify
1924

20-
var pluginsNode = node.properties.filter(function(v) {
25+
var pluginsNode = node.properties.filter(function (v) {
2126
return getKeyValue(v) === 'plugins'
2227
&& t.isArrayExpression(v.value)
2328
}).shift()
@@ -29,21 +34,22 @@ module.exports = function (babel) {
2934
var cssobjImports = path.node.cssobjImports = []
3035
for (var v, prop, value, i = 0; i < elements.length; i++) {
3136
v = elements[i]
32-
if (t.isLiteral(v) && (value='', prop = v.value)
37+
if (t.isLiteral(v) && (value = '', prop = v.value)
3338
|| t.isObjectExpression(v)
3439
&& v.properties.length == 1
3540
&& (prop = getKeyValue(v.properties[0]))
3641
// plugin name cannot be below keywords
3742
&& ['selector', 'value', 'post'].indexOf(prop) < 0
3843
&& (value = v.properties[0].value)) {
39-
var pluginIden = 'cssobj_plugin_' + prop.replace(/-/g, '_')
44+
var pluginNS = option.names[prop]
45+
var pluginIden = pluginNS && pluginNS.name || 'cssobj_plugin_' + prop.replace(/-/g, '_')
4046
elements[i] = t.callExpression(
4147
t.identifier(pluginIden),
4248
value ? [value] : []
4349
)
4450
cssobjImports.push(t.importDeclaration(
4551
[t.importDefaultSpecifier(t.identifier(pluginIden))],
46-
t.stringLiteral('cssobj-plugin-'+prop)
52+
t.stringLiteral(pluginNS && pluginNS.path || 'cssobj-plugin-' + prop)
4753
))
4854
}
4955
}
@@ -57,5 +63,4 @@ module.exports = function (babel) {
5763
if (t.isLiteral(v.key)) return v.key.value
5864
if (t.isIdentifier(v.key)) return v.key.name
5965
}
60-
6166
}

0 commit comments

Comments
 (0)