background

Small A student is A very passionate development of technology, small B is A fish all day master. One day, little B encountered A very difficult logic, which happened to be written by little A. Little B asked if he could share the code with him, and little A generously copied it to him. But little C did not want to repeat the wheel, also found little A. Small A said, I still send A NPM package, we can use directly. There are plenty of examples of this in daily development, but most front-end programmers feel that sending NPM packages is not for the big guys to do. Today we implement a very simple package of functions from 0, focusing on the release experience.

The target

There is a problem of accuracy loss when implementing large number addition in Javascript. We need to implement a function to ensure accuracy is not lost. Compressed and uncompressed versions are required and support the introduction of various modules such as AMD/CJS/ESM.

Project initialization

1 Initialize package.json to determine the name and version of the library. 2 Create the webpack.config.js file. 3 Determine the file directory

Implement different compressed versions

The same entry file is packaged with different names, and output [name].js different files, but how to compress the file with different names, here we use the terser-webpack-plugin.

Note: The re matches are not in quotes!

Set the module import mode

  • LibraryExport can be set to default for direct import into the library
  • LibraryTarget set to UMD can support various module import methods

Body library contents

Exporting function body

Use different compression packages depending on your environment

You need to set the main function call in package.json

Use different compression packages depending on your environment

Published online

NPM publish publish library, note that you need to register NPM account and log in before launching. You can view the current NPM account information by using NPM whoiam and login.

Release success

I am also a rookie, aiming to share the learning process, inevitably there will be unreasonable place welcome correction, light spray, hee hee Hee hee hee hee hee ~