Skip to content
Draft
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
1 change: 0 additions & 1 deletion packages/docusaurus-mdx-loader/src/createMDXLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export function createMDXLoaderRule({
return {
test: /\.mdx?$/i,
include,
with: {type: {not: 'text'}},
use: [createMDXLoaderItem(options)],
};
}
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-ideal-image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export default function pluginIdealImage(
const index = parentRules.indexOf(existingImageRule);
parentRules[index] = {
test: existingImageRule.test,
with: {type: {not: 'text'}},
oneOf: [idealImageRule, existingImageRule],
};
},
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-svgr/src/svgrLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ export function enhanceConfig(config: Configuration, params: Params): void {

const newSvgRule: RuleSetRule = {
test: /\.svg$/i,
with: {type: {not: 'text'}},
oneOf: [
{
use: [createSVGRRule(params)],
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-utils/src/webpackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ function createAssetRuleUtils({isServer}: {isServer: boolean}): AssetRuleUtils {

const rules: AssetRuleUtils['rules'] = {
assets: () => ({
with: {type: {not: 'text'}},
oneOf: [
// Requests created with createAssetRequest() come first on purpose
// They must not be handled by other asset rules (ideal-image, SVGR...)
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus/src/server/plugins/synthetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ export async function createMDXFallbackPlugin({
{
test: /\.mdx?$/i,
exclude: getMDXFallbackExcludedPaths(),
with: {type: {not: 'text'}},
use: [mdxLoaderItem],
},
],
Expand Down
37 changes: 14 additions & 23 deletions packages/docusaurus/src/webpack/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import _ from 'lodash';
import {posixPath} from '@docusaurus/utils';
import {getCurrentBundler} from '@docusaurus/bundler';
import {excludeJS, clientDir, createBaseConfig} from '../base';
import {BundlerNames, createTestConfigureWebpackUtils} from './testUtils';
import {createTestConfigureWebpackUtils} from './testUtils';
import {DEFAULT_FUTURE_CONFIG} from '../../server/configValidation';
import type {CurrentBundler, Props, SiteMetadata} from '@docusaurus/types';

Expand Down Expand Up @@ -117,28 +117,19 @@ describe('base webpack config', () => {
};
}

// Rspack and webpack have a default rule giving the 'asset/source' type to
// modules imported with `with {type: 'text'}`, but the loaders of all other
// matching rules still apply (JS transpilation, MDX compilation, SVGR...)
// Our rules must exclude text imports to return the raw file content
// See https://rspack.rs/config/module-rules#ruleswith
it.each(BundlerNames)(
'excludes text import attributes from all core rules - %s',
async (bundlerName) => {
const config = await createBaseConfig({
props: await createProps(bundlerName),
isServer: false,
minify: true,
configureWebpackUtils:
await createTestConfigureWebpackUtils(bundlerName),
});
const rules = config.module?.rules ?? [];
expect(rules.length).toBeGreaterThan(0);
rules.forEach((rule) => {
expect(rule).toMatchObject({with: {type: {not: 'text'}}});
});
},
);
// The website dogfood tests check that text imports skip all loaders
it('makes text imports skip all subsequent rules - rspack', async () => {
const config = await createBaseConfig({
props: await createProps('rspack'),
isServer: false,
minify: true,
configureWebpackUtils: await createTestConfigureWebpackUtils('rspack'),
});
expect(config.module?.rules?.[0]).toEqual({
with: {type: 'text'},
as: '[resource-path].text',
});
});

it('creates webpack aliases', async () => {
const aliases = ((
Expand Down
26 changes: 20 additions & 6 deletions packages/docusaurus/src/webpack/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,27 @@ import {realpath} from '@docusaurus/fs';
import {loadDocusaurusAliases, loadThemeAliases} from './aliases';
import {BundlerCPUProfilerPlugin} from './plugins/BundlerCPUProfilerPlugin';
import type {Configuration, RuleSetRule} from 'webpack';
import type {Configuration as RspackConfiguration} from '@rspack/core';
import type {
Configuration as RspackConfiguration,
RuleSetRule as RspackRuleSetRule,
} from '@rspack/core';
import type {ConfigureWebpackUtils, Props} from '@docusaurus/types';

const CSS_REGEX = /\.css$/i;
const CSS_MODULE_REGEX = /\.module\.css$/i;

// Text imports get the 'asset/source' type from a default rule, but the
// loaders of all the other matching rules would still apply (JS, MDX, SVGR...)
// Matching the subsequent rules against `file.js.text` skips them all
// See https://rspack.rs/config/module-rules#rulesas
function createTextImportRule(): RuleSetRule {
// Docusaurus bundler configs are typed with Webpack types
return {
with: {type: 'text'},
as: '[resource-path].text',
} satisfies RspackRuleSetRule as RuleSetRule;
}

export const clientDir = path.join(__dirname, '..', 'client');

// The RealContentHashPlugin replaces every occurrence of a provisional chunk
Expand Down Expand Up @@ -179,8 +195,6 @@ export async function createBaseConfig({
};
}

const notTextImport: RuleSetRule['with'] = {type: {not: 'text'}};

return {
mode,
name,
Expand Down Expand Up @@ -290,17 +304,18 @@ export async function createBaseConfig({
},
},
rules: [
...(props.currentBundler.name === 'rspack'
? [createTextImportRule()]
: []),
assetRuleUtils.rules.assets(),
{
test: /\.[jt]sx?$/i,
exclude: excludeJS,
with: notTextImport,
use: [createJsLoader({isServer})],
},
{
test: CSS_REGEX,
exclude: CSS_MODULE_REGEX,
with: notTextImport,
use: configureWebpackUtils.getStyleLoaders(isServer, {
importLoaders: 1,
sourceMap: !isProd,
Expand All @@ -310,7 +325,6 @@ export async function createBaseConfig({
// using the extension .module.css
{
test: CSS_MODULE_REGEX,
with: notTextImport,
use: configureWebpackUtils.getStyleLoaders(isServer, {
modules: {
// Using the same CSS Module class pattern in dev/prod on purpose
Expand Down
Loading
Loading