Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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 {describe, expect, it} from 'vitest';
import {createDevServerConfig} from '../webpack';
import {DevHtmlFilename} from '../../../webpack/plugins/DevHtmlPlugin';
import type {Props} from '@docusaurus/types';

describe('createDevServerConfig', () => {
function createTestProps(baseUrl: string = '/'): Props {
return {
baseUrl,
siteDir: '/site',
siteConfig: {
staticDirectories: [],
future: {
experimental_router: 'browser',
},
},
} as unknown as Props;
}

it('configures historyApiFallback rewrite with DevHtmlFilename for root baseUrl', () => {
const config = createDevServerConfig({
props: createTestProps('/'),
cliOptions: {},
host: 'localhost',
port: 3000,
httpsConfig: false,
});

expect(config.historyApiFallback).toEqual({
rewrites: [{from: /\/*/, to: `/${DevHtmlFilename}`}],
});
});

it('configures historyApiFallback rewrite with DevHtmlFilename for subpath baseUrl', () => {
const config = createDevServerConfig({
props: createTestProps('/subpath/'),
cliOptions: {},
host: 'localhost',
port: 3000,
httpsConfig: false,
});

expect(config.historyApiFallback).toEqual({
rewrites: [{from: /\/*/, to: `/subpath/${DevHtmlFilename}`}],
});
});
});
5 changes: 3 additions & 2 deletions packages/docusaurus/src/commands/start/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
executePluginsConfigureWebpack,
} from '../../webpack/configure';
import {createStartClientConfig} from '../../webpack/client';
import {DevHtmlFilename} from '../../webpack/plugins/DevHtmlPlugin';
import type {StartCLIOptions} from './start';
import type {
ConfigureWebpackUtils,
Expand Down Expand Up @@ -54,7 +55,7 @@ function registerWebpackE2ETestHook(compiler: Compiler) {
});
}

function createDevServerConfig({
export function createDevServerConfig({
cliOptions,
props,
host,
Expand Down Expand Up @@ -116,7 +117,7 @@ function createDevServerConfig({
: 'https',
}),
historyApiFallback: {
rewrites: [{from: /\/*/, to: baseUrl}],
rewrites: [{from: /\/*/, to: path.posix.join(baseUrl, DevHtmlFilename)}],
},
allowedHosts: 'all',
host,
Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus/src/webpack/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ export async function createStartClientConfig({
level: 'warn',
},
plugins: [
// Generates an `index.html` file with the <script> injected.
// Generates the dev HTML shell with the <script> injected.
new DevHtmlPlugin({
filename: 'index.html',
render: (assets) =>
renderDevHtml({
...assets,
Expand Down
13 changes: 10 additions & 3 deletions packages/docusaurus/src/webpack/plugins/DevHtmlPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import type {Compilation, Compiler} from 'webpack';

const PluginName = 'Docusaurus-DevHtmlPlugin';

// Internal dev HTML shell filename. Avoids colliding with user's static/index.html
// See https://github.com/facebook/docusaurus/issues/10198
export const DevHtmlFilename = '__docusaurus.html';

export type DevHtmlAssets = {
// Public URLs of the entrypoint JS files, in load order
scripts: string[];
Expand All @@ -17,7 +21,7 @@ export type DevHtmlAssets = {
};

export type DevHtmlPluginOptions = {
filename: string;
filename?: string;
render: (assets: DevHtmlAssets) => string;
};

Expand Down Expand Up @@ -75,10 +79,13 @@ function getEntrypointAssets(compilation: Compilation): DevHtmlAssets {
* and this works the same way with Webpack and Rspack.
*/
export default class DevHtmlPlugin {
readonly options: DevHtmlPluginOptions;
readonly options: Required<DevHtmlPluginOptions>;

constructor(options: DevHtmlPluginOptions) {
this.options = options;
this.options = {
filename: DevHtmlFilename,
...options,
};
}

apply(compiler: Compiler): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
* LICENSE file in the root directory of this source tree.
*/

import {describe, expect, it} from 'vitest';
import {getAssetUrls} from '../DevHtmlPlugin';
import {afterEach, describe, expect, it} from 'vitest';
import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs/promises';
import {getCurrentBundler} from '@docusaurus/bundler';
import {outputFile} from '@docusaurus/fs';
import DevHtmlPlugin, {DevHtmlFilename, getAssetUrls} from '../DevHtmlPlugin';
import {createStaticDirectoriesCopyPlugin} from '../StaticDirectoriesCopyPlugin';
import type webpack from 'webpack';
import type {CurrentBundler, Props} from '@docusaurus/types';

describe('getAssetUrls', () => {
const files = ['runtime~main.js', 'main.js', 'styles.css', 'main.js.map'];
Expand Down Expand Up @@ -43,3 +51,91 @@ describe('getAssetUrls', () => {
).toEqual({scripts: ['/a%20b/main.mjs?v=1'], stylesheets: ['/x.css?v=2']});
});
});

describe('DevHtmlPlugin', () => {
it('defaults filename to DevHtmlFilename', () => {
const plugin = new DevHtmlPlugin({
render: () => '<html></html>',
});
expect(plugin.options.filename).toBe(DevHtmlFilename);
expect(DevHtmlFilename).toBe('__docusaurus.html');
});

it('allows custom filename', () => {
const plugin = new DevHtmlPlugin({
filename: 'custom.html',
render: () => '<html></html>',
});
expect(plugin.options.filename).toBe('custom.html');
});
});

const tempDirs: string[] = [];

async function createProps(bundlerName: CurrentBundler['name']) {
const siteDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'docusaurus-dev-html-'),
);
tempDirs.push(siteDir);
const currentBundler = await getCurrentBundler({
siteConfig: {webpack: bundlerName === 'webpack' ? {} : undefined},
});
return {
siteDir,
outDir: path.join(siteDir, 'build'),
siteConfig: {staticDirectories: ['static']},
currentBundler,
} as Props;
}

describe.each(['webpack', 'rspack'] as const)(
'%s dev html with static index.html',
(name) => {
afterEach(async () => {
await Promise.all(
tempDirs
.splice(0)
.map((dir) => fs.rm(dir, {recursive: true, force: true})),
);
});

it('emits dev html without colliding with static/index.html', async () => {
const props = await createProps(name);
await outputFile(
path.join(props.siteDir, 'static/index.html'),
'<h1>Static Index</h1>',
);
const copyPlugin = await createStaticDirectoriesCopyPlugin({props});
const devHtmlPlugin = new DevHtmlPlugin({
render: () => '<html>Dev Shell</html>',
});
const compiler = props.currentBundler.instance({
mode: 'development',
context: props.siteDir,
entry: {},
output: {path: props.outDir},
plugins: [copyPlugin!, devHtmlPlugin],
});
let stats: webpack.Stats;
try {
stats = await new Promise((resolve, reject) => {
compiler.run((error, result) =>
error ? reject(error) : resolve(result!),
);
});
} finally {
await new Promise<void>((resolve, reject) => {
compiler.close((error) => (error ? reject(error) : resolve()));
});
}
expect(stats.hasErrors()).toBe(false);
expect(stats.hasWarnings()).toBe(false);
await expect(
fs.readFile(path.join(props.outDir, 'index.html'), 'utf8'),
).resolves.toBe('<h1>Static Index</h1>');
await expect(
fs.readFile(path.join(props.outDir, DevHtmlFilename), 'utf8'),
).resolves.toBe('<html>Dev Shell</html>');
});
},
);