Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ns prototype is null, but that breaks some things #2147

Closed
joeldenning opened this issue Mar 18, 2020 · 7 comments
Closed

ns prototype is null, but that breaks some things #2147

joeldenning opened this issue Mar 18, 2020 · 7 comments

Comments

@joeldenning
Copy link
Collaborator

const ns = Object.create(null);

Some projects assume that their dependencies have the full object prototype available to them. For example, the popular react-table project does this (see line 7 of react-table.development.js)

This throws an Error when the dependency (in this case react) does not have the Object.prototype methods on it.

Is there any reason not to change that code to be ns = {}, so that the prototype is Object.prototype?

@guybedford thoughts?

@guybedford
Copy link
Member

React isn't an ES module though so shouldn't be a namespace - rather it should be used via import React from 'react' as its own default export.

Is this because you are using the named exports extension?

It is very important not to change this - namespaces have an empty prototype by the ECMAScript specification.

@joeldenning
Copy link
Collaborator Author

Yes, I discovered this when talking with someone setting up single-spa. They are using the named-exports extra because that's the easiest way to be able to do import { useState } from 'react';

@guybedford
Copy link
Member

I would suggest getting React table to fix this as a bug in that project then - that they are detecting a namespace using a technique that is not spec valid for namespaces.

@bartoszgolebiowski
Copy link

bartoszgolebiowski commented Mar 18, 2020

I had a serious problem with that
This is my dependency which I am using.

(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
  typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
  (global = global || self, factory(global.ReactQuery = {}, global.React));
}(this, (function (exports, React) { 'use strict';

  React = React && React.hasOwnProperty('default') ? React['default'] : React

The last line couse exception

React variable is Module and does not have hasOwnProperty function.

This is caused because React variable is the module and does not have hasOwnProperty method

this is a screenshot right before the exception accrued.
image

To solve it I need to invoke this formula at the very beginning before importing react-table

    System.import('react')
      .then(ns => Object.setPrototypeOf(ns, Object.prototype))

@guybedford
Copy link
Member

The problem is this bug in Rollup - rollup/rollup#3420 which was released in 1.32.1 - when React table uses that build it will resolve the issue.

I would suggest posting an issue to React table to get a new build out with that release asap.

@joeldenning
Copy link
Collaborator Author

joeldenning commented Mar 18, 2020

Good catch - I had just verified that rollup does this correctly now with this repl example. It looks react-table isn't yet on 1.32.1 - https://github.com/tannerlinsley/react-table/blob/master/yarn.lock#L6720. I'll create a pull request to upgrade this for them.

@joeldenning
Copy link
Collaborator Author

I have created TanStack/table#2021 to resolve this issue in react-table.

tannerlinsley added a commit to TanStack/table that referenced this issue Apr 1, 2020
…stemjs#2147. (#2021)

Co-authored-by: Tanner Linsley <tannerlinsley@gmail.com>
opensource521 pushed a commit to opensource521/react-table-maker that referenced this issue Apr 10, 2020
…stemjs#2147. (#2021)

Co-authored-by: Tanner Linsley <tannerlinsley@gmail.com>
zach393 pushed a commit to zach393/reactTable that referenced this issue Dec 11, 2020
…stemjs#2147. (#2021)

Co-authored-by: Tanner Linsley <tannerlinsley@gmail.com>
Cheetah0723 added a commit to Cheetah0723/Table that referenced this issue Nov 7, 2021
…stemjs#2147. (#2021)

Co-authored-by: Tanner Linsley <tannerlinsley@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants