-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2): Allow configuring babel via babel.config.js (#2903)
* feat(v2): Allow configuring babel via docusaurus.config.js * Use api.caller feature from babel to avoid expose isServer to users * Remove unused optional config key * Make babel loader resolve and require config file
- Loading branch information
1 parent
68a1bb1
commit 729b3ca
Showing
9 changed files
with
154 additions
and
69 deletions.
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/[email protected]. 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,11 @@ | |
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import path from 'path'; | ||
import MiniCssExtractPlugin from 'mini-css-extract-plugin'; | ||
import env from 'std-env'; | ||
import merge from 'webpack-merge'; | ||
import {Configuration, Loader} from 'webpack'; | ||
import {TransformOptions} from '@babel/core'; | ||
|
||
import {version as cacheLoaderVersion} from 'cache-loader/package.json'; | ||
|
||
|
@@ -85,71 +85,30 @@ export function getCacheLoader( | |
}; | ||
} | ||
|
||
export function getBabelLoader(isServer: boolean, babelOptions?: {}): Loader { | ||
const absoluteRuntimePath = path.dirname( | ||
require.resolve(`@babel/runtime/package.json`), | ||
); | ||
return { | ||
loader: require.resolve('babel-loader'), | ||
options: Object.assign( | ||
export function getBabelLoader( | ||
isServer: boolean, | ||
babelOptions?: TransformOptions | string, | ||
): Loader { | ||
let options: TransformOptions; | ||
if (typeof babelOptions === 'string') { | ||
options = { | ||
babelrc: false, | ||
configFile: babelOptions, | ||
caller: {name: isServer ? 'server' : 'client'}, | ||
}; | ||
} else { | ||
options = Object.assign( | ||
babelOptions ?? {presets: [require.resolve('../babel/preset')]}, | ||
{ | ||
babelrc: false, | ||
configFile: false, | ||
// 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/[email protected]. 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'), | ||
], | ||
caller: {name: isServer ? 'server' : 'client'}, | ||
}, | ||
babelOptions, | ||
), | ||
); | ||
} | ||
return { | ||
loader: require.resolve('babel-loader'), | ||
options, | ||
}; | ||
} | ||
|
||
|
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