Change API

This commit is contained in:
Sam 2021-05-12 11:53:41 -04:00
parent b9cd75b751
commit 11e9aad800
No known key found for this signature in database
GPG key ID: 1CE6956F98C2A2A0
7 changed files with 28 additions and 12 deletions

View file

@ -63,7 +63,9 @@ export interface DocusaurusConfig {
} }
)[]; )[];
titleDelimiter?: string; titleDelimiter?: string;
getCustomJSLoader?: (isServer: boolean) => RuleSetRule; webpack?: {
jsLoader: 'babel' | ((isServer: boolean) => RuleSetRule);
};
} }
/** /**

View file

@ -187,14 +187,14 @@ async function buildLocale({
configureWebpack.bind(plugin), // The plugin lifecycle may reference `this`. configureWebpack.bind(plugin), // The plugin lifecycle may reference `this`.
clientConfig, clientConfig,
false, false,
props.siteConfig.getCustomJSLoader, props.siteConfig.webpack?.jsLoader,
); );
serverConfig = applyConfigureWebpack( serverConfig = applyConfigureWebpack(
configureWebpack.bind(plugin), // The plugin lifecycle may reference `this`. configureWebpack.bind(plugin), // The plugin lifecycle may reference `this`.
serverConfig, serverConfig,
true, true,
props.siteConfig.getCustomJSLoader, props.siteConfig.webpack?.jsLoader,
); );
} }
}); });

View file

@ -153,7 +153,7 @@ export default async function start(
configureWebpack.bind(plugin), // The plugin lifecycle may reference `this`. configureWebpack.bind(plugin), // The plugin lifecycle may reference `this`.
config, config,
false, false,
props.siteConfig.getCustomJSLoader, props.siteConfig.webpack?.jsLoader,
); );
} }
}); });

View file

@ -134,7 +134,11 @@ const ConfigSchema = Joi.object({
tagline: Joi.string().allow(''), tagline: Joi.string().allow(''),
titleDelimiter: Joi.string().default('|'), titleDelimiter: Joi.string().default('|'),
noIndex: Joi.bool().default(false), 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 // TODO move to @docusaurus/utils-validation

View file

@ -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', () => { test('getCustomizableJSLoader allows customization', () => {
const customJSLoader = (isServer: boolean): RuleSetRule => ({ const customJSLoader = (isServer: boolean): RuleSetRule => ({
loader: 'my-fast-js-loader', loader: 'my-fast-js-loader',

View file

@ -197,7 +197,7 @@ export function createBaseConfig(
test: /\.(j|t)sx?$/, test: /\.(j|t)sx?$/,
exclude: excludeJS, exclude: excludeJS,
use: [ use: [
getCustomizableJSLoader(siteConfig.getCustomJSLoader)({ getCustomizableJSLoader(siteConfig.webpack?.jsLoader)({
isServer, isServer,
babelOptions: getCustomBabelConfigFilePath(siteDir), babelOptions: getCustomBabelConfigFilePath(siteDir),
}), }),

View file

@ -151,7 +151,7 @@ function getDefaultBabelLoader({
} }
export const getCustomizableJSLoader = ( export const getCustomizableJSLoader = (
getCustomJSLoader?: (isServer: boolean) => RuleSetRule, jsLoader: 'babel' | ((isServer: boolean) => RuleSetRule) = 'babel',
) => ({ ) => ({
isServer, isServer,
babelOptions, babelOptions,
@ -159,9 +159,9 @@ export const getCustomizableJSLoader = (
isServer: boolean; isServer: boolean;
babelOptions?: TransformOptions | string; babelOptions?: TransformOptions | string;
}): RuleSetRule => }): RuleSetRule =>
getCustomJSLoader jsLoader === 'babel'
? getCustomJSLoader(isServer) ? getDefaultBabelLoader({isServer, babelOptions})
: getDefaultBabelLoader({isServer, babelOptions}); : jsLoader(isServer);
// TODO remove this before end of 2021? // TODO remove this before end of 2021?
const warnBabelLoaderOnce = memoize(function () { const warnBabelLoaderOnce = memoize(function () {
@ -197,18 +197,19 @@ function getCacheLoaderDeprecated() {
* @param configureWebpack a webpack config or a function to modify config * @param configureWebpack a webpack config or a function to modify config
* @param config initial webpack config * @param config initial webpack config
* @param isServer indicates if this is a server webpack configuration * @param isServer indicates if this is a server webpack configuration
* @param jsLoader custom js loader config
* @returns final/ modified webpack config * @returns final/ modified webpack config
*/ */
export function applyConfigureWebpack( export function applyConfigureWebpack(
configureWebpack: ConfigureWebpackFn, configureWebpack: ConfigureWebpackFn,
config: Configuration, config: Configuration,
isServer: boolean, isServer: boolean,
getCustomJSLoader?: (isServer: boolean) => RuleSetRule, jsLoader?: 'babel' | ((isServer: boolean) => RuleSetRule),
): Configuration { ): Configuration {
// Export some utility functions // Export some utility functions
const utils: ConfigureWebpackUtils = { const utils: ConfigureWebpackUtils = {
getStyleLoaders, getStyleLoaders,
getJSLoader: getCustomizableJSLoader(getCustomJSLoader), getJSLoader: getCustomizableJSLoader(jsLoader),
getBabelLoader: getBabelLoaderDeprecated, getBabelLoader: getBabelLoaderDeprecated,
getCacheLoader: getCacheLoaderDeprecated, getCacheLoader: getCacheLoaderDeprecated,
}; };