question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recipe for using with lerna & yarn workspaces?

See original GitHub issue

Was under the impression this worked out of the box. But my craco-app is failing to run components from other packages, ie. with same error as standard CRA. Failed to compile. Unexpected token as referenced https://github.com/facebook/create-react-app/issues/1333.

- /packages
    -/craco-app
    -/components

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
patricklafrancecommented, Dec 24, 2019

Hi @sebastienfi,

Lerna + Yarn Workspaces should work out of the box with CRACO.

Here’s the sample project I use to test the feature: https://github.com/patricklafrance/craco-lerna-and-yarn-workspace

Thank you,

Patrick

2reactions
pcmaffeycommented, Oct 31, 2019

In case anyone comes across this, here’s what I ended up doing, using a craco port of https://github.com/react-workspaces/create-react-app/tree/master/packages/react-scripts

// https://github.com/react-workspaces/create-react-app/blob/master/packages/react-scripts/config/yarn-workspaces.js
const yarnWorkspaces = require('./tools/yarn-workspaces')

module.exports = {
  webpack: {
    // get CRA to read from /packages
    // based off https://github.com/react-workspaces/create-react-app/blob/master/packages/react-scripts/config/webpack.config.js
    configure: (webpackConfig, { env, paths }) => {
      const workspacesConfig = yarnWorkspaces.init(paths)
      const dev = env === 'development'
      const prod = env === 'production'
      const workspacesMainFields = [workspacesConfig.packageEntry, 'main']
      const mainFields =
        dev && workspacesConfig.development
          ? workspacesMainFields
          : prod && workspacesConfig.production
          ? workspacesMainFields
          : undefined

      const node_modules = [workspacesConfig.root]
        .concat(workspacesConfig.paths)
        .map(p => p + '/node_modules')

      const modules = Array.from(webpackConfig.resolve.modules).concat(
        node_modules
      )
      // update node_modules paths
      webpackConfig.resolve.modules = modules
      webpackConfig.resolve.mainFields = mainFields

      const include =
        dev && workspacesConfig.development
          ? [paths.appSrc, workspacesConfig.paths]
          : prod && workspacesConfig.production
          ? [paths.appSrc, workspacesConfig.paths]
          : paths.appSrc
      // linter
      webpackConfig.module.rules[1].include = include
      // skip package node_modules from linter
      webpackConfig.module.rules[1].exclude = node_modules
      // loader
      webpackConfig.module.rules[2].oneOf[1].include = include

      return webpackConfig
    },
  },
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Recipe for using with lerna & yarn workspaces? #123 - GitHub
Lerna + Yarn Workspaces should work out of the box with CRACO. Here's the sample project I use to test the feature: https://github.com/ ......
Read more >
Managing monorepos with lerna and yarn workspaces
You can create a packages folder and start adding creating your packages there or use this command $ npx lerna create @projectName/packagename ....
Read more >
Managing monorepos with lerna and yarn workspaces - Medium
You can create a packages folder and start adding creating your packages there or use this command $ npx lerna create @projectName/packagename ....
Read more >
Monorepos: Yarn Workspaces and Lerna for beginners!
Let's discover how to handle monorepos with multiple packages using Yarn Workspaces and LernaJS. We're going to discuss Yarn Workspaces ...
Read more >
Sharing components using a monorepo built with Lerna and ...
Lerna and Yarn workspace are complementary tools to build monorepo. Lerna allows to execute npm scripts in package.json for all sub-projects.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found