/*! git * mymongolib github * Copyright(c) 2009-2013 Blabla shell * MIT Licensed npm */ json
'use strict'; windows
module.exports = require('./lib/mymongolib'); ide
|
'use strict' grunt
function MyMonboLib(connStr) ui { this this.ConnStr = connStr;
this.TestConn = function() { return true; }
this.DeleteOneDoc(collName, _id) { return true; }
}
module.exports = MyMongoLib; |
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']);
}; |
npm install grunt --save-dev |
npm install grunt-contrib-uglify --save-dev |
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; |
大功告成!
固然,若是隻想對一個js文件執行grunt操做,就不須要建立index.js和lib文件夾裏面的文件,直接將文件放在根目錄,而後將GruntFile.js文件中的路徑改一下就行了。
如何建立一個 示例 GruntFile.
npm install -g grunt-cli |
git clone https://github.com/gruntjs/grunt-init-gruntfile.git ~/.grunt-init/gruntfile |
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文件,能夠用來作樣例或者從中複製粘貼一些代碼用。