Skip to content
This repository has been archived by the owner on Aug 5, 2019. It is now read-only.

Provide esm output #2

Closed
wants to merge 3 commits into from
Closed

Provide esm output #2

wants to merge 3 commits into from

Conversation

TrySound
Copy link

@TrySound TrySound commented Sep 3, 2018

In this diff I added esm support via dual mode. The idea is keeping
cjs/esm pair with the same file name and with js and mjs extensions.
Bundler may choose which format to use.

main (dist/index) bundler will choose on of these

  • dist/index.js
  • dist/index.mjs

/emotion reexports from dist/emotion

  • dist/emotion/index.js
  • dist/emotion/index.mjs

Ref https://medium.com/@giltayar/native-es-modules-in-nodejs-status-and-future-directions-part-i-ee5ea3001f71

Upgraded babel to be able to use js config.

@TrySound
Copy link
Author

TrySound commented Sep 3, 2018

/cc @jxnblk

In this diff I added esm support via dual mode. The idea is keeping
cjs/esm pair with the same file name and with js and mjs extensions.
Bundler may choose which format to use.

main (dist/index) bundler will choose on of these
- dist/index.js
- dist/index.mjs

/emotion reexports from dist/emotion
- dist/emotion/index.js
- dist/emotion/index.mjs

Ref https://medium.com/@giltayar/native-es-modules-in-nodejs-status-and-future-directions-part-i-ee5ea3001f71

Upgraded babel to be able to use js config.
@TrySound
Copy link
Author

TrySound commented Sep 6, 2018

Friendly ping @jxnblk

I could help with other packages if you are okay with this PR.

@TrySound
Copy link
Author

TrySound commented Sep 6, 2018

@jxnblk Without this I am not able to bundle system-components/emotion with rollup without hacks because of dumb reexport module.exports = require().

.babelrc.js Outdated
plugins: []
}

if (process.env.NODE_ENV.includes('emotion')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could still be handled with the env field right? Sorry haven't had time to dig into some of the Babel 7 changes yet

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env field works only with json config. I use js here to achieve some logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I haven't seen anything about the env field being deprecated in Babel 7 – if you happen to have a link to the docs that'd be helpful

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not deprecated (well, it was for a while).

env field just not used by js config. Consider json version with env field is a wrapper around it. Don't know where this mentioned in docs to be honest.

package.json Outdated
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bridge is just for jest since it doesn't fully support Babel 7 yet, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

package.json Outdated
"styled:esm": "NODE_ENV=styled:esm babel src/index.js -o dist/index.mjs",
"emotion:cjs": "NODE_ENV=emotion:cjs babel src/index.js -o dist/emotion/index.js",
"emotion:esm": "NODE_ENV=emotion:esm babel src/index.js -o dist/emotion/index.mjs",
"prepare": "rm -rf dist && mkdir dist && mkdir dist/emotion && npm run styled:cjs && npm run styled:esm && npm run emotion:cjs && npm run emotion:esm",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't used to need to mkdir for babel-cli, is this because they're subdirectories? If so, why not keep it flat?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems like a regression in v7. Opened PR babel/babel#8622
Do you want (index.js,emotion.js)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I mean if you're switching from compiling directories to compiling files, I don't see the need for additional subdirectories

@jxnblk
Copy link
Member

jxnblk commented Sep 6, 2018

Thanks for this! I like using babel for small packages like this a lot more than having to include rollup as an additional dependency/config. I'm planning on working on Rebass some over the weekend so will probably push a release then

@TrySound
Copy link
Author

TrySound commented Sep 7, 2018

Made this structure

# static
emotion.js
emotion.mjs
# generated
dist/styled.js
dist/styled.mjs
dist/emotion.js
dist/emotion.mjs

@TrySound TrySound closed this Oct 20, 2018
@TrySound
Copy link
Author

I'm not gonna contribute to this project anymore.

@TrySound TrySound deleted the esm-support branch October 20, 2018 18:00
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 this pull request may close these issues.

None yet

2 participants