Skip to content
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

fix(core): Use proper swc loader options #10605

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/docusaurus-bundler/src/importFaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

import type {ConfigureWebpackUtils} from '@docusaurus/types';
import type {
MinimizerOptions as JsMinimizerOptions,
CustomOptions,
Expand Down Expand Up @@ -34,11 +33,16 @@ export async function importRspack(): Promise<FasterModule['rspack']> {
return faster.rspack;
}

export async function importSwcJsLoaderFactory(): Promise<
ConfigureWebpackUtils['getJSLoader']
export async function importSwcLoader(): Promise<string> {
const faster = await ensureFaster();
return faster.swcLoader;
}

export async function importGetSwcLoaderOptions(): Promise<
FasterModule['getSwcLoaderOptions']
> {
const faster = await ensureFaster();
return faster.getSwcJsLoaderFactory;
return faster.getSwcLoaderOptions;
}

export async function importSwcJsMinimizerOptions(): Promise<
Expand All @@ -55,9 +59,11 @@ export async function importSwcHtmlMinifier(): Promise<
return faster.getSwcHtmlMinifier();
}

export async function importBrowserslistQueries(): Promise<string[]> {
export async function importGetBrowserslistQueries(): Promise<
FasterModule['getBrowserslistQueries']
> {
const faster = await ensureFaster();
return faster.getBrowserslistQueries();
return faster.getBrowserslistQueries;
}

export async function importLightningCssMinimizerOptions(): Promise<
Expand Down
48 changes: 28 additions & 20 deletions packages/docusaurus-bundler/src/loaders/jsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import {getBabelOptions} from '@docusaurus/babel';
import {importSwcJsLoaderFactory} from '../importFaster';
import {importSwcLoader, importGetSwcLoaderOptions} from '../importFaster';
import {getCurrentBundler} from '../currentBundler';
import type {ConfigureWebpackUtils, DocusaurusConfig} from '@docusaurus/types';

Expand All @@ -20,24 +20,32 @@ const BabelJsLoaderFactory: ConfigureWebpackUtils['getJSLoader'] = ({
};
};

const RspackJsLoaderFactory: ConfigureWebpackUtils['getJSLoader'] = () => {
return {
loader: 'builtin:swc-loader',
options: {
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
},
transform: {
react: {
runtime: 'automatic',
},
},
},
},
async function createSwcLoaderFactory(): Promise<
ConfigureWebpackUtils['getJSLoader']
> {
const loader = await importSwcLoader();
const getOptions = await importGetSwcLoaderOptions();
return ({isServer}) => {
return {
loader,
options: getOptions({isServer}),
};
};
};
}

// Same as swcLoader, except we use the built-in SWC loader
async function createRspackLoaderFactory(): Promise<
ConfigureWebpackUtils['getJSLoader']
> {
const loader = 'builtin:swc-loader';
const getOptions = await importGetSwcLoaderOptions();
return ({isServer}) => {
return {
loader,
options: getOptions({isServer}),
};
};
}

// 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
Expand All @@ -61,7 +69,7 @@ export async function createJsLoaderFactory({
'When using Rspack bundler, it is required to enable swcJsLoader too',
);
}
return RspackJsLoaderFactory;
return createRspackLoaderFactory();
}
const jsLoader = siteConfig.webpack?.jsLoader ?? 'babel';
if (
Expand All @@ -76,7 +84,7 @@ export async function createJsLoaderFactory({
return ({isServer}) => jsLoader(isServer);
}
if (siteConfig.future?.experimental_faster.swcJsLoader) {
return importSwcJsLoaderFactory();
return createSwcLoaderFactory();
}
if (jsLoader === 'babel') {
return BabelJsLoaderFactory;
Expand Down
5 changes: 3 additions & 2 deletions packages/docusaurus-bundler/src/minification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import {
importSwcJsMinimizerOptions,
importLightningCssMinimizerOptions,
importBrowserslistQueries,
importGetBrowserslistQueries,
} from './importFaster';
import {getCurrentBundlerAsRspack} from './currentBundler';
import type {CustomOptions, CssNanoOptions} from 'css-minimizer-webpack-plugin';
Expand Down Expand Up @@ -141,7 +141,8 @@ async function getRspackMinimizers({
currentBundler,
}: MinimizersConfig): Promise<WebpackPluginInstance[]> {
const rspack = getCurrentBundlerAsRspack({currentBundler});
const browserslistQueries = await importBrowserslistQueries();
const getBrowserslistQueries = await importGetBrowserslistQueries();
const browserslistQueries = getBrowserslistQueries({isServer: false});
const swcJsMinimizerOptions = await importSwcJsMinimizerOptions();
return [
// See https://rspack.dev/plugins/rspack/swc-js-minimizer-rspack-plugin
Expand Down
55 changes: 30 additions & 25 deletions packages/docusaurus-faster/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,37 @@ import Rspack from '@rspack/core';
import * as lightningcss from 'lightningcss';
import browserslist from 'browserslist';
import {minify as swcHtmlMinifier} from '@swc/html';
import type {RuleSetRule} from 'webpack';
import type {JsMinifyOptions} from '@swc/core';
import type {JsMinifyOptions, Options as SwcOptions} from '@swc/core';

export const rspack = Rspack;

export function getSwcHtmlMinifier(): typeof swcHtmlMinifier {
return swcHtmlMinifier;
}
export const swcLoader = require.resolve('swc-loader');

export function getSwcJsLoaderFactory({
export const getSwcLoaderOptions = ({
isServer,
}: {
isServer: boolean;
}): RuleSetRule {
}): SwcOptions => {
return {
loader: require.resolve('swc-loader'),
options: {
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
},
transform: {
react: {
runtime: 'automatic',
},
},
target: 'es2017',
env: {
targets: getBrowserslistQueries({isServer}),
},
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
},
module: {
type: isServer ? 'commonjs' : 'es6',
transform: {
react: {
runtime: 'automatic',
},
},
},
};
};

export const rspack = Rspack;

export function getSwcHtmlMinifier(): typeof swcHtmlMinifier {
return swcHtmlMinifier;
}

// Note: these options are similar to what we use in core
Expand All @@ -68,7 +65,15 @@ export function getSwcJsMinimizerOptions(): JsMinifyOptions {

// We need this because of Rspack built-in LightningCSS integration
// See https://github.com/orgs/browserslist/discussions/846
export function getBrowserslistQueries(): string[] {
export function getBrowserslistQueries({
isServer,
}: {
isServer: boolean;
}): string[] {
if (isServer) {
return [`node ${process.versions.node}`];
}

const queries = browserslist.loadConfig({path: process.cwd()}) ?? [
...browserslist.defaults,
];
Expand Down