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

Rollup has trouble importing apollo-cache-inmemory CommonJS dependencies. #4071

Closed
bennypowers opened this issue Oct 28, 2018 · 2 comments · Fixed by #4158
Closed

Rollup has trouble importing apollo-cache-inmemory CommonJS dependencies. #4071

bennypowers opened this issue Oct 28, 2018 · 2 comments · Fixed by #4158
Assignees

Comments

@bennypowers
Copy link
Contributor

Intended outcome:

import { InMemoryCache } from 'apollo-cache-inmemory';
const cache = new InMemoryCache();

Should not throw errors

Actual outcome:

Runtime error is thrown

ReferenceError: require is not defined [optimism.js:1:4]
at optimism.js:1:4
at InnerModuleEvaluation self-hosted:4288:5
at ModuleEvaluate self-hosted:4241:9 

How to reproduce the issue:

// rollup.config.js
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default {
  experimentalCodeSplitting: true,
  experimentalDynamicImport: true,

  input: [
    './foo-el.js'
  ],

  output: {
    file: 'bundle.js',
    format: 'es',
    sourcemap: true,
  },

  plugins: [

    // REQUIRED to roll apollo-client up
    resolve({
      browser: true,
      jsnext: true,
      module: true,
    }),

    // REQUIRED to roll apollo-client up
    commonjs({
      namedExports: {
        'apollo-cache-persist': ['persistCache'],
        'graphql-anywhere/lib/async': ['graphql'],
      },
    })

  ]
}
// client.js
import { InMemoryCache } from 'apollo-cache-inmemory';
const cache = new InMemoryCache();

Versions

  System:
    OS: macOS High Sierra 10.13.5
  Binaries:
    Node: 10.11.0 - /usr/local/bin/node
    Yarn: 1.10.1 - /usr/local/bin/yarn
    npm: 6.4.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 69.0.3497.100
    Firefox: 63.0
    Safari: 11.1.1
  npmPackages:
    apollo-cache-inmemory: ^1.3.7 => 1.3.7 
    apollo-client: ^2.4.4 => 2.4.4 
    apollo-link-http: ^1.5.5 => 1.5.5 
    lit-apollo: ^0.6.8 => 0.6.8 
  npmGlobalPackages:
    apollo-link-debounce: 1.0.3
    lit-apollo: 0.6.8

Fix

replace line one of optimism.js with

import Optimism from 'optimism';
var wrap = Optimism.wrap;

It seems like wrap should be imported as a module in the typescript source, and any typescript-related errors should be solved, rather than hinting to the compiler to disable checking on that line, not via the non-standard node/commonjs require function.

See Also

#3047

@benjamn benjamn changed the title apollo-cache-inmemory ships modules which cannot be imported Rollup has trouble importing apollo-cache-inmemory CommonJS dependencies. Oct 29, 2018
@coleHafner
Copy link

Is there any update on getting the PR merged?

@bennypowers
Copy link
Contributor Author

Amazing! Thank you!

kairos666 added a commit to kairos666/marvel-graphql-client that referenced this issue Jan 17, 2019
/!\ known bug dirty fix necessary, change first line of Optimism.js package to play nice with rollup
apollographql/apollo-client#4071
kairos666 added a commit to kairos666/marvel-graphql-client that referenced this issue Jan 17, 2019
/!\ known bug dirty fix necessary, change first line of Optimism.js package to play nice with rollup
apollographql/apollo-client#4071

/!\ useless import declaration of graphql-tag to avoid run-time error
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants