grunt完整的配置demo

  1 const path = require('path')
  2 const fs = require('fs');
  3 module.exports = function (grunt) {
  4   grunt.registerTask('setapi', 'setapi..', function (arg1, arg2) {
  5     var RouteMapLibPath = './dest/limsStyle/RouteMapLib-1.1.js';
  6     var data = fs.readFileSync('./dest/limsStyle/config.json');
  7     var RouteMapLib = fs.readFileSync(RouteMapLibPath).toString();
  8     data = JSON.parse(data);
  9     if (typeof arg1 != 'undefined') {
 10       switch (arg1) {
 11         case 'dev': {
 12           replaceConfig(data.devconfig)
 13         }; break;
 14         case 'test': {
 15           replaceConfig(data.testconfig)
 16         }; break;
 17         case 'build': {
 18           replaceConfig(data.buildconfig)
 19         }; break;
 20       }
 21     }
 22     else {
 23       replaceConfig(data.location)
 24     }
 25     fs.writeFileSync(RouteMapLibPath, RouteMapLib);
 26 
 27     function replaceConfig(d) {
 28       RouteMapLib = RouteMapLib.replace('{#ajaxUrl}', d.ajaxUrl);
 29       RouteMapLib = RouteMapLib.replace('{#user_token}', d.user_token);
 30       RouteMapLib = RouteMapLib.replace('{#ELNEdit}', d.ELNEdit);
 31       // console.log(RouteMapLib);
 32     };
 33 
 34   });
 35 
 36 
 37   grunt.initConfig({
 38     clean: ['/dest'],
 39     jshint: {
 40       files: ['limsStyle/postil.js'],
 41       options: {
 42         globals: {
 43           jQuery: true
 44         }
 45       }
 46     },
 47     watch: {
 48       cwd: 'src',
 49       files: ['src/**/*.js', 'src/**/*.json', 'src/**/*.html', 'src/limsStyle/*.css', 'src/**/*.less'],
 50       tasks: ['copy:main', 'setapi', 'jshint', 'babel'],
 51       options: {
 52         reload: true,
 53         livereload: 35729
 54       }
 55     }
 56     ,
 57     uglify: {
 58       main: {
 59         src: '',
 60         dest: ''
 61       },
 62     },
 63     copy: {
 64       processContentExclude: ['othen/**'],
 65       main: {
 66         expand: true,
 67         cwd: 'src',
 68         //src: ['**', '!*.sln', '!**lib/diff/**', '!**lib/layer-v3.1.1/**', '!**lib/layerdate/**', '!**.vs/**', '!**lib/bootstrap-3.3.7-dist/**', '!**lib/ueditor1_4_3-utf8-net/third-party/**', '!**lib/ueditor1_4_3-utf8-net/third-party/dialogs/**'], //** 
 69         src: ['limsStyle/**', 'othen/**', '_temp/**', 'images/**', 'layertemplate/**', 'ElnIndex.html'],
 70         dest: 'dest/',
 71         flatten: false,
 72         filter: 'isFile'
 73       },
 74       all: {
 75         expand: true,
 76         cwd: 'src',
 77         src: ['**'], //** 
 78         dest: 'dest/',
 79         flatten: false,
 80         filter: 'isFile'
 81       }
 82     }
 83     , babel: {
 84       options: {
 85         sourceMap: true,
 86         presets: ['babel-preset-es2015']
 87 
 88       },
 89       dist: {
 90         files: [{
 91           expand: true,
 92           cwd: 'src/',
 93           src: ['limsStyle/postil.js'],
 94           dest: 'dest/'
 95         }]
 96       }
 97     },
 98     connect: {
 99       options: {
100         port: 9000,
101         hostname: 'localhost',
102         livereload: 35729
103       },
104       server: {
105         options: {
106           open: true,
107           base: [
108             "dest"
109           ]
110         }
111       }
112     },
113   });
114 
115   grunt.loadNpmTasks('grunt-contrib-uglify');
116   grunt.loadNpmTasks('grunt-contrib-jshint');
117   grunt.loadNpmTasks('grunt-contrib-copy');
118   grunt.loadNpmTasks('grunt-contrib-clean');
119   grunt.loadNpmTasks('grunt-contrib-watch');
120   grunt.loadNpmTasks('grunt-contrib-connect');
121   grunt.loadNpmTasks('grunt-babel');
122   // grunt.registerTask('clean', ['clean:release', 'copy:all']);
123 
124 
125   grunt.registerTask('default', ['copy:main', 'babel', 'setapi', 'connect', 'watch']);
126   grunt.registerTask('dev', ['copy:main', 'babel', 'setapi:dev'])
127   grunt.registerTask('test', ['copy:all', 'babel', 'setapi:test']);
128   grunt.registerTask('build', ['copy:all', 'babel', 'setapi:build']);
129 
130 
131 };
原文地址:https://www.cnblogs.com/sxmny/p/10100774.html