Docz: Monorepo nested folders in packages does not work

Created on 27 Nov 2019  ยท  6Comments  ยท  Source: doczjs/docz

Question

I'm currently trying to migrate to 2.x and I have a monorepo using Lerna. I'm using Atomic Design System to organize my components, and I have the following folder structure:

root
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ packages
โ”‚ย ย  โ”œโ”€โ”€ atoms
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ Button
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ package.json
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ README.md
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ src
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ index.mdx
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ index.less
โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ index.tsx
โ”‚ย ย  โ”œโ”€โ”€โ”€โ””โ”€โ”€โ”€โ””โ”€โ”€โ”€โ””โ”€โ”€ index.spec.tsx
โ”‚ย ย  โ”œโ”€โ”€ molecules
โ”‚ย ย  โ”œโ”€โ”€ organisms
โ”‚ย ย  โ””โ”€โ”€ utilities
โ””โ”€โ”€ ...

The project is pretty large and very hard to migrate and find the source of the issues, so I started a fresh new project and I replicated the structure (I only added the folders atoms and utilities.

The Button package.json name value is button and just importing it as the example I get Can't resolve 'button' in '/Projects/docz-app-monorepo-separate-docs/packages/docs/.docz/.cache/caches/gatsby-plugin-mdx/mdx-scopes-dir'.

This I have tried:

  1. Changed import from import Button from 'button' to import Button from 'atoms/button'
  2. I found workspaces in the root package, I changed it as following:
from

"workspaces": {
    "packages": [
      "packages/*"
    ]
  },

to

"workspaces": {
    "packages": [
      "packages/**/"
    ]
  },
  1. I also tried adding atoms in the workspaces object:
from

"workspaces": {
    "packages": [
      "packages/*"
    ]
  },

to

"workspaces": {
    "packages": [
      "packages/atoms/"
    ]
  },

and this one as weel

"workspaces": {
    "packages": {
      "atoms": [
         "packages/**/*"
         // also with  "packages/atoms/**/*"
       ]
    }
  },
  1. I changed the folder and package.json name from alert to atom-alert and it does not work anymore..
not reproducible pending-user-response question stale

Most helpful comment

@giulianok could you provide a small repro it would be really helpful !

Also, did you install button from the package where you're using it ?

@deammer thanks for providing a repro ! This setup will indeed break docz because it's not a yarn or lerna workspace so webpack won't know where to look for dependencies. I submitted a PR that tells webpack to look in both documentation and source node_modules for dependencies.

Another way to solve it would be to use a lerna or yarn workspace that will hoist node_modules to the top and won't need special webpack configuration ๐Ÿ‘

All 6 comments

I'm having a similar issue with my monorepo. I get the errors below when I try to import a file from one directory to the other:

 ERROR #98123  WEBPACK

Generating SSR bundle failed

Can't resolve '../../../../../../../source/src/Button' in '/Users/maxime/Code/docz-monorepo/documentation/.docz/.cache/caches/gatsby-plugin-mdx/mdx-scopes-dir'

File: .cache/caches/gatsby-plugin-mdx/mdx-scopes-dir/049a239130db62be40d44f60627af4dd.js


 ERROR #98123  WEBPACK

Generating SSR bundle failed

Can't resolve '@emotion/core' in '/Users/maxime/Code/docz-monorepo/source/src'

File: ../../source/src/Button.jsx

Here's a barebones repo where I reproduced the problem. This used to work. cc @rakannimer

@giulianok could you provide a small repro it would be really helpful !

Also, did you install button from the package where you're using it ?

@deammer thanks for providing a repro ! This setup will indeed break docz because it's not a yarn or lerna workspace so webpack won't know where to look for dependencies. I submitted a PR that tells webpack to look in both documentation and source node_modules for dependencies.

Another way to solve it would be to use a lerna or yarn workspace that will hoist node_modules to the top and won't need special webpack configuration ๐Ÿ‘

@rakannimer thank you for the quick fix! Posting your code here for posterity in case I delete the test repo:

In gatsby-node.js:

const path = require("path");

exports.onCreateWebpackConfig = args => {
  const documentationNodeModules = path.resolve("../node_modules");
  const sourceNodeModules = path.resolve("../../source/node_modules");
  args.actions.setWebpackConfig({
    resolve: {
      modules: [documentationNodeModules, sourceNodeModules]
    }
  });
};

You're welcome @deammer, thanks for posting the code here ๐Ÿ‘

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Hi all, I seem to be having the same issue when trying to add Docz to an existing Lerna monorepo project: https://github.com/spartan-ui/spartan-ui/compare/feat/docs. The project also uses Typescript, and I've looked through the examples and tried to follow them as much as possible, but I've had no luck getting this error to go away.

$ docz dev

Building app
warn Configuring yargs through package.json is deprecated and will be removed in the next major release, please use the JS API instead.
success open and validate gatsby-configs - 0.575s
success load plugins - 0.228s
success onPreInit - 0.006s
success initialize cache - 0.006s
success copy gatsby files - 0.033s
success onPreBootstrap - 0.215s
success createSchemaCustomization - 0.006s
warn Configuring yargs through package.json is deprecated and will be removed in the next major release, please use the JS API instead.
โ ง source and transform nodes
โš   warning   No cache was found with your props definitions
โš   warning   We'll parse your components to get props from them
success source and transform nodes - 4.513s
success building schema - 0.239s
success createPages - 0.003s
success createPagesStatefully - 0.871s
success onPreExtractQueries - 0.042s
success update schema - 0.044s
success extract queries from components - 0.086s
success write out requires - 0.142s
success write out redirect data - 0.003s
success onPostBootstrap - 0.002s
โ €
info bootstrap finished - 8.527 s
โ €
success run queries - 0.053s - 21/21 396.09/s

 ERROR #98123  WEBPACK

Generating SSR bundle failed

Can't resolve '../../../../../packages/layout/src/Footer/Footer' in '/spartan-ui/.docz/.cache/caches/gatsby-plugin-mdx/mdx-scopes-dir'
Was this page helpful?
0 / 5 - 0 ratings

Related issues

capaj picture capaj  ยท  3Comments

mquandalle picture mquandalle  ยท  3Comments

w0wka91 picture w0wka91  ยท  3Comments

YardWill picture YardWill  ยท  3Comments

nicholasess picture nicholasess  ยท  3Comments