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

Is the .mjs file extension resolved by metro bundler? #59

Closed
mweststrate opened this issue Sep 18, 2017 · 7 comments
Closed

Is the .mjs file extension resolved by metro bundler? #59

mweststrate opened this issue Sep 18, 2017 · 7 comments

Comments

@mweststrate
Copy link

Hi all, quick question as library maintainer, is the .mjs file extension supported by metro? In order words, will a package json with a module field like this work like with webpack (or is module ignored completely, which would be fine as well):

"module": "lib/mobx.mjs"

mweststrate added a commit to mweststrate/metro-bundler that referenced this issue Sep 18, 2017
...assuming they have been transpiled properly that is, like webpack does.

Example setup: https://github.com/mobxjs/mobx/pull/1131/files

Should solve facebook#59

Beyond that I have no clue what I am doing, so this is just a proposal 😁
@mweststrate
Copy link
Author

Had some collegues testing it, using .mjs extension caused the bundler to fall back to the "main" field containing the UMD bundle or our test package.

@mweststrate
Copy link
Author

Update: seems that module or jsnext:main are not picked up by the bundler in the first place. A react-native field is picked up (deducted from source code, I couldn't find docs on that)

@mweststrate
Copy link
Author

What are future plans on this? I have to admit it is quite unobvious as library author how a library should be published to make it suitable for RN, without falling back to UMD / commonjs

@mweststrate
Copy link
Author

Closing for now, until .mjs has become a bit more mainstream. Still curious on thoughts around this though

@rafeca
Copy link
Contributor

rafeca commented Sep 19, 2017

Hey @mweststrate! Thanks for bringing this up.

As you mention, metro-bundler does not take into account the module field on package.json, so it should be safe to use this field in a library to expose .mjs files as long as the package still exposes a .js file in the main field.

For now I think it's better to wait until .mjs gets more traction (and Node ships a version with support for it) to implement proper support for modules in metro.

In the meantime, RN users can customize the sourceExts config param (the same way you did for the default value in #60) to make metro parse these files.

If there is a lot of demand of users wanting to use files with the mjs extension, we could merge your PR, but for the time being I'd prefer to wait until the dust around ES6 modules settles.

@cpojer
Copy link
Contributor

cpojer commented Sep 19, 2017

Please send a pull request to the resolution code in "ResolutionRequest", with tests, and we are happy to merge and support this functionality :)

@mweststrate
Copy link
Author

Tnx! Closed for now, after diving into webpack a bit further, it is not even supported officially there, so probably it is better to wait until webpack has figured it out :).

For now just added a react-native field to the package.json of all the libraries, pointing to an ES5/ES module file with old fashioned .js extension. That works well with Metro.

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

Successfully merging a pull request may close this issue.

3 participants