前端 | 一些實(shí)用的npm庫

nodejs.png

dependencies

1. whatwg-fetch

有些瀏覽器不支持fetch API,可以使用這個(gè)polyfill;fetch-ie8主要支持的是IE8瀏覽器使用Fetch API。

安裝:npm install whatwg-fetch --save or bower install fetch
使用:
在webpack中使用: entry: ['whatwg-fetch', ...]
在js文件中引入 import 'whatwg-fetch'

2. qrcode

用于將一些URL、文字、emojis等生成二維碼,并將二維碼保存成圖片。

安裝:npm install qrcode --save or bower install qrcode
使用:

var  QRCode = require('qrcode');
QRCode.toDataURL(url, function (err, url) {});

3. js-cookie

顧名思義是操作cookie的,有了這個(gè)庫讓我們對(duì)cookie進(jìn)行增刪改查方便了很多。
安裝:npm install js-cookie --save or bower install js-cookie
使用:

import Cookie from 'js-cookie';
Cookies.set('name', 'sherry', { expires: 7, path: '' });

4.cpr

用來將一個(gè)文件夾的文件拷貝到另一個(gè)文件夾。
安裝: npm install cpr --save
使用:

var cpr = require('cpr');
//or
var cpr = require('cpr').cpr;

cpr('/path/from', '/path/to', {
    deleteFirst: true, //Delete "to" before
    overwrite: true, //If the file exists, overwrite it
    confirm: true //After the copy, stat all the copied files to make sure they are there
}, function(err, files) {
    //err - The error if any (err.list might be available with an array of errors for more detailed information)
    //files - List of files that we copied
});

5. ua-parser-js

這個(gè)庫主要用于獲取瀏覽器的一些信息。
安裝: npm install ua-parser-js --save
使用:

var parser = require('ua-parser-js');
var parser = new UAParser();
var os = parser.getOS().name;
var browser = parser.getBrowser().name;
var brover = parser.getBrowser().major;

6.[nprogress] (https://www.npmjs.com/package/nprogress)

devDependencies

1. ora

screenshot.gif

如圖所以,在打包編譯的時(shí)候可以用這個(gè)庫,圖中轉(zhuǎn)動(dòng)的loading圖案就是這個(gè)庫的效果。這在編譯的時(shí)候相當(dāng)于一種反饋,不至于不知道發(fā)生了什么。
安裝:npm install --save ora
使用:

const ora = require('ora');
 
const spinner = ora('Loading unicorns');
spinner.start();
......
spinnner.stop();

2.shelljs

有了這個(gè)庫,你可以在其他平臺(tái)(比如Windows/Linux/OS X)使用Unix平臺(tái)的shell命令。
安裝:npm install [-g] shelljs
使用:

var shell = require('shelljs');
// Copy files to release dir 
shell.rm('-rf', 'out/Release');
shell.cp('-R', 'stuff/', 'out/Release');
 
// Replace macros in each .js file 
shell.cd('lib');

3.preprocess

用來預(yù)處理一些HTML,JS文件
安裝: npm install --save preprocess
使用:

<head>
  <title>Your App</title>
 
  <!-- @if NODE_ENV='production' -->
  <script src="some/production/lib/like/analytics.js"></script> 
  <!-- @endif -->
 
</head>
<body>
  <!-- @ifdef DEBUG -->
  <h1>Debugging mode - <!-- @echo RELEASE_TAG --> </h1>
  <!-- @endif -->
  <p>
  <!-- @include welcome_message.txt -->
  </p>
</body>

4.log4js

未完待續(xù)。。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容