Giter Site home page Giter Site logo

Comments (3)

jrburke avatar jrburke commented on July 17, 2024

How does it fail for almond? Is define() not a function when that code runs? If so, how is the file created? Hopefully almond.js is at the beginning of the file, with this snippet some time later. At first look, it seems OK, but if you have many modules following this pattern and they have dependencies, I expect the r.js optimizer will have trouble finding all the define() calls, as it looks for top level define calls and skips ones that are calls on an object as shown above.

from almond.

Rush avatar Rush commented on July 17, 2024

The generated code looks like this:

define("themes/colored", function(){});

({
    define: typeof define === "function" ? define : function(A, F) {
        module.exports = F.apply(null, A.map(require));
        DOMParser = require('xmldom').DOMParser;
        XMLSerializer = require('xmldom').XMLSerializer;
}
 }).
define([], function() {

});

As you can see the dependency will be found but undefined ...

I have found different pattern to work though, but it would be nice if the first one worked:

var DOMParser;
var XMLSerializer;
var define;
if (typeof define !== "function") {
    define = function(A, F) {
        module.exports = F.apply(null, A.map(require));
        DOMParser = require('xmldom').DOMParser;
        XMLSerializer = require('xmldom').XMLSerializer;
    }
}

from almond.

jrburke avatar jrburke commented on July 17, 2024

OK, so if that second one works, it is due to the define() scanning done by the r.js optimizer to create the minified file. It is not finding the define() call to insert the module ID.

define method calls on an object are not matched by the r.js optimizer because there are libraries, most notably uglifyjs, that created a define property on an object, for their own internal API, and it is not the AMD define().

If the r.js optimizer matched on those cases, it would break code like that. So there are no plans to match on that property access pattern as it will create more bugs than it helps as far as define adapter shims. I agree that it limits the structure of those shims, but it is the result of an API-based module pattern, and mixing that code with other code that could have its own other meaning for a similar looking API.

from almond.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.