-
-
Notifications
You must be signed in to change notification settings - Fork 27k
adding options for esmodules and commonjs build targets (#5216) #6505
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some open questions
const env = process.env.BABEL_ENV || process.env.NODE_ENV; | ||
return create( | ||
api, | ||
Object.assign({ helpers: false }, opts, { useESModules: true }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this default absolutRuntime
to false
?
const env = process.env.BABEL_ENV || process.env.NODE_ENV; | ||
return create( | ||
api, | ||
Object.assign({ helpers: false }, opts, { useCommonJS: true }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this default absolutRuntime
to false
?
targets: { | ||
node: 'current', | ||
}, | ||
targets, | ||
// Do not transform modules to CJS | ||
modules: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be true
for tests?
@@ -35,6 +37,7 @@ | |||
"babel-loader": "8.0.5", | |||
"babel-plugin-dynamic-import-node": "2.2.0", | |||
"babel-plugin-macros": "2.5.0", | |||
"babel-plugin-transform-dynamic-import": "2.1.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This plugin was missing 🤷
This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs. |
This pull request has been automatically closed because it has not had any recent activity. If you have a question or comment, please open a new issue. Thank you for your contribution! |
Fixes #5216
Previous attempt: #5264
Partially related: #6249
This makes it possible to use
babel-preset-react-app
to create NPM packages. I have an example project using this configuration: https://github.com/zumper/react-laddaOne breaking change is that this changes the default for
useESModules
to false and also changes what that option means.Usage with Rollup
If you are creating an NPM package that contains a React component you can use the options for
commonjs
andesmodules
to create proper builds forlib
,es
anddist
folders. The configuration example below will work for most common cases but will not be suitable to all projects. Similar setups are used by popular NPM packages such as react-redux and react-router.package.json
This is a simplified
package.json
file showing how the build script might be configured.Notably absent from the example file below are any dependencies and jest configuration. You can see a more complete example in the
react-ladda
package.babel.config.js
When building for
lib
,es
folders you want to set theabsoluteRuntime
tofalse
. Letting theabsoluteRuntime
default totrue
will include the full local path to the runtime in your build, which is undesirable for a published NPM package. When building for thedist
folder, you also want to disable helpers (because Rollup manages helpers automatically).Note that it is recommended to set
NODE_ENV
environment variable to "production" when building an NPM package. SettingNODE_ENV
to "development" will put the@babel/preset-react
plugin into development mode, which is undesirable for a published NPM package.rollup.config.js