|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var fs = require("fs"), |
| 4 | + path = require("path"); |
| 5 | + |
| 6 | +module.exports = function (grunt) { |
| 7 | + grunt.initConfig({ |
| 8 | + pkg: grunt.file.readJSON("package.json"), |
| 9 | + concat: { |
| 10 | + options: { |
| 11 | + banner: "/**\n" + |
| 12 | + " * <%= pkg.name %>\n" + |
| 13 | + " *\n" + |
| 14 | + " * @copyright <%= grunt.template.today('yyyy') %> <%= pkg.author %>\n" + |
| 15 | + " * @license <%= pkg.license %>\n" + |
| 16 | + " * @version <%= pkg.version %>\n" + |
| 17 | + " */\n" |
| 18 | + }, |
| 19 | + dist: { |
| 20 | + src: [ |
| 21 | + "<banner>", |
| 22 | + "src/intro.js", |
| 23 | + "src/filesize.js", |
| 24 | + "src/outro.js" |
| 25 | + ], |
| 26 | + dest: "lib/filesize.es6.js" |
| 27 | + } |
| 28 | + }, |
| 29 | + "babel": { |
| 30 | + options: { |
| 31 | + sourceMap: false, |
| 32 | + presets: ["babel-preset-env"] |
| 33 | + }, |
| 34 | + dist: { |
| 35 | + files: { |
| 36 | + "lib/<%= pkg.name %>.js": "lib/<%= pkg.name %>.es6.js" |
| 37 | + } |
| 38 | + } |
| 39 | + }, |
| 40 | + eslint: { |
| 41 | + target: [ |
| 42 | + "Gruntfile.js", |
| 43 | + "test/*.js", |
| 44 | + "lib/<%= pkg.name %>.es6.js" |
| 45 | + ] |
| 46 | + }, |
| 47 | + nodeunit: { |
| 48 | + all: ["test/*.js"] |
| 49 | + }, |
| 50 | + uglify: { |
| 51 | + options: { |
| 52 | + banner: "/*\n <%= grunt.template.today('yyyy') %> <%= pkg.author %>\n @version <%= pkg.version %>\n*/", |
| 53 | + sourceMap: true, |
| 54 | + sourceMapIncludeSources: true |
| 55 | + }, |
| 56 | + target: { |
| 57 | + files: { |
| 58 | + "lib/filesize.min.js": ["lib/filesize.js"] |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | + watch: { |
| 63 | + js: { |
| 64 | + files: "<%= concat.dist.src %>", |
| 65 | + tasks: "build" |
| 66 | + }, |
| 67 | + pkg: { |
| 68 | + files: "package.json", |
| 69 | + tasks: "build" |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + // tasks |
| 75 | + grunt.loadNpmTasks("grunt-contrib-concat"); |
| 76 | + grunt.loadNpmTasks("grunt-contrib-nodeunit"); |
| 77 | + grunt.loadNpmTasks("grunt-contrib-watch"); |
| 78 | + grunt.loadNpmTasks("grunt-contrib-uglify"); |
| 79 | + grunt.loadNpmTasks("grunt-babel"); |
| 80 | + grunt.loadNpmTasks("grunt-eslint"); |
| 81 | + grunt.task.registerTask("babili", "Minifies ES2016+ code", function () { |
| 82 | + const data = fs.readFileSync(path.join(__dirname, "lib", "filesize.es6.js"), "utf8"), |
| 83 | + minified = require("babel-core").transform(data, { |
| 84 | + sourceFileName: "filesize.es6.js", |
| 85 | + sourceMaps: true, |
| 86 | + presets: ["minify"] |
| 87 | + }), |
| 88 | + pkg = require(path.join(__dirname, "package.json")), |
| 89 | + banner = "/*\n " + new Date().getFullYear() + " " + pkg.author + "\n @version " + pkg.version + "\n*/\n\"use strict\";"; |
| 90 | + |
| 91 | + fs.writeFileSync(path.join(__dirname, "lib", "filesize.es6.min.js"), banner + minified.code + "\n//# sourceMappingURL=filesize.es6.min.js.map", "utf8"); |
| 92 | + grunt.log.ok("1 file created."); |
| 93 | + fs.writeFileSync(path.join(__dirname, "lib", "filesize.es6.min.js.map"), JSON.stringify(minified.map), "utf8"); |
| 94 | + grunt.log.ok("1 sourcemap created."); |
| 95 | + }); |
| 96 | + |
| 97 | + // aliases |
| 98 | + grunt.registerTask("test", ["eslint", "nodeunit"]); |
| 99 | + grunt.registerTask("build", ["concat", "babel"]); |
| 100 | + grunt.registerTask("default", ["build", "test", "babili", "uglify"]); |
| 101 | +}; |
0 commit comments