babel-plugin-htmlbars-inline-precompile
Advanced tools
Comparing version 0.1.0 to 0.2.0
## 0.0.5 (2015-07-11) | ||
- [Allow single string parameter - #7](https://github.com/pangratz/babel-plugin-htmlbars-inline-precompile/commit/109b5f5c19e1c61654581b33e12f886b39e16412) | ||
- [Allow single string parameter - #7](https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile/commit/109b5f5c19e1c61654581b33e12f886b39e16412) | ||
## 0.0.4 (2015-06-30) | ||
- [Fix "typo" for method which should replace deprecated behavior](https://github.com/pangratz/babel-plugin-htmlbars-inline-precompile/commit/f2458bede137297bd13e8308a35a489754eadc97) | ||
- [Fix "typo" for method which should replace deprecated behavior](https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile/commit/f2458bede137297bd13e8308a35a489754eadc97) | ||
## 0.0.3 (2015-06-26) | ||
- [Replace deprecated behavior of replacing source string - #6](https://github.com/pangratz/babel-plugin-htmlbars-inline-precompile/pull/6). Thanks [@rwjblue](https://github.com/rwjblue). | ||
- [Replace deprecated behavior of replacing source string - #6](https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile/pull/6). Thanks [@rwjblue](https://github.com/rwjblue). | ||
## 0.0.2 (2015-06-13) | ||
- [Replace remove() with dangerouslyRemove() - #4](https://github.com/pangratz/babel-plugin-htmlbars-inline-precompile/pull/4). Thanks [@alisdair](https://github.com/alisdair). | ||
- [Replace remove() with dangerouslyRemove() - #4](https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile/pull/4). Thanks [@alisdair](https://github.com/alisdair). | ||
@@ -17,0 +17,0 @@ ## 0.0.1 (2015-05-06) |
130
index.js
@@ -1,94 +0,72 @@ | ||
module.exports = function(precompile, _options) { | ||
var options = _options || {}; | ||
'use strict'; | ||
function htmlbarsInlineCompilerPlugin(babel) { | ||
var t = babel.types; | ||
module.exports = function(babel) { | ||
let t = babel.types; | ||
var replaceNodeWithPrecompiledTemplate = function(node, template) { | ||
var compiledTemplateString = "Ember.HTMLBars.template(" + precompile(template) + ")"; | ||
return { | ||
visitor: { | ||
Program: { | ||
enter: function(path, state) { | ||
state.hbsImports = []; | ||
}, | ||
exit: function(path, state) { | ||
state.hbsImports.forEach(function(path) { | ||
path.remove(); | ||
}); | ||
}, | ||
}, | ||
// Prefer calling replaceWithSourceString if it is present. | ||
// this prevents a deprecation warning in Babel 5.6.7+. | ||
// | ||
// TODO: delete the fallback once we only support babel >= 5.6.7. | ||
if (node.replaceWithSourceString) { | ||
node.replaceWithSourceString(compiledTemplateString); | ||
} else { | ||
return compiledTemplateString; | ||
} | ||
} | ||
return new babel.Transformer('htmlbars-inline-precompile', { | ||
ImportDeclaration: function(node, parent, scope, file) { | ||
ImportDeclaration: function(path, state) { | ||
let node = path.node; | ||
if (t.isLiteral(node.source, { value: "htmlbars-inline-precompile" })) { | ||
var first = node.specifiers && node.specifiers[0]; | ||
if (t.isImportDefaultSpecifier(first)) { | ||
file.importSpecifier = first.local.name; | ||
} else { | ||
var input = file.code; | ||
var usedImportStatement = input.slice(node.start, node.end); | ||
var msg = "Only `import hbs from 'htmlbars-inline-precompile'` is supported. You used: `" + usedImportStatement + "`"; | ||
throw file.errorWithNode(node, msg); | ||
let first = node.specifiers && node.specifiers[0]; | ||
if (!t.isImportDefaultSpecifier(first)) { | ||
let input = state.file.code; | ||
let usedImportStatement = input.slice(node.start, node.end); | ||
let msg = `Only \`import hbs from 'htmlbars-inline-precompile'\` is supported. You used: \`${usedImportStatement}\``; | ||
throw path.buildCodeFrameError(msg); | ||
} | ||
// Prefer calling dangerouslyRemove instead of remove (if present) to | ||
// suppress a deprecation warning. | ||
// | ||
// TODO: delete the fallback once we only support babel >= 5.5.0. | ||
if (typeof this.dangerouslyRemove === 'function') { | ||
this.dangerouslyRemove(); | ||
} else { | ||
this.remove(); | ||
} | ||
state.hbsImports.push(path); | ||
} | ||
}, | ||
CallExpression: function(node, parent, scope, file) { | ||
if (t.isIdentifier(node.callee, { name: file.importSpecifier })) { | ||
var argumentErrorMsg = "hbs should be invoked with a single argument: the template string"; | ||
if (node.arguments.length !== 1) { | ||
throw file.errorWithNode(node, argumentErrorMsg); | ||
} | ||
Identifier: function(path, state) { | ||
if (path.referencesImport('htmlbars-inline-precompile', 'default')) { | ||
let parent = path.parentPath; | ||
var template = node.arguments[0].value; | ||
if (typeof template !== "string") { | ||
throw file.errorWithNode(node, argumentErrorMsg); | ||
} | ||
let template; | ||
if (parent.isCallExpression({ callee: path.node })) { | ||
let argumentErrorMsg = "hbs should be invoked with a single argument: the template string"; | ||
if (parent.node.arguments.length !== 1) { | ||
throw parent.buildCodeFrameError(argumentErrorMsg); | ||
} | ||
return replaceNodeWithPrecompiledTemplate(this, template); | ||
} | ||
}, | ||
template = parent.node.arguments[0].value; | ||
if (typeof template !== "string") { | ||
throw parent.buildCodeFrameError(argumentErrorMsg); | ||
} | ||
TaggedTemplateExpression: function(node, parent, scope, file) { | ||
if (t.isIdentifier(node.tag, { name: file.importSpecifier })) { | ||
if (node.quasi.expressions.length) { | ||
throw file.errorWithNode(node, "placeholders inside a tagged template string are not supported"); | ||
} else if (parent.isTaggedTemplateExpression({ tag: path.node })) { | ||
if (parent.node.quasi.expressions.length) { | ||
throw parent.buildCodeFrameError("placeholders inside a tagged template string are not supported"); | ||
} | ||
template = parent.node.quasi.quasis.map(function(quasi) { | ||
return quasi.value.cooked; | ||
}).join(""); | ||
} else { | ||
return; | ||
} | ||
var template = node.quasi.quasis.map(function(quasi) { | ||
return quasi.value.cooked; | ||
}).join(""); | ||
let compiledTemplateString = `Ember.HTMLBars.template(${state.opts.precompile(template)})`; | ||
return replaceNodeWithPrecompiledTemplate(this, template); | ||
parent.replaceWithSourceString(compiledTemplateString); | ||
} | ||
} | ||
}); | ||
} | ||
// used by broccoli-babel-transpiler to use this packages | ||
// files and deps as part of the cache key (when deps or | ||
// implementation changes it will bust the cache for already | ||
// transpiled files) | ||
htmlbarsInlineCompilerPlugin.baseDir = function() { | ||
return __dirname; | ||
}, | ||
} | ||
}; | ||
}; | ||
// used by broccoli-babel-transpiler to bust the cache when | ||
// the template compiler being used changes | ||
htmlbarsInlineCompilerPlugin.cacheKey = function() { | ||
return options.cacheKey; | ||
}; | ||
return htmlbarsInlineCompilerPlugin; | ||
}; | ||
module.exports.baseDir = function() { return __dirname; }; |
{ | ||
"name": "babel-plugin-htmlbars-inline-precompile", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Babel plugin to replace tagged template strings with precompiled HTMLBars templates", | ||
@@ -8,9 +8,12 @@ "scripts": { | ||
}, | ||
"repository": "https://github.com/pangratz/babel-plugin-htmlbars-inline-precompile", | ||
"repository": "https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile", | ||
"author": "Clemens Müller <cmueller.418@gmail.com>", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">= 4" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^5.2.10", | ||
"mocha": "^2.2.4" | ||
"babel-core": "^6.7.4", | ||
"mocha": "^3.0.2" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# babel-plugin-htmlbars-inline-precompile [![Build Status](https://travis-ci.org/pangratz/babel-plugin-htmlbars-inline-precompile.svg?branch=master)](https://travis-ci.org/pangratz/babel-plugin-htmlbars-inline-precompile) | ||
# babel-plugin-htmlbars-inline-precompile [![Build Status](https://travis-ci.org/ember-cli/babel-plugin-htmlbars-inline-precompile.svg?branch=master)](https://travis-ci.org/ember-cli/babel-plugin-htmlbars-inline-precompile) | ||
@@ -69,11 +69,7 @@ Babel plugin to replace ES6 tagged template strings with the `HTMLBars.precompile`d version of it: | ||
var pluginConfiguredWithCompiler = HTMLBarsInlinePrecompile(HTMLBarsCompiler.precompile, { | ||
cacheKey: checksumOfTemplateCompilerContents | ||
}); | ||
require('babel').transform("code", { | ||
plugins: [ pluginConfiguredWithCompiler ] | ||
plugins: [ | ||
[HTMLBarsInlinePrecompile, {precompile: HTMLBarsCompiler.precompile}], | ||
], | ||
}); | ||
``` | ||
The provided `cacheKey` option is used to invalidate the broccoli-babel-transpiler cache. |
@@ -1,21 +0,20 @@ | ||
var assert = require('assert'); | ||
var path = require('path'); | ||
'use strict'; | ||
var babel = require('babel-core'); | ||
var HTMLBarsInlinePrecompile = require('../index'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
var transform; | ||
const babel = require('babel-core'); | ||
const HTMLBarsInlinePrecompile = require('../index'); | ||
function transform(code, precompile) { | ||
return babel.transform(code, { | ||
plugins: [ | ||
[HTMLBarsInlinePrecompile, {precompile: precompile}], | ||
], | ||
}).code.trim(); | ||
} | ||
describe("htmlbars-inline-precompile", function() { | ||
beforeEach(function() { | ||
transform = function(code, precompile) { | ||
return babel.transform(code, { | ||
blacklist: ['strict', 'es6.modules'], | ||
plugins: [HTMLBarsInlinePrecompile(precompile)] | ||
}).code; | ||
} | ||
}); | ||
it("strips import statement for 'htmlbars-inline-precompile' module", function() { | ||
var transformed = transform("import hbs from 'htmlbars-inline-precompile'; import Ember from 'ember';"); | ||
let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nimport Ember from 'ember';"); | ||
@@ -38,3 +37,3 @@ assert.equal(transformed, "import Ember from 'ember';", "strips import statement"); | ||
it("replaces tagged template expressions with precompiled version", function() { | ||
var transformed = transform("import hbs from 'htmlbars-inline-precompile'; var compiled = hbs`hello`;", function(template) { | ||
let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;", function(template) { | ||
return "precompiled(" + template + ")"; | ||
@@ -47,11 +46,7 @@ }); | ||
it("doesn't replace unrelated tagged template strings", function() { | ||
var expected = babel.transform("var compiled = anotherTag`hello`;", { | ||
blacklist: ['strict'] | ||
}).code; | ||
var transformed = transform('import hbs from "htmlbars-inline-precompile"; var compiled = anotherTag`hello`;', function(template) { | ||
let transformed = transform('import hbs from "htmlbars-inline-precompile";\nvar compiled = anotherTag`hello`;', function(template) { | ||
return "precompiled(" + template + ")"; | ||
}); | ||
assert.equal(transformed, expected, "other tagged template strings are not touched"); | ||
assert.equal(transformed, "var compiled = anotherTag`hello`;", "other tagged template strings are not touched"); | ||
}); | ||
@@ -61,3 +56,3 @@ | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile'; var compiled = hbs`string ${value}`"); | ||
transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`string ${value}`"); | ||
}, /placeholders inside a tagged template string are not supported/); | ||
@@ -67,12 +62,4 @@ }); | ||
describe('caching', function() { | ||
it('passes through second argument as `cacheKey`', function() { | ||
var plugin = HTMLBarsInlinePrecompile(function() {}, { cacheKey: 'asdfasdf' }); | ||
assert.equal(plugin.cacheKey(), 'asdfasdf'); | ||
}); | ||
it('include `baseDir` function for caching', function() { | ||
var plugin = HTMLBarsInlinePrecompile(function() {}, 'asdfasdf'); | ||
assert.equal(plugin.baseDir(), path.resolve(__dirname, '..')); | ||
assert.equal(HTMLBarsInlinePrecompile.baseDir(), path.resolve(__dirname, '..')); | ||
}); | ||
@@ -83,3 +70,3 @@ }); | ||
it("works with a plain string as parameter hbs('string')", function() { | ||
var transformed = transform("import hbs from 'htmlbars-inline-precompile'; var compiled = hbs('hello');", function(template) { | ||
let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs('hello');", function(template) { | ||
return "precompiled(" + template + ")"; | ||
@@ -93,3 +80,3 @@ }); | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile'; var compiled = hbs('first', 'second');"); | ||
transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs('first', 'second');"); | ||
}, /hbs should be invoked with a single argument: the template string/); | ||
@@ -100,3 +87,3 @@ }); | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile'; var compiled = hbs(123);"); | ||
transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs(123);"); | ||
}, /hbs should be invoked with a single argument: the template string/); | ||
@@ -107,3 +94,3 @@ }); | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile'; var compiled = hbs();"); | ||
transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs();"); | ||
}, /hbs should be invoked with a single argument: the template string/); | ||
@@ -110,0 +97,0 @@ }); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
25629
8
131
75