mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-29 06:29:03 +02:00
Change API
This commit is contained in:
parent
b9cd75b751
commit
11e9aad800
7 changed files with 28 additions and 12 deletions
4
packages/docusaurus-types/src/index.d.ts
vendored
4
packages/docusaurus-types/src/index.d.ts
vendored
|
@ -63,7 +63,9 @@ export interface DocusaurusConfig {
|
|||
}
|
||||
)[];
|
||||
titleDelimiter?: string;
|
||||
getCustomJSLoader?: (isServer: boolean) => RuleSetRule;
|
||||
webpack?: {
|
||||
jsLoader: 'babel' | ((isServer: boolean) => RuleSetRule);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -187,14 +187,14 @@ async function buildLocale({
|
|||
configureWebpack.bind(plugin), // The plugin lifecycle may reference `this`.
|
||||
clientConfig,
|
||||
false,
|
||||
props.siteConfig.getCustomJSLoader,
|
||||
props.siteConfig.webpack?.jsLoader,
|
||||
);
|
||||
|
||||
serverConfig = applyConfigureWebpack(
|
||||
configureWebpack.bind(plugin), // The plugin lifecycle may reference `this`.
|
||||
serverConfig,
|
||||
true,
|
||||
props.siteConfig.getCustomJSLoader,
|
||||
props.siteConfig.webpack?.jsLoader,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -153,7 +153,7 @@ export default async function start(
|
|||
configureWebpack.bind(plugin), // The plugin lifecycle may reference `this`.
|
||||
config,
|
||||
false,
|
||||
props.siteConfig.getCustomJSLoader,
|
||||
props.siteConfig.webpack?.jsLoader,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -134,7 +134,11 @@ const ConfigSchema = Joi.object({
|
|||
tagline: Joi.string().allow(''),
|
||||
titleDelimiter: Joi.string().default('|'),
|
||||
noIndex: Joi.bool().default(false),
|
||||
getCustomJSLoader: Joi.function(),
|
||||
webpack: Joi.object({
|
||||
jsLoader: Joi.alternatives()
|
||||
.try(Joi.string().equal('babel'), Joi.function())
|
||||
.optional(),
|
||||
}).optional(),
|
||||
});
|
||||
|
||||
// TODO move to @docusaurus/utils-validation
|
||||
|
|
|
@ -29,6 +29,15 @@ describe('customize JS loader', () => {
|
|||
);
|
||||
});
|
||||
|
||||
test('getCustomizableJSLoader accepts loaders with preset', () => {
|
||||
expect(getCustomizableJSLoader('babel')({isServer: true}).loader).toBe(
|
||||
require.resolve('babel-loader'),
|
||||
);
|
||||
expect(getCustomizableJSLoader('babel')({isServer: false}).loader).toBe(
|
||||
require.resolve('babel-loader'),
|
||||
);
|
||||
});
|
||||
|
||||
test('getCustomizableJSLoader allows customization', () => {
|
||||
const customJSLoader = (isServer: boolean): RuleSetRule => ({
|
||||
loader: 'my-fast-js-loader',
|
||||
|
|
|
@ -197,7 +197,7 @@ export function createBaseConfig(
|
|||
test: /\.(j|t)sx?$/,
|
||||
exclude: excludeJS,
|
||||
use: [
|
||||
getCustomizableJSLoader(siteConfig.getCustomJSLoader)({
|
||||
getCustomizableJSLoader(siteConfig.webpack?.jsLoader)({
|
||||
isServer,
|
||||
babelOptions: getCustomBabelConfigFilePath(siteDir),
|
||||
}),
|
||||
|
|
|
@ -151,7 +151,7 @@ function getDefaultBabelLoader({
|
|||
}
|
||||
|
||||
export const getCustomizableJSLoader = (
|
||||
getCustomJSLoader?: (isServer: boolean) => RuleSetRule,
|
||||
jsLoader: 'babel' | ((isServer: boolean) => RuleSetRule) = 'babel',
|
||||
) => ({
|
||||
isServer,
|
||||
babelOptions,
|
||||
|
@ -159,9 +159,9 @@ export const getCustomizableJSLoader = (
|
|||
isServer: boolean;
|
||||
babelOptions?: TransformOptions | string;
|
||||
}): RuleSetRule =>
|
||||
getCustomJSLoader
|
||||
? getCustomJSLoader(isServer)
|
||||
: getDefaultBabelLoader({isServer, babelOptions});
|
||||
jsLoader === 'babel'
|
||||
? getDefaultBabelLoader({isServer, babelOptions})
|
||||
: jsLoader(isServer);
|
||||
|
||||
// TODO remove this before end of 2021?
|
||||
const warnBabelLoaderOnce = memoize(function () {
|
||||
|
@ -197,18 +197,19 @@ function getCacheLoaderDeprecated() {
|
|||
* @param configureWebpack a webpack config or a function to modify config
|
||||
* @param config initial webpack config
|
||||
* @param isServer indicates if this is a server webpack configuration
|
||||
* @param jsLoader custom js loader config
|
||||
* @returns final/ modified webpack config
|
||||
*/
|
||||
export function applyConfigureWebpack(
|
||||
configureWebpack: ConfigureWebpackFn,
|
||||
config: Configuration,
|
||||
isServer: boolean,
|
||||
getCustomJSLoader?: (isServer: boolean) => RuleSetRule,
|
||||
jsLoader?: 'babel' | ((isServer: boolean) => RuleSetRule),
|
||||
): Configuration {
|
||||
// Export some utility functions
|
||||
const utils: ConfigureWebpackUtils = {
|
||||
getStyleLoaders,
|
||||
getJSLoader: getCustomizableJSLoader(getCustomJSLoader),
|
||||
getJSLoader: getCustomizableJSLoader(jsLoader),
|
||||
getBabelLoader: getBabelLoaderDeprecated,
|
||||
getCacheLoader: getCacheLoaderDeprecated,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue