ember-templates-loader
Advanced tools
Comparing version 0.1.1 to 0.2.1
131
index.js
@@ -1,3 +0,2 @@ | ||
var path = require('path'); | ||
//var loaderUtils = require('loader-utils'); | ||
var Handlebars = require('handlebars'); | ||
@@ -7,6 +6,128 @@ module.exports = function(source) { | ||
var done = this.async(); | ||
// TODO: Add precompile option | ||
var output = 'module.exports = Ember.Handlebars.compile(' + JSON.stringify(source) + ');'; | ||
done(null, output); | ||
var ast = Handlebars.parse(source); | ||
var options = { | ||
knownHelpers: { | ||
action: true, | ||
unbound: true, | ||
'bind-attr': true, | ||
template: true, | ||
view: true, | ||
_triageMustache: true | ||
}, | ||
data: true, | ||
stringParams: true | ||
}; | ||
var environment = new Compiler().compile(ast, options); | ||
var templateSpec = new JavaScriptCompiler().compile(environment, options, undefined, true); | ||
done(null, 'module.exports = Ember.Handlebars.template(' + templateSpec + '); module.exports.isMethod = false;'); | ||
} | ||
module.exports.seperable = true; | ||
// Below here is mostly copied from ember's ember-handlebars-compiler | ||
// Eliminate dependency on any Ember to simplify precompilation workflow | ||
var objectCreate = Object.create || function(parent) { | ||
function F() {} | ||
F.prototype = parent; | ||
return new F(); | ||
}; | ||
/** | ||
Override the the opcode compiler and JavaScript compiler for Handlebars. | ||
@class Compiler | ||
@namespace Ember.Handlebars | ||
@private | ||
@constructor | ||
*/ | ||
var Compiler = function() {}; | ||
Compiler.prototype = objectCreate(Handlebars.Compiler.prototype); | ||
Compiler.prototype.compiler = Compiler; | ||
/** | ||
@class JavaScriptCompiler | ||
@namespace Ember.Handlebars | ||
@private | ||
@constructor | ||
*/ | ||
var JavaScriptCompiler = function() {}; | ||
JavaScriptCompiler.prototype = objectCreate(Handlebars.JavaScriptCompiler.prototype); | ||
JavaScriptCompiler.prototype.compiler = JavaScriptCompiler; | ||
JavaScriptCompiler.prototype.namespace = 'Ember.Handlebars'; | ||
JavaScriptCompiler.prototype.initializeBuffer = function() { | ||
return "''"; | ||
}; | ||
JavaScriptCompiler.prototype.appendToBuffer = function(string) { | ||
return "data.buffer.push("+string+");"; | ||
}; | ||
// Hacks ahead: | ||
// Handlebars presently has a bug where the `blockHelperMissing` hook | ||
// doesn't get passed the name of the missing helper name, but rather | ||
// gets passed the value of that missing helper evaluated on the current | ||
// context, which is most likely `undefined` and totally useless. | ||
// | ||
// So we alter the compiled template function to pass the name of the helper | ||
// instead, as expected. | ||
// | ||
// This can go away once the following is closed: | ||
// https://github.com/wycats/handlebars.js/issues/634 | ||
var DOT_LOOKUP_REGEX = /helpers\.(.*?)\)/, | ||
BRACKET_STRING_LOOKUP_REGEX = /helpers\['(.*?)'/, | ||
INVOCATION_SPLITTING_REGEX = /(.*blockHelperMissing\.call\(.*)(stack[0-9]+)(,.*)/; | ||
JavaScriptCompiler.stringifyLastBlockHelperMissingInvocation = function(source) { | ||
var helperInvocation = source[source.length - 1], | ||
helperName = (DOT_LOOKUP_REGEX.exec(helperInvocation) || BRACKET_STRING_LOOKUP_REGEX.exec(helperInvocation))[1], | ||
matches = INVOCATION_SPLITTING_REGEX.exec(helperInvocation); | ||
source[source.length - 1] = matches[1] + "'" + helperName + "'" + matches[3]; | ||
} | ||
var stringifyBlockHelperMissing = JavaScriptCompiler.stringifyLastBlockHelperMissingInvocation; | ||
var originalBlockValue = JavaScriptCompiler.prototype.blockValue; | ||
JavaScriptCompiler.prototype.blockValue = function() { | ||
originalBlockValue.apply(this, arguments); | ||
stringifyBlockHelperMissing(this.source); | ||
}; | ||
var originalAmbiguousBlockValue = JavaScriptCompiler.prototype.ambiguousBlockValue; | ||
JavaScriptCompiler.prototype.ambiguousBlockValue = function() { | ||
originalAmbiguousBlockValue.apply(this, arguments); | ||
stringifyBlockHelperMissing(this.source); | ||
}; | ||
var prefix = "ember" + (+new Date()), incr = 1; | ||
/** | ||
Rewrite simple mustaches from `{{foo}}` to `{{bind "foo"}}`. This means that | ||
all simple mustaches in Ember's Handlebars will also set up an observer to | ||
keep the DOM up to date when the underlying property changes. | ||
@private | ||
@method mustache | ||
@for Ember.Compiler | ||
@param mustache | ||
*/ | ||
Compiler.prototype.mustache = function(mustache) { | ||
if (mustache.isHelper && mustache.id.string === 'control') { | ||
mustache.hash = mustache.hash || new Handlebars.AST.HashNode([]); | ||
mustache.hash.pairs.push(["controlID", new Handlebars.AST.StringNode(prefix + incr++)]); | ||
} else if (mustache.params.length || mustache.hash) { | ||
// no changes required | ||
} else { | ||
var id = new Handlebars.AST.IdNode([{ part: '_triageMustache' }]); | ||
// Update the mustache node to include a hash value indicating whether the original node | ||
// was escaped. This will allow us to properly escape values when the underlying value | ||
// changes and we need to re-render the value. | ||
if (!mustache.escaped) { | ||
mustache.hash = mustache.hash || new Handlebars.AST.HashNode([]); | ||
mustache.hash.pairs.push(["unescaped", new Handlebars.AST.StringNode("true")]); | ||
} | ||
mustache = new Handlebars.AST.MustacheNode([id].concat([mustache.id]), mustache.hash, !mustache.escaped); | ||
} | ||
return Handlebars.Compiler.prototype.mustache.call(this, mustache); | ||
}; |
{ | ||
"name": "ember-templates-loader", | ||
"version": "0.1.1", | ||
"description": "Webpack loader for ember handlebars templates", | ||
"version": "0.2.1", | ||
"description": "Load templates into Ember with webpack.", | ||
"main": "index.js", | ||
@@ -22,7 +22,7 @@ "scripts": { | ||
"dependencies": { | ||
"loader-utils": "~0.2.1" | ||
"handlebars": "~1.3.0" | ||
}, | ||
"devDependencies": { | ||
"webpack-dev-server": "~0.11.0", | ||
"should": "~2.0.2", | ||
"should": "~3.1.2", | ||
"mocha": "~1.14.0", | ||
@@ -32,3 +32,9 @@ "mocha-loader": "~0.6.3", | ||
"raw-loader": "~0.5.0" | ||
} | ||
}, | ||
"keywords": [ | ||
"ember", | ||
"templates", | ||
"handlebars", | ||
"webpack" | ||
] | ||
} |
# ember-templates-loader | ||
A WIP ember templates loader for webpack. Please don't use yet. | ||
> Load templates into Ember with webpack. | ||
## Description | ||
This will precompile your Handlebar templates when you build with webpack with overrides specific for Ember. | ||
## Usage | ||
``` js | ||
var MyView = Ember.View.extend({ | ||
template: require('ember-templates!./templates/my-view.hbs') | ||
}); | ||
``` | ||
Or better within your `webpack.config.js`: | ||
``` js | ||
module.exports = { | ||
module: { | ||
loaders: [ | ||
{ test: /\.hbs$/, loader: 'ember-templates' } | ||
] | ||
} | ||
}; | ||
``` | ||
Now all required handlebars templates will be compiled for Ember first. | ||
## Install | ||
`npm install ember-templates-loader --save-dev` | ||
## Contributing | ||
In lieu of a formal styleguide, take care to maintain the existing coding style. | ||
## Release History | ||
* 0.2.0 - Precompile templates for Ember and publish | ||
## License | ||
Copyright (c) 2014 Kyle Robinson Young | ||
Licensed under the MIT license. |
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
6605
115
44
0
+ Addedhandlebars@~1.3.0
+ Addedamdefine@1.0.1(transitive)
+ Addedasync@0.2.10(transitive)
+ Addedhandlebars@1.3.0(transitive)
+ Addedoptimist@0.3.7(transitive)
+ Addedsource-map@0.1.43(transitive)
+ Addeduglify-js@2.3.6(transitive)
+ Addedwordwrap@0.0.3(transitive)
- Removedloader-utils@~0.2.1
- Removedbig.js@3.2.0(transitive)
- Removedemojis-list@2.1.0(transitive)
- Removedjson5@0.5.1(transitive)
- Removedloader-utils@0.2.17(transitive)
- Removedobject-assign@4.1.1(transitive)