Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin API: Type definition of configureWebpack does allow all possible keys #9654

Open
7 tasks done
jarhodes314 opened this issue Dec 21, 2023 · 1 comment
Open
7 tasks done
Labels
bug An error in the Docusaurus core causing instability or issues with its execution

Comments

@jarhodes314
Copy link

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

Description

The type definition for configureWebpack in the plugins API currently uses the Configuration type from webpack:

configureWebpack?: (
config: WebpackConfiguration,
isServer: boolean,
utils: ConfigureWebpackUtils,
content: Content,
) => WebpackConfiguration & {

When I return an object containing a devServer key, this works as expected, but I get a type error because the Configuration type doesn't contain a key called devServer.

As far as I can tell, from some very uninformed searching, I think the type that this should actually be is https://github.com/webpack/webpack/blob/main/declarations/WebpackOptions.d.ts#L797.

Reproducible demo

https://codesandbox.io/p/sandbox/docusaurus-webpack-devserver-tqk8nr

Steps to reproduce

  1. Open docusaurus.config.ts
  2. Uncomment either of the keys under plugins
  3. Observe the type error against redirectPlugin

Expected behavior

I expect docusaurus.config.ts to type check successfully when devServer is configured

Actual behavior

I get a type error in docusaurus.config.ts when devServer is configured, yet it definitely behaves as I expect

Your environment

No response

Self-service

  • I'd be willing to fix this bug myself.
@jarhodes314 jarhodes314 added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Dec 21, 2023
@jarhodes314 jarhodes314 changed the title Type definition of configureWebpack does allow all possible keys Plugin API: Type definition of configureWebpack does allow all possible keys Dec 21, 2023
@slorber slorber removed the status: needs triage This issue has not been triaged by maintainers label Dec 21, 2023
@slorber
Copy link
Collaborator

slorber commented Dec 21, 2023

Hmm, there's something weird in our setup, it seems Webpack now provdies its own type defs now but we keep using @types/webpack external defs.

Anyway, if you use "types": ["@types/webpack-dev-server"] in tsconfig.json it should fix your problem because those types add the required attribute through declaration merging:

declare module 'webpack' {
    interface Configuration {
        /**
         * Can be used to configure the behaviour of webpack-dev-server when
         * the webpack config is passed to webpack-dev-server CLI.
         */
        devServer?: WebpackDevServer.Configuration | undefined;
    }
}

Tested locally on our site and it works 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution
Projects
None yet
Development

No branches or pull requests

2 participants