Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

zen-observable-ts combines 'export' and 'require' #558

Closed
acoreyj opened this issue Mar 23, 2018 · 12 comments
Closed

zen-observable-ts combines 'export' and 'require' #558

acoreyj opened this issue Mar 23, 2018 · 12 comments

Comments

@acoreyj
Copy link
Contributor

acoreyj commented Mar 23, 2018

Expected Behavior
Can package zen-observable-ts with Rollup

Actual Behavior
zen-observable-ts results in
ReferenceError: require is not defined

This is because the compiled zenObservable.js is

export var Observable = require('zen-observable');

As you can see here rollup/rollup#1058 (comment) you shouldn't combine export and require in the same file as commonjs will ignore the file if it has export/import.

It should compile to something like

import * as zenObservable from 'zen-observable';
export var Observable = zenObservable;
acoreyj pushed a commit to acoreyj/apollo-link that referenced this issue Mar 23, 2018
acoreyj pushed a commit to acoreyj/apollo-link that referenced this issue Mar 23, 2018
@acoreyj
Copy link
Contributor Author

acoreyj commented Mar 23, 2018

added PR that fixes the issue

@evans
Copy link
Contributor

evans commented Mar 27, 2018

@acoreyj Thanks for opening the PR and Issue! I'm not sure how to rectify this, since using the import syntax will break React Native. See #389, #248, and #515. Do you have an example project that I can clone?

@acoreyj
Copy link
Contributor Author

acoreyj commented Mar 28, 2018

@evans
Yup I did just run into #248 with my fix. I updated my PR to what I think is an actual fix. Basically we need to export the default of zen-observable.

I created a repo showing this error here https://github.com/acoreyj/apollo-link-observable-rollup-issue

See the readme for more details.

Definitely some strange behavior here, my guess is stemming from how zen-observable handles it's exports? Possibly fixed in most recent version?

@gaberudy
Copy link

This is definitely an issue and I am experiencing it myself with the rollup builds.

This is very painful, hopefully this PR can be merged in.

@acoreyj
Copy link
Contributor Author

acoreyj commented Mar 29, 2018

in the Pull Request I added the allowSyntheticDefaultImports to the tsConfig so now the fixed zenObservable.js will look more normal

Note I haven't tested this change with react-native but it definitely fixes #248

import zenObservable from 'zen-observable';
export var Observable = zenObservable;

@bennypowers
Copy link

Anything I can do to help this along?

@acoreyj
Copy link
Contributor Author

acoreyj commented Apr 10, 2018

@bennypowers

Not sure what else can be done, repo to reproduce is here and PR to fix is here

In the past they mentioned switching to rxjs which would resolve this problem, perhaps that is being actively worked on.

@evans
Copy link
Contributor

evans commented Apr 10, 2018

@acoreyj @bennypowers Thank you for the help so far! I've been chasing after some server changes. I believe the changes in #559 will work for rollup and probably react native.

I think what would help is adding a simple test to bundle zen-observable-ts with rollup. Part of my hesitation is being burned by these types of changes in the past without being able to understand what is going to break. A test would ensure that we don't break things in the future. Does a test like this sound feasible?

@acoreyj
Copy link
Contributor Author

acoreyj commented Apr 11, 2018

@evans

I looked into it and it doesn't seem very practical to test for since the error is at runtime due to the require statement. I think the test would have to use the compiled bundle and then use some sort of headless browser to test. It seems like the Jest test handles the import fine regardless of what method is used.

I'm no Jest expert though so maybe someone else would have some input on something like this, I couldn't figure it out though.

@bennypowers
Copy link

@acoreyj I think you hit the nail on the head there. This is about (trying and ultimately failing at) running apollo in the browser. A test using headless chrome and selenium or something like cypress is what's needed here.

evans pushed a commit to acoreyj/apollo-link that referenced this issue Apr 12, 2018
evans pushed a commit to acoreyj/apollo-link that referenced this issue Apr 12, 2018
@evans evans closed this as completed in 2468565 Apr 12, 2018
@evans
Copy link
Contributor

evans commented Apr 12, 2018

@acoreyj Thanks for bringing up the issue originally!

@bennypowers I think there would be a way to test the bundles for no requires by removing it from the global scope with something like delete global.require, similar to how we do the check for no fetch in this http-common test. If that makes sense, would really appreciate a change to add a test!

Once CI passes, I'll be able to publish

@arjunyel
Copy link

Any chance once this lands you can also add the latest version of this to apollo-boost? If you want an error reproduction: apollographql/apollo-client#3276

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

No branches or pull requests

5 participants