canjs / can-zone

A context for tracking asynchronous activity in JavaScript applications.
https://v4.canjs.com/doc/can-zone.html
MIT License
92 stars 4 forks source link

can-zone warns in a webpack build #177

Closed justinbmeyer closed 6 years ago

justinbmeyer commented 6 years ago

If can-zone is imported by webpack, it provides the following warning:

WARNING in ./node_modules/can-zone/lib/env.js 3:55-62
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
 @ ./node_modules/can-zone/lib/zone.js
 @ ./node_modules/can/es/can-zone.js
 @ ./node_modules/can/can.js
 @ ./index.js

Which is due to this code:

var isNode = typeof process !== "undefined" && {}.toString.call(process) === "[object process]";
var nodeRequire = typeof System !== "undefined" && System._nodeRequire ?
    System._nodeRequire : typeof require === "function" ? require : function(){};
var isNW = isNode && (function(){
  try {
    return nodeRequire("nw.gui") !== "undefined";
  } catch(e) {
    return false;
  }
})();

I think we might need to fool webpack better when accessing require. The following works:

var isNode = typeof process !== "undefined" && {}.toString.call(process) === "[object process]";
var GLOBAL = (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) ? self :

        // Node.js
        typeof process === 'object' &&
        {}.toString.call(process) === '[object process]' ? global :

        // Browser window
        window;
var requireString = "require";
var requireAlias = GLOBAL[requireString];

Couldn't hurt to use can-globals for getting the GLOBAL (I copied it's code above anyway).

matthewp commented 6 years ago

How do you know it's due to that code? The error you posted points to lib/zone.js.

justinbmeyer commented 6 years ago

@matthewp I forgot to paste the whole warning. Updated to include the 1st line.

matthewp commented 6 years ago

Trying to figure out if there is a better way to fix. At least with steal require is not a global, it's part of an IIFE argument.