typedoc2md / typedoc-plugin-markdown

A plugin for TypeDoc that enables TypeScript API documentation to be generated in Markdown.
https://typedoc-plugin-markdown.org
MIT License
691 stars 171 forks source link

Docusaurus V3 throws errors due to missing Node polyfills #493

Closed strmer15 closed 8 months ago

strmer15 commented 8 months ago

When using docusaurus-plugin-typedoc 0.21.0 with Docusaurus v3, the start and build scripts throw a number of errors.

Initially, I got errors like Module not found: Error: Can't resolve 'path' , Module not found: Error: Can't resolve 'module', etc. Here's an example:

Module not found: Error: Can't resolve 'path' in '/home/cplummer/work/beethoven/node_modules/.pnpm/typedoc@0.25.2_typescript@5.0.4/node_modules/typedoc/dist/lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
    - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
    - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
    resolve.fallback: { "path": false }

I updated the webpack config in the plugin to use resolve.fallback and the NodePolyfillPlugin (https://github.com/Richienb/node-polyfill-webpack-plugin), but then got errors about process not being defined, so I added in the DefinePlugin for that. The last problem was that during the server build, I would get errors about __dirname not being defined either, so I added another entry in the DefinePlugin to set it to an empty string.

I also got some warnings about dynamic require / import statements that webpack couldn't get context on, so I added in some ContextReplacementPlugin instances to turn off those warnings. The warning was

[WARNING] {"moduleIdentifier":"/home/cplummer/work/beethoven/node_modules/.pnpm/source-map-loader@4.0.1_webpack@5.88.2/node_modules/source-map-loader/dist/cjs.js!/home/cplummer/work/beethoven/node_modules/.pnpm/typedoc@0.25.2_typescript@5.0.4/node_modules/typedoc/dist/lib/utils/options/readers/typedoc.js","moduleName":"../../../node_modules/.pnpm/typedoc@0.25.2_typescript@5.0.4/node_modules/typedoc/dist/lib/utils/options/readers/typedoc.js","loc":"95:40-53","message":"Critical dependency: the request of a dependency is an expression"}

I created my own plugin that fixed all of the various errors:

const webpack = require('webpack');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');

const typedocWebpackPlugin = () => {
  return {
    name: 'typedoc-webpack-plugin',
    configureWebpack() {
      return {
        resolve: {
          fallback: {
            child_process: false,
            fs: false,
            inspector: false,
            module: false,
            perf_hooks: false,
          },
        },
        plugins: [
          new webpack.DefinePlugin({
            __dirname: "''",
            'process.env': JSON.stringify(process.env),
          }),
          new NodePolyfillPlugin(),
          new webpack.ContextReplacementPlugin(/typedoc\/dist/),
          new webpack.ContextReplacementPlugin(/typescript\/lib/),
        ],
      };
    },
  };
};

module.exports = typedocWebpackPlugin;

I don't know if there's a better way to solve this, but that's what I came up with. It would be great if this plugin could fix these errors without needing to customize the webpack config.

Thanks for creating and maintaining this software! Please let me know if there's additional information that you need about this and I'd be happy to help.

tgreyuk commented 8 months ago

Im not actually able to reproduce this. Is this setup available on a public repo?

strmer15 commented 8 months ago

Im not actually able to reproduce this. Is this setup available on a public repo?

Hmmm... ok, thanks for looking into it. It's not available publicly but I'll see if I can create a simple reproduction or figure out why this is happening. It seems like it must be due to some typedoc import that we have in our code somewhere that the Docusaurus webpack config is picking up.

strmer15 commented 8 months ago

I dug into this some more and found that I was importing ReflectionKind from typedoc into some of the docusaurus components that were being used. Once I removed those imports the errors went away and thing worked correctly again, without the extra webpack config. Sorry for the false alarm!