-
-
Notifications
You must be signed in to change notification settings - Fork 27.1k
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
heygrady
left a comment
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, | ||
| // 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?
| "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-appto 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
useESModulesto 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
commonjsandesmodulesto create proper builds forlib,esanddistfolders. 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.jsonThis is a simplified
package.jsonfile 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-laddapackage.{ "name": "my-package-name", "version": "1.0.0", "main": "lib/index.js", "unpkg": "dist/my-package-name.js", "module": "es/index.js", "files": [ "dist", "lib", "src", "es" ], "scripts": { "build:commonjs": "cross-env MODULES_ENV=commonjs babel src --out-dir lib", "build:es": "cross-env MODULES_ENV=esmodules babel src --out-dir es", "build:umd": "rollup -c", "build": "yarn build:commonjs && yarn build:es && yarn build:umd", "clean": "rimraf lib dist es coverage", "prepare": "yarn clean && yarn build", "test": "jest", } }babel.config.jsWhen building for
lib,esfolders you want to set theabsoluteRuntimetofalse. Letting theabsoluteRuntimedefault totruewill include the full local path to the runtime in your build, which is undesirable for a published NPM package. When building for thedistfolder, you also want to disable helpers (because Rollup manages helpers automatically).Note that it is recommended to set
NODE_ENVenvironment variable to "production" when building an NPM package. SettingNODE_ENVto "development" will put the@babel/preset-reactplugin into development mode, which is undesirable for a published NPM package.rollup.config.js