In this vendible I'm going to imbricate a process of publishing TypeScript package with external dependencies to NPM Write some code The package we're going to publish is React.js custom vaccinate for throttling values: react-use-throttle. I've once written vendible well-nigh developing this vaccinate and now we're going to publish it to NPM First things first, we need to write lawmaking for the package. I've put my lawmaking to src/index.ts file tsconfig.json In order to develop with TypeScript we need to add tsconfig to our repository. My config looks like this:
{
  "include": ["./src/**/*"],
  "compilerOptions": {
    "strict": true,
    "declaration": true, // generates declaration files
    "esModuleInterop": true
  }
}
Enter fullscreen mode Exit fullscreen mode
To learn increasingly well-nigh variegated options please squint at TSConfig Reference Set up Rollup Rollup is a module bundler for JavaScript which compiles small pieces of lawmaking into something larger and increasingly complex, such as a library or application Rollup allows developers hands compile lawmaking into variegated JavaScript module systems such as ESModules, UMD, AMD or CommonJS. There's a unconfined vendible tent major differences between them This is my rollup.config.js file, it exports an variety of objects, where each object defines how Rollup should build our lawmaking in specified format. Here we're towers lawmaking for ES and UMD modules, considering there're most worldwide nowadays. Each stow has TypeScript and Babel plugins, and UMD stow moreover has terser plugin for lawmaking minification
import typescript from 'rollup-plugin-typescript2'
import babel from '@rollup/plugin-babel'
import { terser } from 'rollup-plugin-terser'

export default [
  // ES Modules
  {
    input: 'src/useThrottle.ts',
    output: {
      file: 'dist/index.es.js', format: 'es',
    },
    plugins: [
      typescript(),
      babel({ extensions: ['.ts'] }),
    ],
  },

  // UMD
  {
    input: 'src/useThrottle.ts',
    output: {
      file: 'dist/index.umd.min.js',
      format: 'umd',
      name: 'reactUseThrottle',
      indent: false,
    },
    plugins: [
      typescript(),
      babel({ extensions: ['.ts'], exclude: 'node_modules/**' }),
      terser(),
    ],
  },
]
Enter fullscreen mode Exit fullscreen mode
To learn increasingly well-nigh Rollup configuration please squint at Rollup quick start guide Build lawmaking and publish package to NPM We need to specify the pursuit fields in package.json file
"name": "react-use-throttle",
"version": "0.0.1",
"main": "dist/index.umd.min.js",
"module": "dist/index.es.js",
"types": "dist/useThrottle.d.ts",
"files": ["dist"]
Enter fullscreen mode Exit fullscreen mode
name and version together identify package completely unique main is the primary entry point to our package module is the entry point for ESModules types is the entry point for TypeScript type declarations files is an variety of patterns that describes the entries to be included when your package is installed as a dependency Learn increasingly well-nigh variegated fields in package.json file: Package.json docs Also, we need to specify react as peerDependency, considering it will not be widow to final build
"peerDependencies": {
  "react": "^16.8.0  || ^17.0.0"
}
Enter fullscreen mode Exit fullscreen mode
To build lawmaking with rollup we need to run the pursuit command:
rollup -c
Enter fullscreen mode Exit fullscreen mode
It will build our package based on rules we specified in rollup.config.js. Lawmaking will be generated to dist folder Now we're ready to publish our package, to do this we need to run the pursuit commands:
npm login # this will ask you for your NPM login and password
npm publish
Enter fullscreen mode Exit fullscreen mode
Package was successfully published to NPM ? Alt Text