Recently, the project needs pure stateless components, so we package the components and make them into plug-ins. We directly refer to them, and write down the summary of making plug-ins to share, so that people can make their own plug-ins from scratch.
Vue’s stamp here
Make the plugin
-
Install Node and NPM, create a new folder, open it in terminal, run NPM init, press Enter and type Yes to generate package.json, {“name”: “swTest”, “version”: “1.0.0”, “description” : “”,” main “:” index. Js “, “scripts” : {” test “, “echo \” Error: No test specified\” && exit 1″}, “author”: “”, “license”: “ISC”} Version indicates the version number. You need to manually update the version number each time you release a new version. Description is a description. Main is the entry file, the file that you read when you load this plug-in, and this is also the most important property, this property is the entry file that someone reads when they reference your plug-in, and in this file, you need to expose the interface; Author is the author, which can be an NPM registered name.
-
Create a new SRC folder under the root directory to store your own code snippets.
-
Create a new.npmignore in the root directory. NPM ignores the files in the project when publishing.
-
Create readme. md in the root directory, which contains the project description for publishing to NPM.
Babel escape
Because many syntaxes are ES6 and above, you need to escape to ES5.
- Install Babel globally:
npm install -g babel-cli
- ES6 Transcoding rules:
npm install babel-preset-es2015 --save-dev
- React transcoding rule:
npm install babel-preset-react --save-dev
- Transcoding rules for grammar proposals at different stages of ES7:
npm install babel-preset-stage-0 --save-dev
(0-3 is ok) - Create a.babelrc file in the root directory. ; Adding rules
{ "presets": [ "es2015", "react", "stage-2" ], "plugins": [] }
. - We usually write the code in SRC, then escape the whole SRC to es5, and run the code in the root directory
babel src --out-dir lib
SRC escaped files. - Modify package.json to change main to
lib/index
. - Modify. Npmignore file to add
src/ node_modules/ package-lock.json
️ Publish plug-ins
- If you do not have an NPM account, please go to the official website of NPM to register. The official website is:
https://www.npmjs.com/
; - First change the name in package.json to your plug-in name, and then check whether this name has been registered on NPM official website. If so, change it.
- Project root directory
npm adduser
, input NPM account password and email address, both indispensable. (Note: Enter an account if you already have onenpm login
Login, account password and email, is also indispensable, if errorcode E409
Because the NPM of Taobao was used, the login error was caused. Because you logged in to the NPM warehouse source of Taobao, Taobao should not provide you with login service. Just change the NPM Registry to the official one.nrm use npm
If an error is reported, NRM is not installed. Run the codenpm install -g nrm
Install NRM to execute code:nrm use npm
); - You can run
npm whoami
Check whether the current user is yourself. - Run the code
npm publish
Launch; - You can click on packages in the upper right corner of NPM’s official website to view your releases.
- You can introduce your own plug-ins into your project
npm install xxx --save-dev
To use;
️ Maintenance plug-in
- The functions of the plug-in code have been expanded or modified, and a new version needs to be released. In addition to the code changes, you need to manually modify the version in package.json each time, which is the version number, indicating that the version has changed. The version number rule used by NPM is semver semantic version, initialized with 1.0.0, followed by the master version number. Revision number, major revision number is an incompatible API change, minor revision number is a functional addition, revision number is a problem fix.
- Run it again
babel src --out-dir lib
Get the latest lib and executenpm publish
Launch; - How are projects in use updated after release? (1) If it is NPM, since it is its own plug-in, it can know what version has been changed. Find the version of the dependent package in package.json of the project, manually change the version number to the latest version, and then the project will run
npm install --force
It’s ok; (2) If yarn is used, run the commandyarn upgrade
Also ok; (3) Because the dependency packages in the project will be updated, sometimes we will install third-party plug-ins to check.npm install -g npm-check-updates
Run,ncu
View updatable packages,ncu -u
Update package. Json,npm install
Upgrade to the latest version.
A bit more dry stuff, when installing plug-ins, –save-dev is what you rely on during development, –save is what you rely on after release. -g indicates global installation, –save-dev can be abbreviated to -d, –save can be abbreviated to -s, and NPM install can be abbreviated to NPM I.
Code has been uploaded in the lot, lot address, welcome to visit (address: https://github.com/Songwei1029/create-plug), welcome to leave a message if you have any questions.