babel-plugin-htmlbars-inline-precompile
Advanced tools
Comparing version
@@ -10,3 +10,7 @@ 'use strict'; | ||
let node = path.node; | ||
if (t.isLiteral(node.source, { value: "htmlbars-inline-precompile" })) { | ||
let modulePaths = state.opts.modulePaths || ["htmlbars-inline-precompile"]; | ||
let matchingModulePath = modulePaths.find(value => t.isLiteral(node.source, { value })); | ||
if (matchingModulePath) { | ||
let first = node.specifiers && node.specifiers[0]; | ||
@@ -16,3 +20,3 @@ if (!t.isImportDefaultSpecifier(first)) { | ||
let usedImportStatement = input.slice(node.start, node.end); | ||
let msg = `Only \`import hbs from 'htmlbars-inline-precompile'\` is supported. You used: \`${usedImportStatement}\``; | ||
let msg = `Only \`import hbs from '${matchingModulePath}'\` is supported. You used: \`${usedImportStatement}\``; | ||
throw path.buildCodeFrameError(msg); | ||
@@ -19,0 +23,0 @@ } |
{ | ||
"name": "babel-plugin-htmlbars-inline-precompile", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Babel plugin to replace tagged template strings with precompiled HTMLBars templates", | ||
"scripts": { | ||
"test": "mocha tests" | ||
"test": "jest" | ||
}, | ||
@@ -18,4 +18,9 @@ "repository": "https://github.com/ember-cli/babel-plugin-htmlbars-inline-precompile", | ||
"babel-plugin-transform-es2015-template-literals": "^6.22.0", | ||
"mocha": "^3.0.2" | ||
"jest": "^21.0.0" | ||
}, | ||
"jest": { | ||
"testMatch": [ | ||
"<rootDir>/tests/**/*.js" | ||
] | ||
} | ||
} |
'use strict'; | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
@@ -12,3 +11,3 @@ | ||
describe("htmlbars-inline-precompile", function() { | ||
let precompile, plugins; | ||
let plugins; | ||
@@ -22,8 +21,6 @@ function transform(code) { | ||
beforeEach(function() { | ||
precompile = (template) => template.toUpperCase(); | ||
plugins = [ | ||
[HTMLBarsInlinePrecompile, { | ||
precompile: function() { | ||
return precompile.apply(this, arguments); | ||
precompile(template) { | ||
return `precompiled(${template})`; | ||
} | ||
@@ -37,27 +34,30 @@ }], | ||
assert.equal(transformed, "import Ember from 'ember';", "strips import statement"); | ||
expect(transformed).toEqual("import Ember from 'ember';", "strips import statement"); | ||
}); | ||
it("throws error when import statement is not using default specifier", function() { | ||
try { | ||
transform("import { hbs } from 'htmlbars-inline-precompile'"); | ||
expect(() => transform("import { hbs } from 'htmlbars-inline-precompile'")) | ||
.toThrow(/Only `import hbs from 'htmlbars-inline-precompile'` is supported/, "needed import syntax is present"); | ||
assert.fail("error should have been thrown"); | ||
} catch (e) { | ||
assert.ok(e.message.match(/Only `import hbs from 'htmlbars-inline-precompile'` is supported/), "needed import syntax is present"); | ||
assert.ok(e.message.match(/You used: `import { hbs } from 'htmlbars-inline-precompile'`/), "used import syntax is present"); | ||
} | ||
expect(() => transform("import { hbs } from 'htmlbars-inline-precompile'")) | ||
.toThrow(/You used: `import { hbs } from 'htmlbars-inline-precompile'`/, "used import syntax is present"); | ||
}); | ||
it("replaces tagged template expressions with precompiled version", function() { | ||
precompile = template => `precompiled(${template})`; | ||
let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"); | ||
assert.equal(transformed, "var compiled = Ember.HTMLBars.template(precompiled(hello));", "tagged template is replaced"); | ||
expect(transformed).toEqual("var compiled = Ember.HTMLBars.template(precompiled(hello));", "tagged template is replaced"); | ||
}); | ||
it("replaces tagged template expressions with precompiled version for custom import paths", function() { | ||
plugins[0][1].modulePaths = ['ember-cli-htmlbars-inline-precompile']; | ||
let transformed = transform("import hbs from 'ember-cli-htmlbars-inline-precompile';\nvar compiled = hbs`hello`;"); | ||
expect(transformed).toEqual("var compiled = Ember.HTMLBars.template(precompiled(hello));"); | ||
}); | ||
it("does not cause an error when no import is found", function() { | ||
transform('something("whatever")'); | ||
transform('something`whatever`'); | ||
expect(() => transform('something("whatever")')).not.toThrow(); | ||
expect(() => transform('something`whatever`')).not.toThrow(); | ||
}); | ||
@@ -73,5 +73,5 @@ | ||
let expected = `let a = Ember.HTMLBars.template(HELLO);\nlet b = Ember.HTMLBars.template(HELLO);`; | ||
let expected = `let a = Ember.HTMLBars.template(precompiled(hello));\nlet b = Ember.HTMLBars.template(precompiled(hello));`; | ||
assert.equal(transformed, expected, "tagged template is replaced"); | ||
expect(transformed).toEqual(expected, "tagged template is replaced"); | ||
}); | ||
@@ -83,3 +83,3 @@ | ||
assert.equal(transformed, `define([], function () {\n 'use strict';\n\n var compiled = Ember.HTMLBars.template(HELLO);\n});`, "tagged template is replaced"); | ||
expect(transformed).toEqual(`define([], function () {\n 'use strict';\n\n var compiled = Ember.HTMLBars.template(precompiled(hello));\n});`, "tagged template is replaced"); | ||
}); | ||
@@ -91,3 +91,3 @@ | ||
assert.equal(transformed, `define([], function () {\n 'use strict';\n\n var compiled = Ember.HTMLBars.template(HELLO);\n});`, "tagged template is replaced"); | ||
expect(transformed).toEqual(`define([], function () {\n 'use strict';\n\n var compiled = Ember.HTMLBars.template(precompiled(hello));\n});`, "tagged template is replaced"); | ||
}); | ||
@@ -99,17 +99,14 @@ | ||
assert.equal(transformed, "var compiled = Ember.HTMLBars.template(HELLO);", "tagged template is replaced"); | ||
expect(transformed).toEqual("var compiled = Ember.HTMLBars.template(precompiled(hello));", "tagged template is replaced"); | ||
}); | ||
it("doesn't replace unrelated tagged template strings", function() { | ||
precompile = template => `precompiled(${template})`; | ||
let transformed = transform('import hbs from "htmlbars-inline-precompile";\nvar compiled = anotherTag`hello`;'); | ||
assert.equal(transformed, "var compiled = anotherTag`hello`;", "other tagged template strings are not touched"); | ||
expect(transformed).toEqual("var compiled = anotherTag`hello`;", "other tagged template strings are not touched"); | ||
}); | ||
it("warns when the tagged template string contains placeholders", function() { | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`string ${value}`"); | ||
}, /placeholders inside a tagged template string are not supported/); | ||
expect(() => transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs`string ${value}`")) | ||
.toThrow(/placeholders inside a tagged template string are not supported/); | ||
}); | ||
@@ -119,3 +116,3 @@ | ||
it('include `baseDir` function for caching', function() { | ||
assert.equal(HTMLBarsInlinePrecompile.baseDir(), path.resolve(__dirname, '..')); | ||
expect(HTMLBarsInlinePrecompile.baseDir()).toEqual(path.resolve(__dirname, '..')); | ||
}); | ||
@@ -126,27 +123,22 @@ }); | ||
it("works with a plain string as parameter hbs('string')", function() { | ||
precompile = template => `precompiled(${template})`; | ||
let transformed = transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs('hello');"); | ||
assert.equal(transformed, "var compiled = Ember.HTMLBars.template(precompiled(hello));", "tagged template is replaced"); | ||
expect(transformed).toEqual("var compiled = Ember.HTMLBars.template(precompiled(hello));", "tagged template is replaced"); | ||
}); | ||
it("warns when more than one argument is passed", function() { | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs('first', 'second');"); | ||
}, /hbs should be invoked with a single argument: the template string/); | ||
expect(() => transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs('first', 'second');")) | ||
.toThrow(/hbs should be invoked with a single argument: the template string/); | ||
}); | ||
it("warns when argument is not a string", function() { | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs(123);"); | ||
}, /hbs should be invoked with a single argument: the template string/); | ||
expect(() => transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs(123);")) | ||
.toThrow(/hbs should be invoked with a single argument: the template string/); | ||
}); | ||
it("warns when no argument is passed", function() { | ||
assert.throws(function() { | ||
transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs();"); | ||
}, /hbs should be invoked with a single argument: the template string/); | ||
expect(() => transform("import hbs from 'htmlbars-inline-precompile';\nvar compiled = hbs();")) | ||
.toThrow(/hbs should be invoked with a single argument: the template string/); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
127913
275.75%158
-3.07%