-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
feat(v2): Allow configuring babel via babel.config.js #2903
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f6b3455
feat(v2): Allow configuring babel via docusaurus.config.js
SamChou19815 d872c17
Use api.caller feature from babel to avoid expose isServer to users
SamChou19815 614f0f9
Remove unused optional config key
SamChou19815 aba33b4
Make babel loader resolve and require config file
SamChou19815 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
presets: [require.resolve('@docusaurus/core/lib/babel/preset')], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
presets: [require.resolve('@docusaurus/core/lib/babel/preset')], | ||
}; |
12 changes: 12 additions & 0 deletions
12
packages/docusaurus-init/templates/facebook/babel.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
*/ | ||
|
||
module.exports = { | ||
presets: [require.resolve('@docusaurus/core/lib/babel/preset')], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import path from 'path'; | ||
import {ConfigAPI, TransformOptions} from '@babel/core'; | ||
|
||
function getTransformOptions(isServer: boolean): TransformOptions { | ||
const absoluteRuntimePath = path.dirname( | ||
require.resolve(`@babel/runtime/package.json`), | ||
); | ||
return { | ||
// All optional newlines and whitespace will be omitted when generating code in compact mode | ||
compact: true, | ||
presets: [ | ||
isServer | ||
? [ | ||
require.resolve('@babel/preset-env'), | ||
{ | ||
targets: { | ||
node: 'current', | ||
}, | ||
}, | ||
] | ||
: [ | ||
require.resolve('@babel/preset-env'), | ||
{ | ||
useBuiltIns: 'usage', | ||
loose: true, | ||
corejs: '2', | ||
// Do not transform modules to CJS | ||
modules: false, | ||
// Exclude transforms that make all code slower | ||
exclude: ['transform-typeof-symbol'], | ||
}, | ||
], | ||
require.resolve('@babel/preset-react'), | ||
require.resolve('@babel/preset-typescript'), | ||
], | ||
plugins: [ | ||
// Polyfills the runtime needed for async/await, generators, and friends | ||
// https://babeljs.io/docs/en/babel-plugin-transform-runtime | ||
[ | ||
require.resolve('@babel/plugin-transform-runtime'), | ||
{ | ||
corejs: false, | ||
helpers: true, | ||
// By default, it assumes @babel/runtime@7.0.0. Since we use >7.0.0, better to | ||
// explicitly specify the version so that it can reuse the helper better | ||
// See https://github.com/babel/babel/issues/10261 | ||
version: require('@babel/runtime/package.json').version, | ||
regenerator: true, | ||
useESModules: true, | ||
// Undocumented option that lets us encapsulate our runtime, ensuring | ||
// the correct version is used | ||
// https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-plugin-transform-runtime/src/index.js#L35-L42 | ||
absoluteRuntime: absoluteRuntimePath, | ||
}, | ||
], | ||
// Adds syntax support for import() | ||
isServer | ||
? require.resolve('babel-plugin-dynamic-import-node') | ||
: require.resolve('@babel/plugin-syntax-dynamic-import'), | ||
], | ||
}; | ||
} | ||
|
||
function babelPresets(api: ConfigAPI): TransformOptions { | ||
const caller = api.caller((caller) => caller?.name); | ||
return getTransformOptions(caller === 'server'); | ||
} | ||
|
||
export = babelPresets; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
*/ | ||
|
||
module.exports = { | ||
presets: [require.resolve('@docusaurus/core/lib/babel/preset')], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,12 @@ However, it can be helpful if you have a high-level understanding of how the con | |
|
||
The high-level overview of Docusaurus configuration can be categorized into: | ||
|
||
- [Site metadata](#site-metadata) | ||
- [Deployment configurations](#deployment-configurations) | ||
- [Theme, plugin, and preset configurations](#theme-plugin-and-preset-configurations) | ||
- [Custom configurations](#custom-configurations) | ||
- [What goes into a `docusaurus.config.js`?](#what-goes-into-a-docusaurusconfigjs) | ||
- [Site metadata](#site-metadata) | ||
- [Deployment configurations](#deployment-configurations) | ||
- [Theme, plugin, and preset configurations](#theme-plugin-and-preset-configurations) | ||
- [Custom configurations](#custom-configurations) | ||
- [Customizing Babel Configuration](#customizing-babel-configuration) | ||
|
||
For exact reference to each of the configurable fields, you may refer to [**`docusaurus.config.js` API reference**](docusaurus.config.js.md). | ||
|
||
|
@@ -149,3 +151,15 @@ const Hello = () => { | |
If you just want to use those fields on the client side, you could create your own JS files and import them as ES6 modules, there is no need to put them in `docusaurus.config.js`. | ||
|
||
::: | ||
|
||
## Customizing Babel Configuration | ||
|
||
For new Docusaurus projects, we automatically generated a `babel.config.js` in project root. | ||
|
||
```js title="babel.config.js" | ||
module.exports = { | ||
presets: [require.resolve('@docusaurus/core/lib/babel/preset')], | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. require.resolve() is for yarn2 right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
``` | ||
|
||
Most of the times, this configuration will work just fine. If you want to customize it, you can directly edit this file to customize babel configuration. For your changes to take effect, you need to restart Docusaurus devserver. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think it's not worth to keep the babelOptions object here, as it's an internal method and it won't ever be called with an object.
I'd rather have:
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.
It's not completely internal. It has been exposed here: https://v2.docusaurus.io/docs/lifecycle-apis#configurewebpackconfig-isserver-utils
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.
ah :) that's fine then, I guess we need to be retrocompatible