Demonstration of how you build full-stack, typed, NPM packages, the right way

Overview

NPM Packages - The Right way

This repository aims to demonstrate how to build NPM packages the right way.

  • my-package should import the shared code, built for the current environment.
  • my-package/server should import the server-only code (and maybe shared code as well?)
  • my-package/client should import the client-only code (and maybe shared code as well?)
  • Packages should be written in TypeScript and expose their type definitions

Bonus features:

  • Folders may have multiple indexes index.client.ts, index.server.ts etc.
  • Support common 3rd party tools: importing in Jest, Storybook, Cypress...

To sum it up, we want to demonstrate how to build fullstack, typed NPM packages.

We take inspiration from Meteor package system: https://guide.meteor.com/writing-atmosphere-packages.html. They provide an incredible developer experience, but were sadly limited to Meteor apps and suffer from a few technical issues. We want to achieve the same "DevX" with modern, generic bundlers.

Note: the shared code will be exactly the same in both environment, this is slightly different to what we call isomorphism. Isomorphism is only possible with some magic at import time (basically changing my-package to my-package/<current-environment> at build time in your app), which is out of scope here.

Run and test

  • Clone this repo
  • yarn - Install relevant packages
  • cd my-package-webpack && yarn && yarn run publish - Will build your package + generate a tarball
  • cd .. && cd demo-next-app && yarn && yarn run dev - Will start a Next.js app that imports each package
  • Open relevant page to test the import

How you can help

See contribute section below.

