?
- Create a folder.
- Open the command line or PowerShell, run NPM Init, and fill in the package information as prompted.
- Create the Index.js file in the folder.
/*! * Mymongolib * Copyright (c) 2009-2013 BlaBla * MIT Licensed */ ? ' Use strict '; ? Module.exports = require ('./lib/mymongolib '); ? |
- 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; |
?
- 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 ']); ? }; |
?
- Install grunt in the command line or PowerShell.
NPM Install grunt--save-dev |
?
- Install the Grunt-contrib-uglify module in the command line or PowerShell.
NPM Install grunt-contrib-uglify--save-dev |
?
- Execute the grunt command on the command line or in PowerShell.
?
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.
- Installing GRUNT-CLI
?
- Install the Windows version of Git.
- Create an empty folder.
- 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 |
?
- Create the sample Docker file.
?
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