rollup / rollup-plugin-node-resolve

This module has moved and is now available at @rollup/plugin-node-resolve / https://github.com/rollup/plugins/blob/master/packages/node-resolve
MIT License
469 stars 96 forks source link

Issue bundling/resolving when module has mixed .js and .mjs files #168

Closed antstanley closed 6 years ago

antstanley commented 6 years ago

Cross-post of issue from #2333. Realised it's more likely a rollup-plugin-node-resolve issue than a rollup core issue.


Hi!

I've got an issue bundling files where the package has .js (cjs) and .mjs (esm) versions. In my case I'm bundling Apollo GraphQL server for use in a FaaS platform.

My code imports apollo-server-core (cjs only) which has a peer dependency of graphql (cjs and esm co-exist). It picks up that graphql has esm modules and uses the root index.mjs file for that module, but sub esm modules that the root tries to import it reverts back to trying to use the .js version not the .mjs version when bundling.

Error message below

src/index.js → dist/index.mjs...
[!] Error: 'GraphQLSchema' is not exported by node_modules/graphql/type/index.js
https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module
node_modules/graphql/index.mjs (36:9)
34: export { graphql, graphqlSync } from './graphql'; // Create and operate on GraphQL type definitions and schema.
35:
36: export { GraphQLSchema, // Definitions
             ^
37: GraphQLScalarType, GraphQLObjectType, GraphQLInterfaceType, GraphQLUnionType, GraphQLEnumType, GraphQLInputObjectType, GraphQLList, GraphQLNonNull, GraphQLDirective, // "Enum" of Type Kinds
38: TypeKind, // Scalars

Rollup Config

import commonjs from 'rollup-plugin-commonjs'
import resolve from 'rollup-plugin-node-resolve'
import json from 'rollup-plugin-json'

export default {
  input: 'src/index.js',
  output: {
    file: 'dist/index.mjs',
    format: 'esm'
  },
  plugins: [
    resolve({
      module: true,
      main: true
    }),
    commonjs({
      include: 'node_modules/**'
    }),
    json({
      include: 'node_modules/**'
    })
  ]
}

My app dependencies are...

"dependencies": {
    "apollo-server-core": "^2.0.0-rc.8",
    "graphql": "^14.0.0-rc.2"
  }

I am using the @rc versions of these packages

My rollup versions and plugins are...

"devDependencies": {
    "rollup": "^0.62.0",
    "rollup-plugin-commonjs": "^9.1.3",
    "rollup-plugin-json": "^3.0.0",
    "rollup-plugin-node-resolve": "^3.3.0"
  },

I've created a simpler repo to reproduce the error here https://github.com/antstanley/rollup-mjs

npm install && rollup -c should reproduce the error

I've found two workarounds.

  1. Edit node_modules/graphql/index.mjs and explicitly call the .mjs version by adding `/index.mjs' to every module exported.

  2. Or add package.json to every sub-directory with .js and .mjs files exporting different module and main packages. eg

{
    "module": "index.mjs",
    "main": "index.js"
}

Manually editing imported modules is not exactly ideal and not really sustainable.

Would it be possible to change the default import behaviour during bundling of Rollup to assume the same file extension as the file doing the import, unless overriden by package.json. So if it's bundling a .mjs file, it expects imported modules to also be .mjs files, unless package.json says otherwise. Or just put an order of preference, so it looks for .mjs before .js?

tbh I am surprised I got this far with Rollup bundling an esm module imported from a cjs module imported by my code whilst using non-stable versions of the app modules.

This isn't urgent on my part, as this is an experiment I'm doing. Adding this issue, as I'm sure it will crop up again in the future where there are mixed .js and .mjs files.

TrySound commented 6 years ago

Try to specify own extension set in nodeResolve options

antstanley commented 6 years ago

Ok. So looking at the code for rollup-plugin-node-resolve that I have in my repo from the package on npm does not match the code that is currently in master. Potentially an issue with npm publishing?

antstanley commented 6 years ago

Ok. double checked. Cloned the master branch of the repo and ran npm install it generated two files in /dist, namely rollup-plugin-node-resolve.es.js and rollup-plugin-node-resolve.cjs.js

When I run npm install --save-dev rollup-plugin-node-resolve and check node_modules/rollup-plugin-node-resolve/dist and compare the files they are different, with the installed files not having any of the .mjs updates. Double checking package.json it shows the latest version is installed "rollup-plugin-node-resolve": "^3.3.0"

Am running npm 6.1.0 & node 10.6.0

antstanley commented 6 years ago

Closing this, as this has actually been fixed, but the the branch with .mjs support hasn't been published to npm.

Created a new more accurate issue detailing the actual problem (not published to npm) at #169