docusaurus/packages/docusaurus-bundler/src/loaders/jsLoader.ts
Sébastien Lorber 0372ecd1e9
Some checks failed
Argos CI / take-screenshots (push) Has been cancelled
Build Hash Router / Build Hash Router (push) Has been cancelled
Canary Release / Publish Canary (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Continuous Releases / Continuous Releases (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (20) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (20.0) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (22) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (24) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 (25) (push) Has been cancelled
E2E Tests / E2E — Yarn v1 Windows (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (node-modules, -s) (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (node-modules, -st) (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (pnp, -s) (push) Has been cancelled
E2E Tests / E2E — Yarn Berry (pnp, -st) (push) Has been cancelled
E2E Tests / E2E — npm (push) Has been cancelled
E2E Tests / E2E — pnpm (push) Has been cancelled
fix(faster): fix server build SWC / browserslist node target (#11496)
2025-10-17 21:02:45 +02:00

88 lines
2.9 KiB
TypeScript

/**
* 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.
*/
import {getBabelOptions} from '@docusaurus/babel';
import {importSwcLoader, importGetSwcLoaderOptions} from '../importFaster';
import {getCurrentBundler} from '../currentBundler';
import type {ConfigureWebpackUtils, DocusaurusConfig} from '@docusaurus/types';
const BabelJsLoaderFactory: ConfigureWebpackUtils['getJSLoader'] = ({
isServer,
babelOptions,
}) => {
return {
loader: require.resolve('babel-loader'),
options: getBabelOptions({isServer, babelOptions}),
};
};
async function createSwcJsLoaderFactory(): Promise<
ConfigureWebpackUtils['getJSLoader']
> {
const loader = await importSwcLoader();
const getOptions = await importGetSwcLoaderOptions();
return ({isServer}) => {
return {
loader,
options: getOptions({isServer, bundlerName: 'webpack'}),
};
};
}
// Same as swcLoader, except we use the built-in SWC loader
async function createRspackSwcJsLoaderFactory(): Promise<
ConfigureWebpackUtils['getJSLoader']
> {
const loader = 'builtin:swc-loader';
const getOptions = await importGetSwcLoaderOptions();
return ({isServer}) => {
return {
loader,
options: getOptions({isServer, bundlerName: 'rspack'}),
};
};
}
// Confusing: function that creates a function that creates actual js loaders
// This is done on purpose because the js loader factory is a public API
// It is injected in configureWebpack plugin lifecycle for plugin authors
export async function createJsLoaderFactory({
siteConfig,
}: {
siteConfig: {
webpack?: DocusaurusConfig['webpack'];
future: {
experimental_faster: DocusaurusConfig['future']['experimental_faster'];
};
};
}): Promise<ConfigureWebpackUtils['getJSLoader']> {
const currentBundler = await getCurrentBundler({siteConfig});
const isSWCLoader = siteConfig.future.experimental_faster.swcJsLoader;
if (isSWCLoader) {
if (siteConfig.webpack?.jsLoader) {
throw new Error(
`You can't use siteConfig.webpack.jsLoader and siteConfig.future.experimental_faster.swcJsLoader at the same time.
To avoid any configuration ambiguity, you must make an explicit choice:
- If you want to use Docusaurus Faster and SWC (recommended), remove siteConfig.webpack.jsLoader
- If you want to use a custom JS loader, use siteConfig.future.experimental_faster.swcJsLoader: false`,
);
}
return currentBundler.name === 'rspack'
? createRspackSwcJsLoaderFactory()
: createSwcJsLoaderFactory();
}
const jsLoader = siteConfig.webpack?.jsLoader ?? 'babel';
if (jsLoader instanceof Function) {
return ({isServer}) => jsLoader(isServer);
}
if (jsLoader === 'babel') {
return BabelJsLoaderFactory;
}
throw new Error(`Docusaurus bug: unexpected jsLoader value${jsLoader}`);
}