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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* 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 {afterEach, describe, expect, it, vi} from 'vitest';
import path from 'node:path';
import {rspack} from '@rspack/core';
import {createStyleLoadersFactory} from '../styleLoader';
import type {CurrentBundler} from '@docusaurus/types';

const currentBundler = {
name: 'rspack',
instance: rspack,
} as unknown as CurrentBundler;

async function getPresetEnvOptions() {
const getStyleLoaders = await createStyleLoadersFactory({currentBundler});
const postcssLoader = getStyleLoaders(false, {}).find(
(loader) =>
typeof loader === 'object' &&
loader.loader === require.resolve('postcss-loader'),
) as {options: {postcssOptions: {plugins: [string, unknown][]}}};
const [, presetEnvOptions] = postcssLoader.options.postcssOptions.plugins[0]!;
return presetEnvOptions;
}

describe('createStyleLoadersFactory', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('gives postcss-preset-env the default Browserslist queries', async () => {
vi.spyOn(process, 'cwd').mockReturnValue(__dirname);
await expect(getPresetEnvOptions()).resolves.toEqual({
browsers: ['baseline widely available'],
});
});

it("gives postcss-preset-env the site's Browserslist queries", async () => {
vi.spyOn(process, 'cwd').mockReturnValue(
path.join(__dirname, '__fixtures__/siteWithBrowserslistConfig'),
);
await expect(getPresetEnvOptions()).resolves.toEqual({
browsers: ['chrome 120'],
});
});
});
7 changes: 5 additions & 2 deletions packages/docusaurus-bundler/src/loaders/styleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import {getCSSExtractPlugin} from '../currentBundler';
import {getClientQueries} from '../browserslist';
import type {ConfigureWebpackUtils, CurrentBundler} from '@docusaurus/types';

export async function createStyleLoadersFactory({
Expand Down Expand Up @@ -72,8 +73,10 @@ export async function createStyleLoadersFactory({
[
require.resolve('postcss-preset-env'),
{
// Keeping this empty options object on purpose
// It could be more convenient for configurePostCss() usage
// Otherwise, preset-env ignores the Docusaurus default
// queries, and Autoprefixer reads the config of each CSS
// file's path
browsers: getClientQueries(),
},
],
],
Expand Down
Loading