Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add shim to compile optimiser config (with test). #135

Open
wants to merge 3 commits into
from

Conversation

Projects
None yet
2 participants
Contributor

maxpeterson commented Mar 18, 2013

This is the same issue as caolan/jam/#113 with the addition of a test.

Issue: the compile command currently doesn't include shim config the project is using, so optimized output breaks
Solution: include shim config from jam/require.config.js in the optimize call

The crux of this issue is that if you use shim to define that a package exports a variable (e.g. Backbone or in mytest PackageOne) then this shim is needed by require's optimiser to defines the package such that it returns the exported variable.

My test includes a module with main.js:

var PackageOne = {
    name: 'Package One'
}

and package.js:

{
    "name": "package-one",
    "version": "0.0.1",
    "description": "Test package one shim",
    "jam": {
        "shim": {
            "exports": "PackageOne"
        }
    }
}

If the shim is not passed to the optimiser then it will define package-one/main as:

define("package-one/main",function(){})

If shim is passed to the optimiser then it will define package-one/main as:

define("package-one/main",function(e){return function(){var t,n;return t||e.PackageOne}}(this));

In both cases package-one is defined as

define("package-one",["package-one/main"],function(e){return e})

The result is that without the shim any package that requires package-one as a dependency will not get passed PackageOne:

define('mypackage', ['package-one'], function (one) { 
   //  one will only be defined as PackageOne if shim is included
}

In addition I fixed some typos in the other integration tests,

Contributor

maxpeterson commented Apr 12, 2013

@caolan have you had a chance to look at this? It is blocking us from using jam compile to build our projects. We are using requires r.js -o app.build.js with app.build.js packages and shim copied from jam/require.config.js.

If there is anything I can do let me know.

I'm running into the same problem right now actually with the added issue of I need to supply extra shim configuration. It would appear that the require.config.js for jam gets regenerated and is not a safe place to do this so I have a separate file that specifies our additional shim info.

Again, in a non-optimized site it works fine, but once you run everything through jam compile it breaks.

Contributor

maxpeterson commented Apr 12, 2013

@copenhas you can usually put the shim in the package.js. Have you tested with this branch. The only change you actually need for the fix is in lib/commands/compile.js, the rest is tests.

@maxpeterson I haven't tested with this branch. I've been fighting this issue for a little over a day and once I knew what was happening I started searching. Great to see that the exact issue is trying to be resolved, but at the moment I need to get this working and will probably resort to globals temporarily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment