1. 创建一个文件夹。
  2. 打开命令行或者powershell, 运行npm init,根据提示填入package的信息。
  3. 在文件夹中创建index.js文件。

/*!

* mymongolib

* Copyright(c) 2009-2013 Blabla

* MIT Licensed

*/

 

'use strict';

 

module.exports = require('./lib/mymongolib');

 

  1. 在文件夹中创建lib目录,然后在新目录中创建mymongolib.js文件。

'use strict'

 

function MyMonboLib(connStr)

{

this.ConnStr = connStr;

 

this.TestConn = function()

{

return
true;

}

 

this.DeleteOneDoc(collName, _id)

{

return
true;

}

 

}

 

module.exports = MyMongoLib;

 

  1. 回到项目的根目录文件夹,然后里面创建Gruntfile.js文件。

module.exports = function(grunt) {

 

// Project configuration.

grunt.initConfig({

pkg: grunt.file.readJSON('package.json'),

uglify: {

options: {

banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'

},

build: {

src: ['lib/<%= pkg.name %>.js'],

dest: 'dist/<%= pkg.name %>.js'

}

}

});

 

// Load the plugin that provides the "uglify" task.

grunt.loadNpmTasks('grunt-contrib-uglify');

 

// Default task(s).

grunt.registerTask('default', ['uglify']);

 

};

 

  1. 在命令行或者PowerShell中安装grunt.

npm install grunt --save-dev

 

  1. 在命令行或者PowerShell中安装grunt-contrib-uglify模块。

npm install grunt-contrib-uglify --save-dev

 

  1. 在命令行或者PowerShell中执行grunt命令。

grunt

 

结果如下:

Running "uglify:build" (uglify) task

>> 1 file created 265 B → 193 B

 

此时看到项目的根目录下,自动创建了一个dist文件夹,里面自动创建了一个mymongolib.js文件, 文件内容如下:

/*! mymongolib 2017-11-07 */

 

"use strict";function MyMonboLib(n){return
this.ConnStr=n,this.TestConn=function(){return!0},this.DeleteOneDoc(collName,_id),!0}module.exports=MyMongoLib;

 

大功告成!

 

 

 

附录1:

当然,如果只想对一个js文件执行grunt操作,就不需要创建index.js和lib文件夹里面的文件,直接将文件放在根目录,然后将GruntFile.js文件中的路径改一下就好了。

 

附录2:

如何创建一个 示例 GruntFile.

  1. 安装grunt-cli

npm install -g grunt-cli

 

  1. 安装windows版本的git.
  2. 建立一个空文件夹。
  3. 从命令行或者PowerShell中定位到新文件夹,然后从github上下载模板。

git clone https://github.com/gruntjs/grunt-init-gruntfile.git ~/.grunt-init/gruntfile

 

  1. 创建 sample Docker file.
grunt-init gruntfile

 

这个命令会给出提示:

Please answer the following:

[?] Is the DOM involved in ANY way? (Y/n) n

[?] Will files be concatenated or minified? (Y/n) y

[?] Will you have a package.json file? (Y/n) y

[?] Do you need to make any changes to the above before continuing? (y/N) n

 

Writing Gruntfile.js...OK

Writing package.json...OK

 

Initialized from template "gruntfile".

 

这样,在文件夹中就有了对应的GruntFile.js和package.json文件,可以用来做样例或者从中复制粘贴一些代码用。

例子:使用Grunt创建一个Node.js类库的更多相关文章

  1. 如何快速搭建一个 Node.JS 项目并进入开发?

    了解:如何快速搭建一个项目并进入开发? 在此不概述 Node.JS 的历史以及发展过程. 因为之前接触过通过 Java 开发语言,所以明确地知道一个服务器所需的文件,以及一个服务器所需要的操作. 那么 ...

  2. 开启第一个Node.js的Express项目

    手动创建一个Express.js的应用可大致分为以下步骤: 1.创建文件夹 a. 创建一个项目根文件夹,如helloWord b.在项目的根目录下创建项目的目录结构,依次创建{public,publi ...

  3. 如何用Docker建立一个Node.js的开发环境

      建立一个文件夹 用管理员身份打开powershell. 在文件夹下面运行npm init, 根据提示填入信息,以便产生一个package.json文件. 在文件中加入需要的dependencies ...

  4. 3.第一个Node.js程序:Hello World!

    转自:http://www.runoob.com/nodejs/nodejs-tutorial.html 以下是我们的第一个Node.js程序: console.log("Hello Wor ...

  5. 建立一个node.js服务器(使用express搭建第一个Web环境)

    一.官网下载node.js 下载地址:https://nodejs.org/en/download/ 根据向导,下一步安装就可以了! 二.使用express搭建Web环境 express是一个开源的n ...

  6. 运行第一个Node.js程序

    初学Node.js,萌新一枚.感觉Node.js挺不错的,从基础开始一步一步来,加油吧! 我们来使用Node.js的express来运行第一个程序helloworld: 在命令提示符键入express ...

  7. 指令创建 Express Node.js 项目

    1.安装 Express 1.1 安装 Express 框架 首先保证已经安装过了 Node.js,然后进入终端使用管理员身份来安装 Express 框架. # 安装 express $ sudo n ...

  8. 一个node.js图片上传显示小应用

    文件结构如下: 实现的功能有: 可以通过浏览器使用. 当请求http://domain/start时,可以看到一个欢迎页面,页面上有一个文件上传的表单. 用户可以选择一个图片并提交表单,随后文件将被上 ...

  9. node.js03 第一个node.js程序和读取文件

    Hello World 1.创建运行 创建txt文件起名为hellonode,在记事本中编写JavaScript脚本文件 例如: var bbl = 'hellonode' console.log(b ...

随机推荐

  1. PHP之PDO_MYSQL扩展安装步骤(转)

    看到CakePHP文档要求安装pdo_mysql扩展,于是就尝试安装了一下. 这里我的系统是CentOS 6.0.如果你的系统是其他版本的Linux/Unix,可以参考.如果你的系统是Windows的 ...

  2. How to properly set clock speed for STM32F4xx devices

    http://stm32f4-discovery.com/2015/01/properly-set-clock-speed-stm32f4xx-devices/ I see that you have ...

  3. IAR EWAR 内联汇编 Error[Og010], Error [Og005], Error [Og006]

    Error [Og005] + [Og006] when using inline assembler EW targets: 430, ARM, AVR EW component: C/C++ co ...

  4. Cocos2d-x3.1TestCpp之NewRenderTest Demo分析

    1.代码构成 VisibleRect.h VisibleRect.cpp AppDelegate.h AppDelegate.cpp HelloWorldScene.h HelloWorldScene ...

  5. Cocos2d-x之Schedule

    Cocos2dx的定时器 from://http://blog.linguofeng.com/archive/2012/11/14/cocos2d-x-Schedule.html 一.schedule ...

  6. Module ngx_http_v2_module

    官方配置说明: http://nginx.org/en/docs/http/ngx_http_v2_module.html#example ngx_http_v2_module模块指令中文说明 ngx ...

  7. 吸血鬼日记第一季/全集The Vampire Diaries迅雷下载

    本季The Vampire Diaries 1 第一季(2009)看点: <吸血鬼日记>由美国女作家L.J.史密斯的同名畅销系列小说改编而成.4个月前一场可怕的车祸夺去了他们双亲的生命,但 ...

  8. Process 'command '/Users/lidaqiang/Library/Android/sdk/build-tools/27.0.3/aapt'' finished with non-zero exit value 1

    Process 'command '/Users/lidaqiang/Library/Android/sdk/build-tools/27.0.3/aapt'' finished with non-z ...

  9. pip 安装错误 'ascii' codec can't encode characters

    安装 python-dev既可解决 apt-get install python-dev

  10. WorkerThread与MainThread之间通过Handler进行最简单的消息传递

    一.从自己开启的线程中给主线程发送信息,更新UI 这个实例的效果是,在线程中通过handler发送一条信息给handler,然后通过handler更改UI线程中,textview的文字. 主要方法是( ...