Skip to content

Instantly share code, notes, and snippets.

@jasonsturges
Last active May 14, 2021 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonsturges/408bb65155c04b671eb1f3f3d95c244d to your computer and use it in GitHub Desktop.
Save jasonsturges/408bb65155c04b671eb1f3f3d95c244d to your computer and use it in GitHub Desktop.
Creating a TypeScript NPM Package
node_modules
build
dist
docs
{
"projectFolder": ".",
"mainEntryPointFilePath": "<projectFolder>/build/index.d.ts",
"bundledPackages": [],
"compiler": {
"tsconfigFilePath": "<projectFolder>/tsconfig.json",
"overrideTsconfig": {
"compilerOptions": {
"outDir": "build"
}
}
},
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "<projectFolder>/dist/index.d.ts"
},
"apiReport": {
"enabled": false
},
"docModel": {
"enabled": false
},
"tsdocMetadata": {
"enabled": false
},
"messages": {
"compilerMessageReporting": {
"default": {
"logLevel": "none"
}
},
"extractorMessageReporting": {
"default": {
"logLevel": "none"
}
},
"tsdocMessageReporting": {
"default": {
"logLevel": "none"
}
}
}
}
{
"name": "my-lib",
"version": "1.0.0",
"description": "Really awesome thing this does",
"author": "Me <me@email.com> (https://www.website.com)",
"homepage": "https://github.com/<username>/my-lib",
"keywords": [
"something",
"awesome"
],
"main": "dist/my-lib.cjs.js",
"module": "dist/my-lib.esm.js",
"browser": "dist/my-lib.umd.js",
"files": [
"dist"
],
"scripts": {
"dev": "rollup -c -w",
"build": "rollup -c",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-typescript": "^8.2.1",
"eslint": "^7.26.0",
"rollup": "^2.47.0",
"ts-node": "^9.1.1",
"tslib": "^2.2.0",
"typescript": "^4.2.4"
}
}
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import pkg from "./package.json";
export default [
// browser-friendly UMD build
{
input: "src/index.ts",
output: {
name: "myLib",
file: pkg.browser,
format: "umd",
},
plugins: [
resolve(),
commonjs(),
typescript({ tsconfig: "./tsconfig.json" }),
],
},
// CommonJS (for Node) and ES module (for bundlers) build.
// (We could have three entries in the configuration array
// instead of two, but it's quicker to generate multiple
// builds from a single configuration where possible, using
// an array for the `output` option, where we can specify
// `file` and `format` for each target)
{
input: "src/index.ts",
output: [
{ file: pkg.main, format: "cjs" },
{ file: pkg.module, format: "es" },
],
plugins: [typescript({ tsconfig: "./tsconfig.json" })],
},
];
{
"compilerOptions": {
"target": "es6",
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment