assetgraph / assetgraph-builder

AssetGraph-based build system for web apps and web pages.
BSD 3-Clause "New" or "Revised" License
489 stars 42 forks source link

calling buildProduction on front end using requirejs fails #54

Closed mreinstein closed 11 years ago

mreinstein commented 11 years ago

I've tried running assetgraph-builder against my requirejs backed code. I've run into some build problems:

Here is my the line from my index.html which includes require.js:

<script src="/js/vendor/requirejs/require.js" data-main="/js/app"></script>

According to requirejs documentation, the basePath should now be set to /js. Instead it seems like it's being set to the index.html directory (I'm assuming it's just using what I set for --outroot.) Here's some parts of my requirejs config (from /js/app.js):

# sets the require.js configuration for the application
require.config {
    paths:
        backbone         : 'vendor/backbone-amd/backbone' 
        deepmodel        : 'vendor/deep-model'
        ...
}

My project directory structure:

|- dist/
|- public/
   |- index.html
   `- js/
      |- app.js
      `- vendor/
         |- backbone-amd/
         |  `- backbone.js
         |- deep-model.js
         `- requirejs/
            `- require.js

My build command:

cd projectdir
buildProduction --outroot dist/ --root public/ public/index.html

Here are some of the errors I'm seeing:

file:///root/hellosprout/public/vendor/backbone-amd/backbone.js: ENOENT, open '/root/hellosprout/public/vendor/backbone-amd/backbone.js'
Including assets:
    file:///root/hellosprout/public/js/app.js

file:///root/hellosprout/public/vendor/deep-model.js: ENOENT, open '/root/hellosprout/public/vendor/deep-model.js'
Including assets:
    file:///root/hellosprout/public/js/models/cart.js
papandreou commented 11 years ago

Thanks for the detailed report. I know there are some race conditions where the require.js configuration won't yet be extracted at the time when the require/define calls are resolved. Could you try defining the require.js config in an inline script in your HTML instead using this syntax?

<script>
    var require = {
        paths:
            backbone         : 'vendor/backbone-amd/backbone' 
            deepmodel        : 'vendor/deep-model'
            ...
    };
</script>
<script src="/js/vendor/requirejs/require.js" data-main="/js/app"></script>
mreinstein commented 11 years ago

That's a lot of stuff to jam into the index file. It's a sizable app. Can the race conditions be fixed? Based on the numerous examples Ive seen, this is a very common way apps are set up. If requirejs compatibility is a goal, this is important IMO.

papandreou commented 11 years ago

I agree, and it's certainly on my list. I asked you to try the above so I'd know whether this is a separate issue.

mreinstein commented 11 years ago

Fair enough. Ill wire it up and let you know what happens

papandreou commented 11 years ago

Hmm, that turned out not to be the problem. I've created a stripped-down test case based on your description, and it does indeed look like the module names aren't resolved relative to your data-main module. I'm working on a fix. In the mean time you can avoid the bug by using an explicit baseUrl setting:

require.config {
    baseUrl: '/js'
    paths:
        backbone         : 'vendor/backbone-amd/backbone' 
        deepmodel        : 'vendor/deep-model'
        ...
}
mreinstein commented 11 years ago

@papandreou sorry I didn't get to the test before you did. the baseUrl does indeed bypass the problem for now. Thanks!

papandreou commented 11 years ago

No prob, had all the info I needed in your report. Fixed in 1.7.5.

mreinstein commented 11 years ago

nice!