npm is a package manager for the JavaScript programming language. npm, Inc. is a subsidiary of GitHub, that provides hosting for software development and version control with the usage of Git. npm is the default package manager for the JavaScript runtime environment Node.js. Wikipedia
In this article, I use typescript, but if you don't want to use it, you can skip things that are related to typescript such as installing typescript, generating tsconfig.json, and using ts extension and types in codes.
Set up a template project
First, we need to create a folder for the package template and run yarn init or npm init. I think package.json that is generated by npm init can be better than yarn's one since it covers the most basic items.
$ mypackagetemplate
$ cd mypackagetemplate
$ yarn init or npm init
Install packages for the template
In this step, we install some packages for the template.
In this step, we will set up eslint and Jest.
We will install eslint and jest. yarn run eslint --init command allows us to create a config file intractively.
$ yarn add eslint jest ts-jest -D$ yarn run eslint --init# settings for this# ❯ To check syntax and find problems# ❯ JavaScript modules (import/export)# ❯ None of these# ? Does your project use TypeScript? › No / ❯ Yes# ✔ Browser# ✔ Node# ❯ JavaScript# ? Would you like to install them now with npm? › No / ❯ Yes
The final step is setting up rollup.js(including Babel).
rollup.js
Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application. It uses the new standardized format for code modules included in the ES6 revision of JavaScript, instead of previous idiosyncratic solutions such as CommonJS and AMD. ES modules let you freely and seamlessly combine the most useful individual functions from your favorite libraries. This will eventually be possible natively everywhere, but Rollup lets you do it today.
import{terser}from"rollup-plugin-terser";importpluginTypescriptfrom"@rollup/plugin-typescript";importpluginCommonjsfrom"@rollup/plugin-commonjs";importpluginNodeResolvefrom"@rollup/plugin-node-resolve";import{babel}from"@rollup/plugin-babel";import*aspathfrom"path";importpkgfrom"./package.json";constmoduleName=pkg.name.replace(/^@.*\//,"");constinputFileName="src/index.ts";constauthor=pkg.author;constbanner=`
/**
* @license
* author: ${author}
* ${moduleName}.js v${pkg.version}
* Released under the ${pkg.license} license.
*/
`;exportdefault[{input:inputFileName,output:[{name:moduleName,file:pkg.browser,format:"iife",sourcemap:"inline",banner,},{name:moduleName,file:pkg.browser.replace(".js",".min.js"),format:"iife",sourcemap:"inline",banner,plugins:[terser()],},],plugins:[pluginTypescript(),pluginCommonjs({extensions:[".js",".ts"],}),babel({babelHelpers:"bundled",configFile:path.resolve(__dirname,".babelrc.js"),}),pluginNodeResolve({browser:true,}),],},// ES{input:inputFileName,output:[{file:pkg.module,format:"es",sourcemap:"inline",banner,exports:"named",},],external:[...Object.keys(pkg.dependencies||{}),...Object.keys(pkg.devDependencies||{}),],plugins:[pluginTypescript(),pluginCommonjs({extensions:[".js",".ts"],}),babel({babelHelpers:"bundled",configFile:path.resolve(__dirname,".babelrc.js"),}),pluginNodeResolve({browser:false,}),],},// CommonJS{input:inputFileName,output:[{file:pkg.main,format:"cjs",sourcemap:"inline",banner,exports:"default",},],external:[...Object.keys(pkg.dependencies||{}),...Object.keys(pkg.devDependencies||{}),],plugins:[pluginTypescript(),pluginCommonjs({extensions:[".js",".ts"],}),babel({babelHelpers:"bundled",configFile:path.resolve(__dirname,".babelrc.js"),}),pluginNodeResolve({browser:false,}),],},];
Finally, we can build. If everything goes well, you will see dist folder and 4 js files(hellotslib.cjs.js, hellotslib.es.js, hellotslib.js, and hellotslib.min.js)
$ yarn build
Now you can add any functionalities that you want to bring to npm world via your npm package 😎