GithubHelp home page GithubHelp logo

nguyenphuquang / lerna-webpack-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dan-kez/lerna-webpack-example

0.0 0.0 0.0 1.94 MB

An example monorepo implementing lerna, webpack, codesplitting, and eslint

Home Page: https://dan-kez.github.io/lerna-webpack-example/

License: MIT License

JavaScript 62.63% HTML 2.07% TypeScript 35.30%

lerna-webpack-example's Introduction

lerna-webpack-example

DEMO

This repository provides an example configuration for a monorepo using lerna. There are a few features baked into this relatively simple app.

  1. Monorepo code splitting via lerna

  2. Babel building across packages.

  3. A package with a webpack config that has dependencies on packages in the monorepo.

  4. Code splitting and asynchronous loading.

  5. Using "external" & code splitting to only load the code necessary

  6. A mix of TypeScript and Javascript packages (to show a transitional state)

Code splitting + Routing example

This is useful pattern for upgrading older sites that rely on page redirects (e.g. django). We use this at Klaviyo to incrementally add react pages in a well factored way.

The navigation links on the main django site use 'hard' page redirects to reload the entire page. on load of the page, the main.js bundle is loaded. If the current route matches in router/src/index.js we then load the associated package and only that package to render.

Typescript implementation

This example repository uses@babel/plugin-transform-typescript to compile typescript files at webpack build time. Note that this does not do any type checking. Instead we rely on the typescript compiler for type checking.

This has some benefits:

  1. You can write TS files and have fast hot reloading in the browser (relative to TS compilation)

  2. Your IDE (e.g VSCODE) can report on typing errors. Alternatively run yarn tsc -b to build / type check your packages.

This also has some drawbacks / caveats:

  1. More build complexity with additional tools

  2. You need to have CI to ensure your types are being honored before deploy.

Commands

  • Bootstrap all dependencies. This will hoist shared packages to the root dir for a faster install. It will also link your dependencies together via symlinks.

    NOTE - use yarn install on the root repository first to install the necessary devdependencies (lerna)

    yarn bootstrap
  • Start a dev server. This will run webpack in any package that has a dev script.

    yarn dev
  • Build webpack across all packages

    yarn build
  • Lint all JS packages

    yarn lint
  • Lint all TS packages

    yarn lint:ts

Directory structure

.
├── README.md
├── lerna.json
├── package.json
├── packages
│   ├── async-leaf          // Asynchronously loaded package (via webpack chunks)
│   │   ├── package.json
│   │   └── src
│   │       └── index.jsx
│   ├── leaf                // Synchronously loaded package
│   │   ├── package.json
│   │   └── src
│   │       └── index.jsx
│   └── router              // Web facing package
│       ├── package.json
│       ├── src
│       │   ├── index.html
│       │   └── index.jsx
│       └── webpack.config.js
└── yarn.lock

Suggestions

I am fairly new to lerna and would love recommendations, or PRs, on how I might improve this simple example.

lerna-webpack-example's People

Contributors

dan-kez avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.