Skip to content
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

AMD module generator should be able to define dependencies #202

Merged
merged 6 commits into from
Feb 16, 2014

Conversation

highwaycoder
Copy link
Contributor

The AMD module generator is great, but at the moment it's hardcoded to produce an empty list of dependencies. This obviously makes it hard to integrate a Jison-generated AMD module into a larger project.

Should only take a small patch of line 954 of jison.js:
var out = '\n\ndefine('+JSON.stringify(opt.deps)+', function('+opt.deps.join(',')+'){'

If I'm correct in my assumptions, that patch would allow you to define dependencies for the generated module using the generator options by providing an array of strings. It's a bit limited since it uses the same array for the names of the modules and their filenames, but it would be enough for most projects.

@highwaycoder
Copy link
Contributor Author

I've updated my code so it picks up the moduleName option and uses it, as well as the "deps" option that I added. (must be a flat array of string values)
It can probably stand to be cleaned up a bit but for now, it at least does what I need for my project.

…nd although technically allowed, defining a module name explicitly is not recommended by requirejs.
@lddubeau
Copy link

I landed here looking for a solution to adding dependencies to a parser generated with jison as an AMD module designed to be loaded in a browser by RequireJS. Now, unless I misread something, the proposed patch won't handle dependencies like:

  • "./xmlcharacters"
  • "../blah"
  • "foo-foo"
  • "foo.foo"

The first dependency in this list is one I actually have in my parser. I've added the other cases for the sake of completeness. I don't right now need to load in my jison-generated parser modules with dashes in them or periods but some modules do have these characters in their names.

For the record, I'm currently fixing my problem by replacing the initial:

define([], function () {

with:

define(function (require) {

A call to define without dependencies but with a list of argument on the factory tells RequireJS that the module's factory is using a CommonJS idiom. I do not know whether this is common to AMD loaders in general or specific to RequireJS.

@highwaycoder
Copy link
Contributor Author

I was not aware of the define(function(require) { sugar. That's a perfect solution to the problem. I'll code up that alternative and commit it to the branch this pull request is coming from, so it should update the PR in a few minutes once I've pushed.

zaach added a commit that referenced this pull request Feb 16, 2014
AMD module generator should be able to define dependencies
@zaach zaach merged commit 2b209f7 into zaach:master Feb 16, 2014
@zaach
Copy link
Owner

zaach commented Feb 16, 2014

Thanks @cjbrowne and @lddubeau! 🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants