npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-cra-loader

v0.3.0-0

Published

Transpile Create-React-App imports in Lerna projects.

Downloads

7

Readme

babel-loader-lerna-cra

Transpile Create-React-App imports in Lerna projects.

This package overrides the Webpack configuration of Create-React-App projects in a Lerna repo.

⚠️ Please Note

As with packages like React-App-Rewired...

Using babel-loader-lerna-cra breaks the "guarantees" that Create React App provides. That is to say, you now "own" the configs. No support will be provided. Proceed with caution.

"Stuff can break" — Dan Abramov https://twitter.com/dan_abramov/status/1045809734069170176

The Problem

Many people are trying to use Create-React-App in a Lerna repo with a project structure similar to this:

my-lerna-project/
├── lerna.json
├── package.json
└── packages
    ├── comp-a
    ├── comp-b
    ├── my-react-app-1
    │   └── comp-a
    └── my-react-app-2
        ├── comp-a
        └── comp-b

When running the React App, errors like these show up:

Error: You may need an appropriate loader to handle this file.
Failed to compile.

../comp-button/src/index.js
SyntaxError: .../monorepo-react/packages/comp-button/src/index.js: Unexpected token (4:4)

  2 |
  3 | const Button = ({ type = 'button', children, onClick }) => (
> 4 |     <div>
    |     ^
  5 |       <button type={type} className="button" onClick={onClick}>
  6 |         {children}
  7 |       </button>

These errors show up because the Webpack config in your Create-React-App does not look outside the React App's ./src directory for additional import dirs. In fact, how could it? It has no idea how you would configure your monorepo.

The Solution

This module (babel-loader-lerna-cra) allows you to configure Webpack config overrides in your Lerna project's package.json file; allowing babel to transpile imported Lerna packages using dev and prod.

Usage

  1. Install babel-loader-lerna-cra in your Lerna repo:

    npm i -D babel-loader-lerna-cra
  2. Configure the package.json in your Lerna root:

    {
        "name": "root",
        "private": true,
        "devDependencies": {
            "babel-learna-loader-cra": "*"
        },
        "babel-loader-lerna-cra": {
            "imports": "packages/comp-*/src",
            "apps":  "packages/*react-app*"
        }
    }
    • imports - glob pattern for imports that require transpiling
    • apps - glob pattern for app that need overriding
  3. Boostrap your React Apps with Webpack overrides:

    Note: you MUST complete step two first.

    npx babel-loader-lerna-cra

    You should see this output:

    babel-lerna-loader-cra: bootstraping...
    babel-lerna-loader-cra: config = {
      lernaRoot: '../monorepo-react',
      settings: {
        imports: 'packages/comp-*/src',
        apps: 'packages/*react-app*'
      },
      apps: [
        '../packages/my-react-app-1',
        '../packages/my-react-app-2'
      ],
      imports: [
        '../packages/comp-a/src',
        '../packages/comp-a/src',
        '../packages/comp-b/src'
        ]
    }
    babel-lerna-loader-cra: copying: my-react-app-1/... webpack.config.dev.js => backup.webpack.config.prod.js
    babel-lerna-loader-cra: copying: my-react-app-1/... webpack.config.replacement.js => webpack.config.dev.js
    babel-lerna-loader-cra: copying: my-react-app-1/... webpack.config.prod.js => backup.webpack.config.prod.js
    babel-lerna-loader-cra: copying: my-react-app-1/... webpack.config.replacement.js => webpack.config.prod.js
    babel-lerna-loader-cra: copying: my-react-app-2/... webpack.config.dev.js => backup.webpack.config.prod.js
    babel-lerna-loader-cra: copying: my-react-app-2/... webpack.config.replacement.js => webpack.config.dev.js
    babel-lerna-loader-cra: copying: my-react-app-2/... webpack.config.prod.js => backup.webpack.config.prod.js
    babel-lerna-loader-cra: copying: my-react-app-2/... webpack.config.replacement.js => webpack.config.prod.js

    Note: you will need to bootstrap again when:

    1. Installing packages in CI
    2. When a new create-react-app is added

Related issues

  • https://github.com/babel/babel-loader/issues/377
  • https://github.com/facebook/create-react-app/issues/4161
  • https://github.com/facebook/create-react-app/issues/1333