Example: Creating a node. js class Library using Grunt

Source: Internet
Author: User
Tags create index

?

    1. Create a folder.
    2. Open the command line or PowerShell, run NPM Init, and fill in the package information as prompted.
    3. Create the Index.js file in the folder.

/*!

* Mymongolib

* Copyright (c) 2009-2013 BlaBla

* MIT Licensed

*/

?

' Use strict ';

?

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

?

    1. Create the Lib directory in the folder, and then create the Mymongolib.js file in the new directory.

' Use strict '

?

function mymonbolib (CONNSTR)

{

This. ConnStr = connstr;

?

This. Testconn = function()

{

Returntrue;

}

?

This. Deleteonedoc (Collname, _id)

{

Returntrue;

}

?

}

?

Module.exports = Mymongolib;

?

    1. Go back to the project root folder and create the Gruntfile.js file inside.

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. Install grunt in the command line or PowerShell.

NPM Install grunt--save-dev

?

    1. Install the Grunt-contrib-uglify module in the command line or PowerShell.

NPM Install grunt-contrib-uglify--save-dev

?

    1. Execute the grunt command on the command line or in PowerShell.

Grunt

?

The results are as follows:

Running "Uglify:build" (uglify) task

>> 1 file created 265 b→193 B

?

At this time see the project root directory, automatically created a dist folder, which automatically created a mymongolib.js file, the file content is as follows:

/*! Mymongolib 2017-11-07 */

?

"Use strict"; function mymonbolib (n) {returnthis. Connstr=n,this. testconn=function() {return! 0},this. Deleteonedoc (collname,_id),! 0}module.exports=mymongolib;

?

Done!

?

?

?

Appendix 1:

Of course, if you just want to do a grunt operation on a JS file, you do not need to create Index.js and Lib folder files, directly put the file in the root directory, and then the Gruntfile.js file in the path to change it.

?

Appendix 2:

How to create a sample gruntfile.

    1. Installing GRUNT-CLI

NPM install-g GRUNT-CLI

?

    1. Install the Windows version of Git.
    2. Create an empty folder.
    3. Navigate to the new folder from the command line or PowerShell and download the template from GitHub.

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

?

    1. Create the sample Docker file.

grunt-init gruntfile

?

This command will give you a hint:

please answer the following:

[? Is the DOM involved in any? (y/n) n

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

[?] Would 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".

?

Thus, in the folder has the corresponding gruntfile.js and Package.json files, can be used to do the sample or copy and paste some code from it.

Example: Creating a node. js class Library using Grunt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.