timdown / rangy

A cross-browser JavaScript range and selection library.
MIT License
2.23k stars 367 forks source link

AMD define() function getting called during webpack build #456

Closed nicholascloud closed 5 years ago

nicholascloud commented 5 years ago

I'm trying to use the rangy npm package in a project, but when webpack attempts to build our script(s), the AMD define() function is being called:

(function(factory, root) {
    if (typeof define == "function" && define.amd) {
        console.info('why is this called?');
        console.info(define);
        console.info(define.amd);
        // AMD. Register as an anonymous module.
        define(factory);
    } else if (typeof module != "undefined" && typeof exports == "object") {
        // Node/CommonJS style
        module.exports = factory();
    } else {
        // No AMD or CommonJS support so we place Rangy in (probably) the global variable
        root.rangy = factory();
    }
})...

This is the output in the Firefox console:

screen shot 2018-11-08 at 11 53 05 am

However, webpack executes in a CommonJS/ES6 environment, so if I comment out the "AMD" portion of the if-statement above, the CommonJS code is executed.

(function(factory, root) {
//    if (typeof define == "function" && define.amd) {
//        console.info('why is this called?');
//        console.info(define);
//        console.info(define.amd);
//        // AMD. Register as an anonymous module.
//        define(factory);
//    } else if (typeof module != "undefined" && typeof exports == "object") {
    if (typeof module != "undefined" && typeof exports == "object") {
        console.info('now we are node!');
        // Node/CommonJS style
        module.exports = factory();
    } else {
        // No AMD or CommonJS support so we place Rangy in (probably) the global variable
        root.rangy = factory();
    }
})...
screen shot 2018-11-08 at 11 54 06 am

My guess is that this is a webpack incompatibility problem, but perhaps changing the order of evaluation in the if-block across all rangy modules would be a simple fix for this.

nicholascloud commented 5 years ago

Closed because I don't believe this is a webpack issue, but probably something in my configuration.