{"id":277,"date":"2015-10-01T21:04:49","date_gmt":"2015-10-01T13:04:49","guid":{"rendered":"https:\/\/blog.kotori.moe\/?p=277"},"modified":"2015-10-01T21:04:49","modified_gmt":"2015-10-01T13:04:49","slug":"%e4%bd%bf%e7%94%a8gruntbabel%e8%bf%9b%e8%a1%8ces6%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/sora.sound.moe\/?p=277","title":{"rendered":"\u4f7f\u7528Grunt+Babel\u8fdb\u884cES6\u5f00\u53d1"},"content":{"rendered":"<p>\u6700\u8fd1\u6253\u7b97\u91cd\u6784\u4e00\u4e0bWikiplus\uff08\u867d\u7136\u5e94\u8be5\u4f1a\u5728\u9ad8\u8003\u540e\u8fdb\u884c\uff09\uff0c\u53c8\u4e00\u76f4\u5bf9ES6\u7684\u67d0\u4e9b\u8bed\u6cd5\u2026\u2026\u6bd4\u5982Class\u8fd9\u79cd\u751c\u751c\u7684\u8bed\u6cd5\u7cd6\u2026\u2026\u5782\u6d8e\u5df2\u4e45\uff0c\u4e8e\u662f\u4eca\u5929\u5c31\u6784\u5efa\u4e86\u4e00\u4e0b\u73af\u5883\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u9996\u5148\uff0c\u5fc5\u987b\u51c6\u5907Node.js\u73af\u5883\uff0cGrunt\u4e5f\u662f\u57fa\u4e8eNode.js\u7684\u3002Babel\u4e5f\u9700\u8981\u901a\u8fc7Node.js\u7684\u5305\u7ba1\u7406\u5668npm\u6765\u5b89\u88c5\u3002<\/p>\n<p>\u5982\u4f55\u5b89\u88c5Node.js\u4e0d\u8bb2\u4e86\uff0c\u8fd9\u91cc\u6709\u4e2a\u5c0f\u70b9\uff0c\u5982\u679c\u5728\u5899\u5185\u4f7f\u7528npm\u6709\u56f0\u96be\uff0c\u53ef\u4ee5\u7528\u6dd8\u5b9d\u63d0\u4f9b\u7684cnpm\u955c\u50cf\u3002<\/p>\n<p>\u9996\u5148\u5728\u9879\u76ee\u76ee\u5f55\u5efa\u7acbnpm\u7528\u7684package.json\uff0c\u6211\u7684\u5927\u6982\u662f\u8fd9\u6837\u7684<\/p>\n<pre class=\"lang:default decode:true\">{\n  \"name\": \"Wikiplus\",\n  \"version\": \"0.1.0\",\n  \"description\": \"A front-end jQuery-based Mediawiki plugin.\",\n  \"license\": \"Apache\",\n  \"devDependencies\": {\n    \"grunt\": \"~0.4.1\", \/\/grunt\u672c\u4f53\n    \"grunt-babel\": \"^5.0.3\",\n    \"grunt-contrib-uglify\": \"~0.2.1\", \/\/js\u538b\u7f29\u7528\u7684\n    \"grunt-contrib-watch\": \"^0.6.1\", \/\/grunt\u76d1\u89c6\u6587\u4ef6\u53d8\u52a8\u7684\n    \"load-grunt-tasks\": \"^3.3.0\" \/\/grunt\u4efb\u52a1\u8f7d\u5165\u7528\u7684\n  }\n}\n<\/pre>\n<p>\u7136\u540e\u5728\u8fd9\u4e2a\u76ee\u5f55\u6267\u884cnpm install\uff0cnpm\u5c31\u4f1a\u5e2e\u4f60\u88c5\u597d\u6240\u6709\u9700\u8981\u7684\u5305\u3002<\/p>\n<p>\u7136\u540e\u5728\u8fd9\u4e2a\u76ee\u5f55\u5efa\u7acbGruntfile.js\uff0c\u6765\u5b9a\u4e49grunt\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u6211\u7684\u5927\u6982\u957f\u8fd9\u6837<\/p>\n<pre class=\"lang:js decode:true \">module.exports = function (grunt) {\n    require('load-grunt-tasks')(grunt);\n    grunt.initConfig({\n        uglify: {\n            options: {\n            },\n            app_task: {\n                files: {\n                    '.\/Main.min.js': '.\/Main.js'\n                }\n            }\n        },\n        babel: {\n            options: {\n                sourceMap: true\n            },\n            dist: {\n                files: {\n                    '.\/Main.js': '.\/Main.new.js'\n                }\n            }\n        },\n        watch: {\n            another: {\n                files: ['.\/Main.new.js'],\n                tasks: ['babel','uglify'], \/\/ \u5148babel\u7ffb\u8bd1\uff0c\u518d\u538b\u7f29\n                options: {\n                    \/\/ Start another live reload server on port 1337\n                    livereload: 1337\n                }\n            }\n        }\n    });\n    grunt.loadNpmTasks('grunt-contrib-uglify');\n    grunt.loadNpmTasks('grunt-contrib-watch');\n    grunt.registerTask('default', ['watch']); \/\/\u5b9a\u4e49\u9ed8\u8ba4\u6267\u884c\u7684\u4efb\u52a1\n}<\/pre>\n<p>\u7136\u540e\u6267\u884cgrunt\u5c31\u884c\u4e86\u2026\u2026grunt\u5c31\u53ef\u4ee5\u76d1\u89c6\u4f60\u7684\u6587\u4ef6\u53d8\u52a8\uff0c\u6bcf\u5f53\u6211\u4fdd\u5b58Main.new.js\u7684\u65f6\u5019\uff0c\u4f1a\u628a\u5b83\u7528Babel\u8f6c\u8bd1\u5b58\u5230Main.js\uff0c\u518d\u7528uglifyJS\u538b\u7f29\u5b58\u5230Main.min.js\u3002\u5de5\u4f5c\u91cf\u4e00\u4e0b\u5b50\u5c31\u5c0f\u4e86\u597d\u591a\uff01\u518d\u4e5f\u4e0d\u7528\u624b\u5de5\u6267\u884c\u8fd9\u4e2a\u90a3\u4e2a\u4e86\u2026\u2026\u597d\u68d2&gt;.&lt;<\/p>\n<p>&nbsp;<\/p>\n<p>\u987a\u4fbf\u6211\u6700\u8fd1\u5df2\u7ecf\u629b\u5f03\u4e86Sublime\u6539\u7528Visual Studio Code\u3002\u611f\u89c9VSC\u66f4\u5389\u5bb3\u4e5f\u66f4\u597d\u7528\u4e00\u4e9b\u3002\u4e0d\u8fc7VSC\u5bf9ES6\u9ed8\u8ba4\u662f\u4f1a\u5212\u7ea2\u7ebf\u7684\uff0c\u9700\u8981\u5728\u6587\u4ef6\u5939\u4e0b\u5efa\u7acb\u4e00\u4e2ajsconfig.json\uff0c\u5185\u5bb9\u5982\u4e0b<\/p>\n<pre class=\"lang:js decode:true \">{\n\t\"compilerOptions\": {\n\t\t\"target\": \"ES6\"\n\t},\n\t\"files\": [\n\t\t\"Main.new.js\"\n\t]\n}<\/pre>\n<p>\u6765\u907f\u514d\u5bf9ES6\u7684\u62a5\u9519\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u6253\u7b97\u91cd\u6784\u4e00\u4e0bWikiplus\uff08\u867d\u7136\u5e94\u8be5\u4f1a\u5728\u9ad8\u8003\u540e\u8fdb\u884c\uff09\uff0c\u53c8\u4e00\u76f4\u5bf9ES6\u7684\u67d0\u4e9b\u8bed\u6cd5\u2026\u2026\u6bd4\u5982Class\u8fd9\u79cd\u751c\u751c &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/sora.sound.moe\/?p=277\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u4f7f\u7528Grunt+Babel\u8fdb\u884cES6\u5f00\u53d1\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"twentyseventeen-featured-image":false,"twentyseventeen-thumbnail-avatar":false},"uagb_author_info":{"display_name":"Sora","author_link":"https:\/\/sora.sound.moe\/?author=1"},"uagb_comment_info":4,"uagb_excerpt":"\u6700\u8fd1\u6253\u7b97\u91cd\u6784\u4e00\u4e0bWikiplus\uff08\u867d\u7136\u5e94\u8be5\u4f1a\u5728\u9ad8\u8003\u540e\u8fdb\u884c\uff09\uff0c\u53c8\u4e00\u76f4\u5bf9ES6\u7684\u67d0\u4e9b\u8bed\u6cd5\u2026\u2026\u6bd4\u5982Class\u8fd9\u79cd\u751c\u751c&hellip;","_links":{"self":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts\/277"}],"collection":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=277"}],"version-history":[{"count":0,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=\/wp\/v2\/posts\/277\/revisions"}],"wp:attachment":[{"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sora.sound.moe\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}