@@ -9,18 +9,22 @@ import Rspack from '@rspack/core';
99import * as lightningcss from 'lightningcss' ;
1010import browserslist from 'browserslist' ;
1111import { minify as swcHtmlMinifier } from '@swc/html' ;
12+ import semver from 'semver' ;
1213import type { JsMinifyOptions , Options as SwcOptions } from '@swc/core' ;
14+ import type { CurrentBundler } from '@docusaurus/types' ;
1315
1416export const swcLoader = require . resolve ( 'swc-loader' ) ;
1517
1618export const getSwcLoaderOptions = ( {
1719 isServer,
20+ bundlerName,
1821} : {
1922 isServer : boolean ;
23+ bundlerName : CurrentBundler [ 'name' ] ;
2024} ) : SwcOptions => {
2125 return {
2226 env : {
23- targets : getBrowserslistQueries ( { isServer} ) ,
27+ targets : getBrowserslistQueries ( { isServer, bundlerName } ) ,
2428 } ,
2529 jsc : {
2630 parser : {
@@ -63,20 +67,53 @@ export function getSwcJsMinimizerOptions(): JsMinifyOptions {
6367 } ;
6468}
6569
70+ // TODO this is not accurate
71+ // for Rspack we should read from the built-in browserslist data
72+ // see https://github.com/facebook/docusaurus/pull/11496
73+ function getLastBrowserslistKnownNodeVersion (
74+ bundlerName : CurrentBundler [ 'name' ] ,
75+ ) : string {
76+ if ( bundlerName === 'rspack' ) {
77+ // TODO hardcoded value until Rspack exposes its Browserslist data
78+ // see https://github.com/facebook/docusaurus/pull/11496
79+ return '22.0.0' ;
80+ }
81+ // browserslist('last 1 node versions')[0]!.replace('node ', '')
82+ return browserslist . nodeVersions . at ( - 1 ) ! ;
83+ }
84+
85+ function getMinVersion ( v1 : string , v2 : string ) : string {
86+ return semver . lt ( v1 , v2 ) ? v1 : v2 ;
87+ }
88+
6689// We need this because of Rspack built-in LightningCSS integration
6790// See https://github.com/orgs/browserslist/discussions/846
6891export function getBrowserslistQueries ( {
6992 isServer,
93+ bundlerName,
7094} : {
7195 isServer : boolean ;
96+ bundlerName : CurrentBundler [ 'name' ] ;
7297} ) : string [ ] {
7398 if ( isServer ) {
74- return [ `node ${ process . versions . node } ` ] ;
99+ // Escape hatch env variable
100+ if ( process . env . DOCUSAURUS_SERVER_NODE_TARGET ) {
101+ return [ `node ${ process . env . DOCUSAURUS_SERVER_NODE_TARGET } ` ] ;
102+ }
103+ // For server builds, we want to use the current Node version as target
104+ // But we can't pass a target that Browserslist doesn't know about yet
105+ const nodeTarget = getMinVersion (
106+ process . versions . node ,
107+ getLastBrowserslistKnownNodeVersion ( bundlerName ) ,
108+ ) ;
109+
110+ return [ `node ${ nodeTarget } ` ] ;
75111 }
76112
77113 const queries = browserslist . loadConfig ( { path : process . cwd ( ) } ) ?? [
78114 ...browserslist . defaults ,
79115 ] ;
116+
80117 return queries ;
81118}
82119
0 commit comments