Skip to content

Commit f76f3ca

Browse files
Handle new Bublé template string compilation. (#155)
bublejs/buble#67 changed Bublé template strings to compile similarly to Babel ones, for spec reasons. With this patch, the new style as well as the old style are detected correctly.
1 parent 267523a commit f76f3ca

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"dependencies": {
2424
"@choojs/findup": "^0.2.0",
2525
"acorn-node": "^1.3.0",
26+
"estree-is-member-expression": "^1.0.0",
2627
"fast-json-parse": "^1.0.2",
2728
"insert-css": "^2.0.0",
2829
"map-limit": "0.0.1",

transform.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const isMemberExpression = require('estree-is-member-expression')
12
const cssResolve = require('style-resolve').sync
23
const transformAst = require('transform-ast')
34
const staticEval = require('static-eval')
@@ -18,6 +19,13 @@ function isBabelTemplateDefinition (node) {
1819
node.callee.type === 'Identifier' && node.callee.name === '_taggedTemplateLiteral'
1920
}
2021

22+
function isBubleTemplateDefinition (node) {
23+
return node.type === 'CallExpression' &&
24+
isMemberExpression(node.callee, 'Object.freeze') &&
25+
node.arguments[0] && node.arguments[0].type === 'ArrayExpression' &&
26+
node.arguments[0].elements.every(function (el) { return el.type === 'Literal' })
27+
}
28+
2129
// inline sheetify transform for browserify
2230
// obj -> (str, opts) -> str
2331
function transform (filename, options) {
@@ -61,6 +69,7 @@ function transform (filename, options) {
6169
// judge us too harshly, we'll work on perf ✨soon✨ -yw
6270
const nodes = []
6371
const babelTemplateObjects = {}
72+
const bubleTemplateObjects = {}
6473
const src = Buffer.concat(bufs).toString('utf8')
6574
var mname = null
6675
var ast
@@ -117,11 +126,17 @@ function transform (filename, options) {
117126
var css
118127
var elements
119128

120-
if (node.type === 'VariableDeclarator' && node.init && isBabelTemplateDefinition(node.init)) {
121-
// Babel generates helper calls like
122-
// _taggedTemplateLiteral([":host .class { color: hotpink; }"], [":host .class { color: hotpink; }"])
123-
// we only keep the "cooked" part
124-
babelTemplateObjects[node.id.name] = node.init.arguments[0]
129+
if (node.type === 'VariableDeclarator' && node.init) {
130+
if (isBabelTemplateDefinition(node.init)) {
131+
// Babel generates helper calls like
132+
// _taggedTemplateLiteral([":host .class { color: hotpink; }"], [":host .class { color: hotpink; }"])
133+
// we only keep the "cooked" part
134+
babelTemplateObjects[node.id.name] = node.init.arguments[0]
135+
} else if (isBubleTemplateDefinition(node.init)) {
136+
// Buble generates helper calls like
137+
// Object.freeze([":host .class { color: hotpink; }"])
138+
bubleTemplateObjects[node.id.name] = node.init.arguments[0]
139+
}
125140
}
126141

127142
if (node.type === 'TemplateLiteral' && node.parent && node.parent.tag) {
@@ -146,7 +161,12 @@ function transform (filename, options) {
146161
} else if (node.arguments[0] && node.arguments[0].type === 'Identifier') {
147162
// Babel generates code like
148163
// sheetify(_templateObject)
149-
elements = babelTemplateObjects[node.arguments[0].name].elements
164+
var arg = node.arguments[0].name
165+
if (babelTemplateObjects[arg]) {
166+
elements = babelTemplateObjects[arg].elements
167+
} else if (bubleTemplateObjects[arg]) {
168+
elements = bubleTemplateObjects[arg].elements
169+
}
150170
}
151171

152172
if (elements) {

0 commit comments

Comments
 (0)