-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How handlebars.runtime.amd.js should to be used? #796
Comments
👍 ran into this issue last week - I just aliased |
I have the same problem. It is actually a build bug, just look inside the |
Another way to resolve this issue I think is to use Handlebars source as a bower dependency:
|
Found the solution, it is possible to use non amd version that also works on build time and with shim: {
handlebars: {
exports: 'Handlebars',
init: function() {
this.Handlebars = Handlebars;
return this.Handlebars;
}
}
} |
Of course you can do so, but as I've said, AMD build should work without shims and globals. And the issue is only with AMD runtime build, I don't see any issues with other builds. |
I believe the grunt-contrib-requirejs task needs to be configured correctly. As you guys said it's including relative path names that don't actually resolve to anything. Just as a heads up, though, the compilation options are complex. If anyone wants to take a stab at it there is an "example" that has detailed explanations of every option here: https://github.com/jrburke/r.js/blob/master/build/example.build.js It's a powerful tool, just not very easy to use. |
What is the root issue here? That |
If I recall correctly it's unclear how to use requirejs to get the exports from the file. If you simple require() the file it returns nothing and doesn't install anything in the global namespace. An example somewhere would be nice but I think the trick might be a different style of module compilation. For example, just use the normal file but instead of installing into global namespace wrap the file in define(function(require){�c}); and make sure you return the object that would have otherwise been installed in the global namespace. I do realize that this might not work with other module systems that aren't requirejs, but on of the reasons I like require is because authors have to do next to nothing to conform.
|
We do have the AMD library loaded via require in our AMD test suite: https://github.com/wycats/handlebars.js/blob/master/spec/amd.html#L31 (That this assigns to a global is merely to make things cooperate with the numerous other test environments we need to handle) I must admit that I'm not a require user outside of this simple hack, but my understanding is that this is fairly straightforward require usage. Am I off base here? |
So at least my specific issue is only with Handlebars.runtime.amd.js . I have not tried to use Handlebars.amd.js with an AMD loader because i only really use pre-compiled templates on the client side. What I can tell you is that doing As an aside, in my opinion, if you are mutating the global object in an AMD file then it defeats the purpose. Code that does that can already be used with RequireJS without any modification by adding shim: { handlebars: { exports: 'Handlebars' } } to your requirejs configuration. It tells require that the file sets a variable of the specified name and to use that in place of a return value from the wrapping define(function(require){}) function. One day I will get some time to try my hand at fixing some of this here and submitting a pull request. I like handlebars =) |
Whoops! It turns out that it's because I was including the wrong top-level file. I was including dist/handlebars.runtime.amd instead of /dist/amd/handlebars.runtime.amd :( Writing JavaScript has addled my brain, sorry XD OK SO. If anyone is trying to include the AMD version of Runtime, I had success by doing the following in my require config: paths: {
handlebars: 'path/to/handlebars/dist/amd'
} THEN, include it somewhere by going var Handlebars = require('handlebars/handlebars.runtime')['default']; At first I had mistaken the Handlebars.runtime.amd.js file for the only one necessary to include. Then I realized that is uses exports.default instead of returning directly from define(); |
The wonderful thing about standards is that there are so, so many to choose from :D |
So there are a few options here (not all of which I am certain that the AMD libraries support):
@ixtli I agree, try supporting all of the standards, when you don't use them in your day to day :) |
I am using approach #1 (using it as a pure unbuilt AMD module) as it makes the most sense to me. The issue I am having now is that everything is nested within the 'default' property of an object.
If there is a reason for this, then the change needs to be applied to 'grunt-contrib-handlebars' However, I am unsure of why there would be a benefit to wrapping everything inside of an object... EDIT: POINT TO MORE RELEVANT FIX |
I believe this is because the ES6 module syntax lets you Also I how is this breaking the grunt handlebars module?
|
Grunt Options:
Output:
Notice that it calls |
Huh yeah I checked the docs and you're right. They need to let you change the name. A reasonable way to solve this before they figure out how to let you specify what the template function is called would be to make a new js file that includes the real runtime and exports the default. Then you can pass that shim's module name as a string to the amd param.
|
@ixtli the reason of the and +1 for the reasonable way. It's how I do on my differents projects:
|
I've implemented two changes that should hopefully help with this issue:
|
Released in v2.0.0-beta.1 |
I suppose, as an AMD-compatible build,
handlebars.runtime.amd.js
, as well ashandlebars.amd.js
, should work without any shim configs, but I'm facing an issue trying to use the first (runtime) AMD build. Here is an explanation.Both these AMD builds are bundles with several normalized AMD modules inside (modules with their IDs inlined into
define
as a first argument).To be able to use such bundle through
require.config
without help of crutches like shims&globals we should require them by an ID of one of the modules (the main one) from the bundle. For a non-runtime AMD build such ID ishandlebars
and everything works fine.But when you look into a runtime AMD build, you'll see that ID of an entry module is
handlebars.runtime
instead ofhandlebars
. Well, then you'll try to require the runtime bundle using IDhandlebars.runtime
, but this way won't work: r.js will fail to build your project being unable to findbase.js
module (the first dependency ofhandlebars.runtime
module -./handlebars/base
).So, as I understand, all another modules inside the runtime AMD bundle still want the bundle be required only using ID
handlebars
.The text was updated successfully, but these errors were encountered: