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.

Webpack alias support (or similar)

See original GitHub issue

Is this a bug report?

No

I’ll be using SBAR for this issue. Hopefully that helps put things into context.

Situation

My team has a few separate npm packages that we import into our applications made with create-react-app. These packages are shared between other applications that also use either create-react-app or the react-native-cli.

Recently, it has become increasingly challenging to use linked packages in create-react-app that are not yet published to npm.

Background

Historically, when we’ve wanted to dev or test these packages inside a CRA application, we would simply npm link it into that CRA application.

We can no longer do this (with ease) because of two things in React:

  1. hooks
  2. context

React isn’t the culprit here but the features above do cause some challenging issues when it comes to singletons.

Assessment

With hooks, React requires the same instance of React to be available in both the linked package and the CRA application.

When you npm link a package into a CRA application, what happens is the npm linked package will resolve to a different instance of React than the instance inside the CRA application causing an error. @JennieJi 's post in https://github.com/facebook/react/issues/13991 explains this perfectly.

With context, the convention is to create the context as a singleton and export it somewhere inside the package being developed. This causes issues because it’s possible that the context instance inside the CRA application is different from the context instance from the package causing components or hooks that use the context to not find it.

Recommendations

First off, I would recommend not ignoring/closing this issue 😅 😆.

Even if you consider linking package within CRA as advanced usage, I believe it’s not out of the scope of CRA’s mission. There are a lot of users having issues linking react packages and it would be amazing if CRA had a solution for this problem (even if it’s not the solution below).

I think a good and simple solution would be to allow the users of CRA to have access to the resolve.alias configuration of webpack (or a similar, restricted variant). (relevant post)

Package authors can alias their packages to the exact place on their machine and then CRA could handle compiling the the package and all the appropriate dependencies would resolve nicely.

Let me know if this is a feasible feature to add. I’d also be willing to work on a PR upon your blessing. 🙏 😆

Thank you for your time!

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
dwjohnstoncommented, May 28, 2019

I’m resolving this issue by using react-app-rewired and customize-cra.

Agree that it would be nice for CRA to support this.

Here is my config-overrides.js :

const {
    override,
    addWebpackAlias,
  } = require("customize-cra");

const path = require('path'); 

module.exports = override( 
    addWebpackAlias({
        react: path.resolve('./node_modules/react')
    })
)

Example project: https://github.com/dwjohnston/material-ui-hooks-issue/tree/master

3reactions
j-lee8commented, Nov 6, 2019

Any update on this? We’ve just gone through a restructuring exercise and would really like to use aliases to target components, utils etc without using relative paths!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolve
Configure how modules are resolved. For example, when calling import 'lodash' in ES2015, the resolve options can change where webpack goes to look...
Read more >
How to Create a Path Alias in Webpack
Path aliases streamline your development process! In this post: how to use Webpack to implement path aliases with TypeScript and Storybook.
Read more >
Webpack Aliases Keep My Code Sane
Aliases are a way to let webpack know where to find our code by providing a word or character that represents a partial...
Read more >
The Right Usage of Aliases in Webpack and TypeScript
Today I want to share some tips with you to improve your projects and make it much easier to maintain. I'm talking about...
Read more >
add support for webpack alias #20 - smeijer/unimported
I think we can add support for webpack, but I'm not sure what to do with babel. Babel configs are highly dynamic. An...
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