You can:

  • add a demo with a new bundler: Rollup, Tsup (https://github.com/egoist/tsup), Unbuild (https://github.com/unjs/unbuild) could be great candidates.
  • improve the build for one of the bundler we cover
  • add automated tests for the Next.js app, using Jest or Cypress
  • open an issue with ideas, feedback, knowledge about bundlers, a feature you'd like to test etc. etc.
  • add a performance benchmark
  • automate building, facilitate development

Learnings

Formats

  • UMD used to be the way to go for having a single shared bundle, but now ES Modules should be preferred. Moderne bundlers such as Esbuild might not support them: https://github.com/evanw/esbuild/issues/507
  • In Webpack, commonjs2 seems to do something similar to UMD
  • ES Modules will be the reference in the short run

NPM packages

TypeScript

Webpack

At the moment this repo doesn't demo importing other packages, or monorepo, but it could useful in the future.

Esbuild

  • Handling external is utterly painful for the server export! You want to add packages such as React, Graphql etc. as "externals" but there is no easy way to add all packages from package.json as externals

Others

  • Typing the bundler config is often difficult, because they are run at low-level, using Node. The @types directive in comments might help having IntelliSense in VS code, without actually using TypeScript files.
  • Don't forget to drop .next from time to time when working with local packages. Next.js might cache them, leading to stale imports
  • NPM and/or Yarn may cache .tgz files in an unexpected way, reinstalling a stale version everytime:https://github.com/yarnpkg/yarn/issues/6811

Contribute

If you want to test a new bundler, copy package-template and setup package.json accordingly. Please try to modify only the package.json: this way we can compare bundlers against the same basic features. Then add a new page in the Next.js demo app.

To update a package, publish it, and rerun yarn in the Next.js app. You may need to use npm if the package is not updated, see https://github.com/yarnpkg/yarn/issues/6811

You may use this package to reproduce bugs for certain bundlers. In this case, reproduce your bug, and open a pull request. We'll keep it open until the maintainers of the bundler fix the bug.

If you want to test a new feature (exporting React components, supporting path aliases or whatever), you may open a PR modifying the "package-template". We can then apply this change to all the packages using a command.

References

You might also like...

If you want to know more about your favorite TV show then you've come to the right place!

If you want to know more about your favorite TV show then you've come to the right place!

Ultimate Show Guide We want to introduce you this web page that will help you to manage all your favourite TV series from place. 🖥️ Desktop version H

Jun 23, 2022

Build a Full Stack Marketplace on Ethereum with React, Solidity, Hardhat, and Ethers.js

Build a Full Stack Marketplace on Ethereum with React, Solidity, Hardhat, and Ethers.js

Building a Digital Marketplace on Ethereum The technologies used in this workshop are React, Next.js, Tailwind CSS, HardHat, Solidity, and Ethers. Get

Nov 15, 2022

Lecture by lecture commits of the https://www.udemy.com/build-blockchain-full-stack/ course

Commit-by-commit breakdown of "Build a Blockchain & Cryptocurrency | Full-Stack Edition" This is a commit-by-commit breakdown of "Build a Blockchain &

Dec 12, 2022

A simple, strictly typed ORM, to assist you in using Cloudflare's D1 product

D1-Orm ✨ A simple, strictly typed ORM, to assist you in using Cloudflare's D1 product API reference can be found at https://d1-orm.pages.dev/modules D

Dec 25, 2022

Client-side app using various Lichess APIs for demonstration purpose

Lichess OAuth app demo This is an example for a fully client side OAuth app that uses various APIs. Features Fully client side, no server needed Login

Dec 6, 2022

A simple NFT trading and breeding platform for demonstration purposes.

A simple NFT trading and breeding platform for demonstration purposes.

📦 boxyz I know it sounds strange, but maybe these boxes can breed and have children. They can be sold as well. boxyz is a fun project to trade and br

Dec 12, 2022

A demonstration app for Fresh that shows how to use SSR, the islands functionality, APIs and more

Fresh Pokemon Demo Code This is a demonstration app for Fresh that shows how to use SSR, the islands functionality, APIs and more. You do need to conn

Dec 18, 2022

Project developed in typescript with clean architecture + tdd for demonstration purposes.

Description Project developed in typescript with clean architecture + tdd for demonstration purposes in Usystem. Installation $ yarn Running the app #

Oct 5, 2022

Easiest way to build documentation for your project. No config or build required, hosted on @netlify.

Easiest way to build documentation for your project. No config or build required, hosted on @netlify.

Hyperdocs is the simplest way you can create documentation for your project. It blends the best of all the other documentation tools in one. Hyperdocs

Dec 22, 2022
Owner
VulcanJS
VulcanJS
An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format. It can be used in Node.js and browser applications.

An npm package for demonstration purposes using TypeScript to build for both the ECMAScript Module format (i.e. ESM or ES Module) and CommonJS Module format. It can be used in Node.js and browser applications.

Snyk Labs 57 Dec 28, 2022
npm i uuid, npm i nodemon, npm i commander

goit-nodejs-hw-01 Получаем и выводим весь список контактов в виде таблицы (console.table) node index.js --action list Получаем контакт по id node inde

Oksana Banshchykova 3 Jul 5, 2022
It is a solo Project and In this repo I try to build a E-Commerce full-stack website with MERN stack technologies. For Practice purpose.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Alok Kumar 5 Aug 3, 2022
Detect npm packages by author name in your package-lock.json or yarn.lock.

detect-package-by-author Detect npm packages by author name in your package-lock.json or yarn.lock. Install Install with npm: # Not Yet Publish # npm

azu 2 Jan 11, 2022
Package fetcher is a bot messenger which gather npm packages by uploading either a json file (package.json) or a picture representing package.json. To continue...

package-fetcher Ce projet contient un boilerplate pour un bot messenger et l'executable Windows ngrok qui va permettre de créer un tunnel https pour c

AILI Fida Aliotti Christino 2 Mar 29, 2022
Get packages from a monorepo (pnpm, yarn, npm, lerna)

?? You can help the author become a full-time open-source maintainer by sponsoring him on GitHub. @egoist/get-packages Get packages from a monorepo (p

EGOIST 45 Jun 1, 2022
The high-impact (popular) packages of npm

npm-high-impact The high-impact (popular) packages of npm. Contents What is this? When should I use this? Install Use API npmHighImpact npmTopDependen

Titus 26 Dec 21, 2022
write the (w)right way!

Wrighter - A Powerful Markdown Blogger & A Writing Companion ⚡ Wrighter is originally made for the hashnode x PlanetScale hackathon It has a powerful

TK Vishal 93 Dec 6, 2022
HackFest is a 36-hour long hackathon wherein you have to put on your hacker hats and build anything that falls in either or both the domain of full-stack web development

HackFest is a 36-hour long hackathon wherein you have to put on your hacker hats and build anything that falls in either or both the domain of full-stack web development (the stack we learn in full-stack web developer roadmap on codedamn).

Shivam Kumar 2 Jun 6, 2022
This is a full stack application where you can log all you places where you visited....-

Full Stack Travelling Log ?? ?? ✈️ This is a full stack application where you can log ✈️ your all places ?? ?? ?? you have visited .... ??️ ??️ ??️ Se

null 19 Sep 29, 2022