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
5 changes: 5 additions & 0 deletions packages/docusaurus-faster/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export const getSwcLoaderOptions = ({
runtime: 'automatic',
},
},
experimental: {
// Required to match import attributes in rules, see
// https://rspack.rs/config/module-rules#ruleswith
keepImportAttributes: true,
},
},
};
};
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-mdx-loader/src/createMDXLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function createMDXLoaderRule({
return {
test: /\.mdx?$/i,
include,
with: {type: {not: 'text'}},
use: [createMDXLoaderItem(options)],
};
}
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-ideal-image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default function pluginIdealImage(
rules: [
{
test: /\.(?:png|jpe?g)$/i,
with: {type: {not: 'text'}},
// We don't want to use the image loader for non-React source code
// ie we don't want to use ideal image loader for CSS files...
// See https://github.com/facebook/docusaurus/issues/10862
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-svgr/src/svgrLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function enhanceConfig(config: Configuration, params: Params): void {

const newSvgRule: RuleSetRule = {
test: /\.svg$/i,
with: {type: {not: 'text'}},
oneOf: [
{
use: [createSVGRRule(params)],
Expand Down
7 changes: 7 additions & 0 deletions packages/docusaurus-utils/src/webpackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ function createFileLoaderUtils({
}!`,
};

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

const rules: FileLoaderUtils['rules'] = {
/**
* Loads image assets, inlines images via a data URI if they are below
Expand All @@ -117,6 +119,7 @@ function createFileLoaderUtils({
images: () => ({
use: [loaders.url({folder: 'images'})],
test: /\.(?:ico|jpe?g|png|gif|webp|avif)(?:\?.*)?$/i,
with: notTextImport,
}),

/**
Expand All @@ -126,11 +129,13 @@ function createFileLoaderUtils({
svgs: () => ({
use: [loaders.url({folder: 'images'})],
test: /\.svg$/i,
with: notTextImport,
}),

fonts: () => ({
use: [loaders.url({folder: 'fonts'})],
test: /\.(?:woff2?|eot|ttf|otf)$/i,
with: notTextImport,
}),

/**
Expand All @@ -140,11 +145,13 @@ function createFileLoaderUtils({
media: () => ({
use: [loaders.url({folder: 'medias'})],
test: /\.(?:mp4|avi|mov|mkv|mpg|mpeg|vob|wmv|m4v|webm|ogv|wav|mp3|m4a|aac|oga|flac)$/i,
with: notTextImport,
}),

otherAssets: () => ({
use: [loaders.file({folder: 'files'})],
test: /\.(?:pdf|docx?|xlsx?|zip|rar)$/i,
with: notTextImport,
}),
};

Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus/src/server/plugins/synthetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export async function createMDXFallbackPlugin({
{
test: /\.mdx?$/i,
exclude: getMDXFallbackExcludedPaths(),
with: {type: {not: 'text'}},
use: [mdxLoaderItem],
},
],
Expand Down
20 changes: 20 additions & 0 deletions packages/docusaurus/src/webpack/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,26 @@ describe('base webpack config', () => {
],
} as Props;

// 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('excludes text import attributes from all core rules', async () => {
const config = await createBaseConfig({
props,
isServer: false,
minify: true,
faster: DEFAULT_FASTER_CONFIG,
configureWebpackUtils: await createTestConfigureWebpackUtils(),
});
const rules = config.module?.rules ?? [];
expect(rules.length).toBeGreaterThan(0);
rules.forEach((rule) => {
expect(rule).toMatchObject({with: {type: {not: 'text'}}});
});
});

it('creates webpack aliases', async () => {
const aliases = ((
await createBaseConfig({
Expand Down
7 changes: 6 additions & 1 deletion packages/docusaurus/src/webpack/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import {getFileLoaderUtils, md5Hash} from '@docusaurus/utils';
import {loadDocusaurusAliases, loadThemeAliases} from './aliases';
import {BundlerCPUProfilerPlugin} from './plugins/BundlerCPUProfilerPlugin';
import type {Configuration} from 'webpack';
import type {Configuration, RuleSetRule} from 'webpack';
import type {
ConfigureWebpackUtils,
FasterConfig,
Expand Down Expand Up @@ -154,6 +154,8 @@ export async function createBaseConfig({
};
}

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

return {
mode,
name,
Expand Down Expand Up @@ -261,6 +263,7 @@ export async function createBaseConfig({
{
test: /\.[jt]sx?$/i,
exclude: excludeJS,
with: notTextImport,
use: [
createJsLoader({
isServer,
Expand All @@ -271,6 +274,7 @@ export async function createBaseConfig({
{
test: CSS_REGEX,
exclude: CSS_MODULE_REGEX,
with: notTextImport,
use: configureWebpackUtils.getStyleLoaders(isServer, {
importLoaders: 1,
sourceMap: !isProd,
Expand All @@ -280,6 +284,7 @@ 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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* 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 React from 'react';
import Heading from '@theme/Heading';
import TextImportTest from './_TextImportTest';

import jsonData from './_fixtures/json-fixture.json' with {type: 'json'};

import jsText from './_fixtures/js-fixture.js' with {type: 'text'};
import tsText from './_fixtures/ts-fixture.ts' with {type: 'text'};
import tsxText from './_fixtures/tsx-fixture.tsx' with {type: 'text'};
import jsonText from './_fixtures/json-fixture.json' with {type: 'text'};
import svgText from './_fixtures/svg-fixture.svg' with {type: 'text'};
import mdText from './_fixtures/md-fixture.md' with {type: 'text'};
import mdxText from './_fixtures/mdx-fixture.mdx' with {type: 'text'};
import yamlText from './_fixtures/yaml-fixture.yaml' with {type: 'text'};
import txtText from './_fixtures/txt-fixture.txt' with {type: 'text'};
import cssModuleText from './_fixtures/css-module-fixture.module.css' with {type: 'text'};
import cssText from './_fixtures/css-fixture.css' with {type: 'text'};

export default function JsxTests() {
return (
<>
<Heading as="h3">JSON import</Heading>
<pre>{JSON.stringify(jsonData)}</pre>
<TextImportTest
name="JS"
language="jsx"
value={jsText}
expected="<div>JS fixture</div>"
/>
<TextImportTest
name="TS"
language="ts"
value={tsText}
expected="tsFixture: string"
/>
<TextImportTest
name="TSX"
language="tsx"
value={tsxText}
expected="{name}: {name: string}"
/>
<TextImportTest
name="JSON"
language="json"
value={jsonText}
expected='"fixture": "JSON fixture"'
/>
<TextImportTest
name="CSS"
language="css"
value={cssText}
expected=".cssFixture {"
/>
<TextImportTest
name="CSS Module"
language="css"
value={cssModuleText}
expected=".cssModuleFixture {"
/>
<TextImportTest
name="SVG"
language="xml"
value={svgText}
expected="<svg xmlns"
/>
<TextImportTest
name="Markdown"
language="md"
value={mdText}
expected="Some **Markdown** text."
/>
<TextImportTest
name="MDX"
language="mdx"
value={mdxText}
expected='<MdxFixture name="world" />'
/>
<TextImportTest
name="YAML"
language="yaml"
value={yamlText}
expected="fixture: YAML fixture"
/>
<TextImportTest
name="Text"
language="text"
value={txtText}
expected="Text fixture"
/>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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 React from 'react';
import CodeBlock from '@theme/CodeBlock';
import Heading from '@theme/Heading';

// Asserts that a module imported with `with {type: 'text'}`
// is the raw file content, not processed by any loader
// Throws on purpose: a regression must fail the website build
export default function TextImportTest({
name,
value,
expected,
language,
}: {
name: string;
// Typed as unknown: TypeScript doesn't type text imports as strings
value: unknown;
// A raw source snippet that loaders would transform or remove
expected: string;
language: string;
}): React.JSX.Element {
if (typeof value !== 'string' || !value.includes(expected)) {
throw new Error(
`Text import attribute test failed for ${name}: expected a string containing ${JSON.stringify(expected)}, got ${typeof value}:\n${String(value)}`,
);
}
return (
<section>
<Heading as="h3">✅ {name}</Heading>
<CodeBlock language={language}>{value}</CodeBlock>
</section>
);
}
Loading
Loading