railsware / js-routes

Brings Rails named routes to javascript

Home Page:http://railsware.github.io/js-routes/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Webpack usage: `this` refers to `undefined`, causing the script to explode

bbrassart opened this issue · comments

Hi there!
For information, I'm using Rails 5.1. I generated a Javascript Routes file via Rake task using the JsRoutes.!generate() method. I even manage to use this file in my test environment (I'm using Jest). I then don't have to mock all the different routes. So far, so good.

Now, whenever I try to import that file in a pack using require('./routes.js'), the file is correctly picked up but the script explodes. Basically, after investigating a bit, I found out that

(function() {
  var DeprecatedBehavior, NodeTypes, ParameterMissing, ReservedOptions, SpecialOptionsKey, Utils, createGlobalJsRoutesObject, defaults, root,
        hasProp = {}.hasOwnProperty,
        slice = [].slice;

      root = typeof exports !== "undefined" && exports !== null ? exports : this;
      // console logging root here gives undefined
     ...............................
}).call(this);

the error is located in the first lines of the script. Instead of being set to {}, root is undefined. It then makes the namespace function execution explodes. The funny thing is that, if I ran this script through a pack

(function() {
  var root;

  root = typeof exports !== "undefined" && exports !== null ? exports : this;
  return root;
}).call(this);

I do get root as an empty {}. Which leads me to think that the error is somewhere line 2 of the script var DeprecatedBehavior, NodeTypes, ParameterMissing, ReservedOptions, SpecialOptionsKey, Utils, createGlobalJsRoutesObject, defaults, root, hasProp = {}.hasOwnProperty; .

Anyone has an idea what the problem can be ?

Can you start removing variables definitions from that long line one by one and leave only the minimum number of variables that reproduce the issue?

Right, I forgot to mention this. The variable declaration that seems to be causing this is the last one slice = [].slice;. When I delete this declaration and run

(function() {
  var DeprecatedBehavior, NodeTypes, ParameterMissing, ReservedOptions, SpecialOptionsKey, Utils, createGlobalJsRoutesObject, defaults, root,
        hasProp = {}.hasOwnProperty;
      // slice declaration has been removed
      root = typeof exports !== "undefined" && exports !== null ? exports : this;
}).call(this);

it sets correctly root to {}

@bbrassart does this mean, that your javascript environment doesn't have array slice function? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice How this possible?

@bbrassart this is not what I asked you for. Can you submit a minimum code that does have an issue instead of one that doesn't?

@bogdan @le0pard Thanks for your help and sorry for the confusion. It appears to be all working now. I have no idea what caused this error and it is now working correctly. I will now close the issue.