Giter VIP home page Giter VIP logo

template-package-react's Introduction

React + TypeScript + Vite

  1. Setup Vite: pnpm create vite
  2. Setup nvm and node version: echo "v20.11.0" > .nvmrc && nvm use
  3. Setup prettier:
{
  "tabWidth": 2,
  "useTabs": false
}
  1. Vite library mode:

p i -D @types/node

In vite.config.ts:

import { resolve } from 'path'

build: {
    lib: {
      entry: resolve(__dirname, "lib/main.ts"),
      fileName: "library",
      formats: ["cjs", "es"],
    },
    rollupOptions: {
      // make sure to externalize deps that shouldn't be bundled into your library
      external: ["react", "react-dom"],
      output: {
        // provide global variables to use in the UMD build for externalized deps
        globals: {
          react: "React",
          "react-dom": "React-dom",
        },
      },
    },
    sourcemap: true,
    emptyOutDir: true,
  },

Reference components in lib/main.ts:

export { Counter } from "../src/components/Counter";
  1. Package

add to package.json:

"name": "template-package-react",
"version": "1.0.0",
"main": "dist/library.cjs",
"module": "dist/library.js",
"files": [
    "/dist"
],
"build:pack": "pnpm build && pnpm pack --pack-destination ~",

The package can be imported in other projects pnpm i ~/template-package-react-1.0.0.tgz

  1. Add types to package

Add tsconfig.build.json that can be used to build type files:

{
  "extends": "./tsconfig.json",
  "include": ["lib"],
  "exclude": [],
  "compilerOptions": {
    "outDir": "./dist",
    "noEmit": false,
    "emitDeclarationOnly": true,
    "declaration": true
  }
}

Include type files in package ... update package.json:

  "types": "dist/lib/main.d.ts",
  "scripts": {
    "build": "tsc && vite build",
    "build:types": "tsc -p tsconfig.build.json",
    "build:pack": "pnpm build && pnpm build:types && pnpm pack --pack-destination ~",
  }

template-package-react's People

Contributors

johnathan-sewell avatar

Watchers

 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.