docusaurus: Minify failure when building docusaurus site in a yarn workspaces with another project

Edit from @slorber: TL.DR, try using this nohoist config

🐛 Bug Report

I have a Docusaurus-built site that shares a yarn.lock file with several other projects. Since bumping to Docusaurus to 2.0.0-alpha.64 from 2.0.0-alpha.63, I have been unable to docusaurus build. I get this error:

(undefined) TypeError: Cannot read property 'replace' of undefined
    at Object.options.minifyJS (main:18614:28)
    at Object.chars (main:19102:24)
    at main:48074:19
    at String.replace (<anonymous>)
    at new HTMLParser (main:48066:19)
    at minify (main:18888:3)
    at module.exports.exports.minify (main:19249:16)
    at serverEntry_render (main:87861:38)
Error: Failed to compile with errors.
    at /Users/treece/src/docusaurus-minify-troubles/node_modules/@docusaurus/core/lib/webpack/utils.js:164:24
    at finalCallback (/Users/treece/src/docusaurus-minify-troubles/node_modules/webpack/lib/MultiCompiler.js:254:12)
    at /Users/treece/src/docusaurus-minify-troubles/node_modules/webpack/lib/MultiCompiler.js:277:6
    at done (/Users/treece/src/docusaurus-minify-troubles/node_modules/neo-async/async.js:2931:13)
    at runCompilers (/Users/treece/src/docusaurus-minify-troubles/node_modules/webpack/lib/MultiCompiler.js:181:48)
    at /Users/treece/src/docusaurus-minify-troubles/node_modules/webpack/lib/MultiCompiler.js:188:7
    at /Users/treece/src/docusaurus-minify-troubles/node_modules/webpack/lib/MultiCompiler.js:270:7
    at finalCallback (/Users/treece/src/docusaurus-minify-troubles/node_modules/webpack/lib/Compiler.js:257:39)
    at /Users/treece/src/docusaurus-minify-troubles/node_modules/webpack/lib/Compiler.js:273:13
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/treece/src/docusaurus-minify-troubles/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:40:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/treece/src/docusaurus-minify-troubles/node_modules/tapable/lib/Hook.js:154:20)
    at onCompiled (/Users/treece/src/docusaurus-minify-troubles/node_modules/webpack/lib/Compiler.js:271:21)
    at /Users/treece/src/docusaurus-minify-troubles/node_modules/webpack/lib/Compiler.js:681:15
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/treece/src/docusaurus-minify-troubles/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:4:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/treece/src/docusaurus-minify-troubles/node_modules/tapable/lib/Hook.js:154:20)
    at /Users/treece/src/docusaurus-minify-troubles/node_modules/webpack/lib/Compiler.js:678:31
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Have you read the Contributing Guidelines on issues?

Yes

To Reproduce

I’ve created a minimal repro repo, https://github.com/taylorreece/docusaurus-minify-troubles, with a new Docusaurus project created with npx @docusaurus/init@next init docs classic. I did not touch the docusaurus project otherwise. The other project in this repo, aptly named “other-project” has a package.json file with a series of dependencies. These two projects share a yarn.lock.

  1. git clone git@github.com:taylorreece/docusaurus-minify-troubles.git
  2. cd docusaurus-minify-troubles
  3. yarn install
  4. cd docs/
  5. yarn build

If you remove other-project and then yarn install and yarn build, the Docusaurus site builds fine.

Expected behavior

I’d expect it to build.

Actual Behavior

I get the stack trace listed above

Your Environment

MacOS Node v12.16.2 Yarn v1.22.4

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 46 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I’ve fixed like this

resolutions: { “terser” : “4.8.0” }

I was able to make the repro repo build with the following:

  "workspaces": {
    "packages": [
      "docs",
      "other-project"
    ],
    "nohoist": [
      "**/html-minifier-terser",
    ]
  },

Note: I’m not sure the “nohoist” config works with just yarn install, had to regenerate the whole lockfile to make it work.

Please let me know how this workaround works for you.

I’m going to close this issue because I’m not sure we can do anything about it currently, really looks related to how Yarn install dependencies. We’ll upgrade html-minifier-terser as soon as it upgrades to Terser v5 so that this becomes a non-problem and we only use a single version of Terser.

Temporary workaround: if you nohoist the docusaurus project, and remove all node_modules directories and run yarn install again, the problem goes away. You do get duplicated directories in dependencies, as all of the docusaurus project’s dependencies are no longer shared, but that’s better than builds failing 🤷

{
  "name": "my-shared-project",
  "private": true,
  "workspaces": {
    "packages": [
      "docs",
      "other-project"
    ],
    "nohoist": [
      "docs/**"
    ]
  }
}

The latest Docusaurus betas shouldn’t have this problem anymore, no internal deps are using Terser 4 now

My deps tree using @docusaurus/core@^2.0.0-alpha.66:

@docusaurus/core@^2.0.0-alpha.66:
    html-minifier-terser "^5.0.5"
    html-webpack-plugin "^4.0.4"
    terser-webpack-plugin "^4.1.0"
    webpack "^4.44.1"

html-minifier-terser@^5.0.1, html-minifier-terser@^5.0.5:
    terser "^4.6.3"

html-webpack-plugin@^4.0.4:
    html-minifier-terser "^5.0.1"

terser-webpack-plugin@^1.4.3:
    terser "^4.1.2"

terser-webpack-plugin@^4.1.0:
    terser "^5.3.4"

webpack@^4.44.1:
    terser-webpack-plugin "^1.4.3"

Which gets resolved by Yarn into node_modules structure:

@docusaurus
    /core@2.0.0-alpha.66
        /terser@5.3.7
        /terser-webpack-plugin@4.2.3
html-minifier-terser@5.1.1
    <none>
html-webpack-plugin@4.5.0
    <none>
terser@4.8.0
    <none>
terser-webpack-plugin@1.4.5
    <none>
webpack@4.44.2
    <none>

As it should be.

The only user of terser@5 is Docusaurus, which is also a user of html-minifier-terser and html-webpack-plugin. Both of which use older terser@4. Looks like Docusaurus gives a config in terser@5 format to those plugins as well, which fails with an error.

Adding Yarn resolution to lower terser-webpack-plugin temporary solves the issue:

{
  "resolutions": {
    "**/terser-webpack-plugin": "^1.4.3"
  }
}

@slorber great sleuthing 😃 thanks so much…will hope to see the dependencies catch up

This is a pretty bad bug. I’m surprised the Docusaurus team has not run in to it, since they are also using a yarn workspace structure 🤔. I am also working in a monorepo, with a custom theme under /packages/ and my test site under /sites/, and the test site wouldn’t build after bumping up from alpha.63. I finally managed to get a nohoist config that seems to work but this feels like a very hacky solution, that only happens to work right now because html-minifier-terser is a first-level dependency

{
  "workspaces": {
    "packages": [
      "packages/*",
      "sites/*"
    ],
    "nohoist": [
      "*/@docusaurus/*"
    ]
  }
}