Giter Site home page Giter Site logo

typescript-library-template's Introduction

A template to create TypeScript libraries.

Motivation

When creating a library or component, it is common to also create a small project to show it. In addition, that project can also be used to document or test our library.

This template uses Yarn Workspaces and is intended to serve as a basis for developing TypeScript libraries. It contains two important folders:

/demo --> an usual React application (CRA)
/lib  --> our library

Installation

Download the repository:

git clone https://github.com/gchumillas/typescript-library-template

and replace all occurrences of @npm-username/library-name with the name of your library. For example, let's say your NPM username is anderson and the desired library name is my-lib. Then, the full library name would be @anderson/my-lib:

// demo/src/App.tsx

import React from 'react'
- import { fibonacci } from '@npm-username/library-name'
+ import { fibonacci } from '@anderson/my-lib'
import './App.css'
// demo/package.json

{
  "name": "demo",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
-    "@npm-username/library-name": "*",
+    "@anderson/my-lib": "*",
// lib/package.json

{
-  "name": "@npm-username/library-name",
+  "name": "@anderson/my-lib",

And finally install the dependencies:

yarn install

Caveats: If you use VSCode you may need to reload the editor after replacing the library name. To reload the editor simply press Crl + P and select Developer: Reload Window.

Start developing

To start a development server, simply execute the following command from the root directory:

yarn start

If you take a look at the above command (package.json : scripts), you can see that it does two things:

  1. Compile your library into the /lib/src/dist folder and start a React server.
  2. Watch for changes in /lib/src and recompile it when needed.

Publish your library to NPM

Before publishing your library to NPM check out the following information:

// lib/package.json

{
  "name": "@npm-username/library-name",
  "license": "ISC",
  "version": "0.1.0",
  "description": "Another amazing library",
  "repository": "github:npm-username/library-name",
  "author": "Your Name",

Then log into NPM and publish your library

cd lib
npm login
npm publish --access public

Finally install your library from another project and verify that it works:

yarn add @npm-user/library-name

typescript-library-template's People

Contributors

gchumillas avatar

Watchers

 avatar

Forkers

joncatmullimp

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.