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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.9.7] UMD incorrectly tree shaking module #1740

Closed
michaeltugby0 opened this issue Jul 16, 2018 · 4 comments 路 Fixed by #2993
Closed

[1.9.7] UMD incorrectly tree shaking module #1740

michaeltugby0 opened this issue Jul 16, 2018 · 4 comments 路 Fixed by #2993

Comments

@michaeltugby0
Copy link

michaeltugby0 commented Jul 16, 2018

馃悰 bug report

When using the experimental scope hoisting argument on build, I'm getting the following error:
Uncaught TypeError: Object prototype may only be an Object or null: undefined
The error does not appear if I build without experimental scope hoisting.

馃帥 Configuration (.babelrc, package.json, cli command)

Tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "~/*": ["./*"]
    },
    "outDir": "out-tsc",
    "module": "esnext",
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "strictNullChecks": true,
    "emitDecoratorMetadata": true,
    "target": "es6",
    "jsx": "react",
    "sourceMap": true
  }
}

馃 Expected Behavior

It should build correctly.

馃槸 Current Behavior

This function was throwing the error:

    var dq = Ba && Ba.__extends || function() {
        var e = Object.setPrototypeOf || {
            __proto__: []
        }instanceof Array && function(e, r) {
            e.__proto__ = r
        }
        || function(e, r) {
            for (var t in r)
                r.hasOwnProperty(t) && (e[t] = r[t])
        }
        ;
        return function(r, t) {
            function n() {
                this.constructor = r
            }
            e(r, t),
            r.prototype = null === t ? Object.create(t) : (n.prototype = t.prototype,
            new n)
        }
    }()

The function seems to be generated automatically by the typescript compiler, and is simulating single class inheritance.

    var rm = function(e) {
        function r(r) {
            return e.call(this, sm(r).request) || this
        }
        return dq(r, e),
        r
    }(Qm);

Here, the function is called with an undefined value (e/Qm), hence the error. Following the stack trace, it brings me back to a file called httpLink.js in the apollo-link-http library.

httpLink.js before Parcel build:

import { ApolloLink, Observable, fromError } from 'apollo-link';
return new ApolloLink(function (operation) {
...
}

In Parcel bundle:

var lE, kE, jE, iE, hE, gE, pj;
...
return new pj(function(e) {
...
}

ApolloLink doesn't seem to appear in the bundle at all. There is just an undeclared variable in its place.

馃拋 Possible Solution

The problem seems to stem from the fact that importing just the ApolloProvider from react-apollo is tree shaking apollo-client out of the bundle.

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('prop-types'), require('apollo-client')) :
    typeof define === 'function' && define.amd ? define(['exports', 'react', 'prop-types', 'apollo-client'], factory) :
    (factory((global['react-apollo'] = {}),global.React,global.PropTypes,global.apolloClient));
}(this, (function (exports,React,PropTypes,apolloClient) {
var ApolloProvider = ...
}

Since ApolloProvider does not use the apolloClient import here, it gets taken out. But, apollo-client is still required by the app:

import { ApolloClient } from "apollo-client";
import { HttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloProvider } from "react-apollo";

The error seems to go away if I remove the apolloClient import in the UMD module, or if I import the ApolloProvider file directly.

馃敠 Context

Couldn't build an application using Scope Hoisting and Apollo due to the problem.

馃捇 Code Sample

https://github.com/Icestorm05/react-apollo-test

馃實 Your Environment

Software Version(s)
Parcel 1.9.7
Node 10.6.0
npm/Yarn 6.1.0
Operating System macOS 10.13.1 High Sierra
@sqs
Copy link

sqs commented Jul 22, 2018

I am also seeing modules be omitted that should have been included, when using TypeScript export * from './module' with --experimental-scope-hoisting on Parcel 1.9.6.

@michaeltugby0 michaeltugby0 changed the title [1.9.7] Uncaught TypeError: Object prototype may only be an Object or null: undefined with Experimental scope hoisting [1.9.7] Export from syntax omitting modules with Experimental scope hoisting Jul 22, 2018
@michaeltugby0
Copy link
Author

Had a little play around with it today, and I don't think the issue is to do with export from at all. It seems to be because of a UMD module. I'll update the possible solution. The export from fix I mentioned previously may have just been deceptive.

@sqs Your issue could be the same as mine, could you check it out if you can?

@michaeltugby0 michaeltugby0 changed the title [1.9.7] Export from syntax omitting modules with Experimental scope hoisting [1.9.7] UMD incorrectly tree shaking module Jul 22, 2018
@sqs
Copy link

sqs commented Jul 23, 2018

@sqs Your issue could be the same as mine, could you check it out if you can?

Yes, mine is a different issue then. Thanks.

@devongovett
Copy link
Member

Appears to be fixed by #2993.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants