Skip to content

Commit

Permalink
Handle new Bublé template string compilation. (#155)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
goto-bus-stop authored Jun 27, 2018
1 parent 267523a commit f76f3ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"@choojs/findup": "^0.2.0",
"acorn-node": "^1.3.0",
"estree-is-member-expression": "^1.0.0",
"fast-json-parse": "^1.0.2",
"insert-css": "^2.0.0",
"map-limit": "0.0.1",
Expand Down
32 changes: 26 additions & 6 deletions transform.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const isMemberExpression = require('estree-is-member-expression')
const cssResolve = require('style-resolve').sync
const transformAst = require('transform-ast')
const staticEval = require('static-eval')
Expand All @@ -18,6 +19,13 @@ function isBabelTemplateDefinition (node) {
node.callee.type === 'Identifier' && node.callee.name === '_taggedTemplateLiteral'
}

function isBubleTemplateDefinition (node) {
return node.type === 'CallExpression' &&
isMemberExpression(node.callee, 'Object.freeze') &&
node.arguments[0] && node.arguments[0].type === 'ArrayExpression' &&
node.arguments[0].elements.every(function (el) { return el.type === 'Literal' })
}

// inline sheetify transform for browserify
// obj -> (str, opts) -> str
function transform (filename, options) {
Expand Down Expand Up @@ -61,6 +69,7 @@ function transform (filename, options) {
// judge us too harshly, we'll work on perf ✨soon✨ -yw
const nodes = []
const babelTemplateObjects = {}
const bubleTemplateObjects = {}
const src = Buffer.concat(bufs).toString('utf8')
var mname = null
var ast
Expand Down Expand Up @@ -117,11 +126,17 @@ function transform (filename, options) {
var css
var elements

if (node.type === 'VariableDeclarator' && node.init && isBabelTemplateDefinition(node.init)) {
// Babel generates helper calls like
// _taggedTemplateLiteral([":host .class { color: hotpink; }"], [":host .class { color: hotpink; }"])
// we only keep the "cooked" part
babelTemplateObjects[node.id.name] = node.init.arguments[0]
if (node.type === 'VariableDeclarator' && node.init) {
if (isBabelTemplateDefinition(node.init)) {
// Babel generates helper calls like
// _taggedTemplateLiteral([":host .class { color: hotpink; }"], [":host .class { color: hotpink; }"])
// we only keep the "cooked" part
babelTemplateObjects[node.id.name] = node.init.arguments[0]
} else if (isBubleTemplateDefinition(node.init)) {
// Buble generates helper calls like
// Object.freeze([":host .class { color: hotpink; }"])
bubleTemplateObjects[node.id.name] = node.init.arguments[0]
}
}

if (node.type === 'TemplateLiteral' && node.parent && node.parent.tag) {
Expand All @@ -146,7 +161,12 @@ function transform (filename, options) {
} else if (node.arguments[0] && node.arguments[0].type === 'Identifier') {
// Babel generates code like
// sheetify(_templateObject)
elements = babelTemplateObjects[node.arguments[0].name].elements
var arg = node.arguments[0].name
if (babelTemplateObjects[arg]) {
elements = babelTemplateObjects[arg].elements
} else if (bubleTemplateObjects[arg]) {
elements = bubleTemplateObjects[arg].elements
}
}

if (elements) {
Expand Down

0 comments on commit f76f3ca

Please sign in to comment.