Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ビルドプロセスの自動化で効率アップ

Ayumu Sato
November 10, 2012

 ビルドプロセスの自動化で効率アップ

Grunt.jsの紹介スライドです。package.jsonの扱いや、grunt自身の0.4.0対応が弱い部分が残りますが、数ヶ月前というところでご容赦くだされば、おおまかな概要を示しています。

Ayumu Sato

November 10, 2012
Tweet

More Decks by Ayumu Sato

Other Decks in Programming

Transcript

  1. ͞·͟·ͳϏϧυ 6 $44 ϓϦϓϩηοα $PNQJMF $PGGFF4DSJQUͳͲ BMU+4ͷ$PNQJMF ϑΝΠϧͷ݁߹ $PODBUFOBUF ը૾ͷ

    ϩεϨεѹॖ $PNQSFTTJPO %BUB63*ม׵ *OMJOF&NCFE ίʔυ࠷খԽ .JOJGZ 0QUJNJ[F
  2. 7 υΩϡϝϯτ ੜ੒ Ωϟογϡ ϚχϑΣετ ؅ཧ +BWB4DSJQUͷ ςετ࣮ߦ ؀ڥʹϑΝΠϧ Ξοϓϩʔυ

    $44 ϓϦϓϩηοα $PNQJMF $PGGFF4DSJQUͳͲ BMU+4ͷ$PNQJMF ϑΝΠϧͷ݁߹ $PODBUFOBUF ը૾ͷ ϩεϨεѹॖ $PNQSFTTJPO %BUB63*ม׵ *OMJOF&NCFE ίʔυ࠷খԽ .JOJGZ 0QUJNJ[F
  3. 5BTL#BTFE#VJME5PPM 23 OPEF+4 (SVOU 5BTL 5BTL 5BTL ίϚϯυϥΠϯͰ ࣮ߦ 5BTLͷ

    ࣮ߦ݁ՌΛಘΔ ‣֤छίϯύΠϧͷ࣮ߦ ‣ॲཧࡁΈϑΝΠϧͷ࡞੒ ‣ͳͲͳͲ
  4. +4ͱ$44ͷ݁߹ઃఆ 36 grunt.initConfig({ concat: { js: { src: [ 'js/lib/jquery.js',

    'js/app/main.js', 'js/app/sub.js' ], dest: 'js/all.js' }, css: { src: ['css/main.css', 'css/sub.css'], dest: 'css/all.css' } }, } HSVOUKT
  5. +BWB4DSJQUͷϏϧυઃఆ module.exports = function(grunt) { grunt.initConfig({ concat: { dist: {

    src: ['js/jquery.js', 'js/app/main.js', 'js/app/sub.js'], dest: 'js/all.js' } }, min: { dist: { src: 'js/all.js', dest: 'js/all.min.js' } } }); grunt.registerTask('jsbuild', ['concat', 'min']); }; 38 HSVOUKT
  6. 40 AHSVOUKTCVJMEAͷ࣮ߦ݁Ռ % grunt jsbuild # concat λεΫ͕ߦΘΕͯɺjs/all.js ͕ੜ੒͞Εͨ Running

    "concat:dist" (concat) task File "js/all.js" created. # ͞Βʹ min λεΫͰɺѹॖ͕ߦΘΕͨ Running "min:dist" (min) task File "js/all.min.js" created. Uncompressed size: 56545 bytes. Compressed size: # ͓͠·͍ Done, without errors. ࠇ͍ը໘
  7. ϑΝΠϧͷߋ৽ݕ஌ͱΞΫγϣϯ watch: { css: { // ͜ΕΒͷϑΝΠϧ͕ߋ৽͞ΕͨΒ files: ['css/*.less'], //

    ҎԼͷλεΫΛ࣮ߦ͢Δ tasks: 'less' }, js: { files: ['js/app/*.js', 'index.html'], tasks: 'lint concat min' } } 42 HSVOUKT
  8. 43 AHSVOUXBUDIAͷ࣮ߦ݁Ռ % grunt watch # ϑΝΠϧͷߋ৽؂ࢹΛ։࢝ Running "watch" task

    Waiting...OK # ݕ஌ʂ File "css/main.less" changed. # lessλεΫ͕࣮ߦ͞ΕͯɺίϯύΠϧ Running "less:dist" (less) task File css/main.css created. ࠇ͍ը໘
  9. 45 ։ൃ؀ڥͱͯ͠૊Έ߹Θͤͨઃఆ grunt.initConfig({ /*ʢલུɾ͜Ε·Ͱͷwatch΍֤λεΫͷઃఆʣ*/ server: { port: 8000, // ੩తϩʔΧϧαʔόΛཱͯΔϙʔτ

    base: '.' }, reload: { port: 35729 // LiveReloadαʔόͷϙʔτ }, }); // develop λεΫͱͯ͠ෳ߹ grunt.registerTask( 'develop', ['server', 'reload', 'watch'] ); HSVOUKT
  10. 46 AHSVOUXBUDIAͷ࣮ߦ݁Ռ % grunt devlop # localhost:8000Λىಈ Running "server" task

    Starting static web server on port 8000. # LiveReloadαʔόʔΛىಈ Running "reload" task Proxying http://localhost:8000/ reload server running at http://localhost:35729 # ϑΝΠϧͷߋ৽Λ։࢝ Running "watch" task Waiting.. ࠇ͍ը໘
  11. ϓϥάΠϯΛΠϯετʔϧ 53 # ݟ͚ͭͨϓϥάΠϯΛnpmͰΠϯετʔϧ % npm install grunt-data-uri npm http

    GET https://registry.npmjs.org/grunt-data-uri npm http 200 https://registry.npmjs.org/grunt-data-uri npm http GET https://registry.npmjs.org/mime npm http 200 https://registry.npmjs.org/mime [email protected] node_modules/grunt-data-uri !"" [email protected] ࠇ͍ը໘
  12. // ͜ΜͳJSONΛίϐϖͯ͠ϑΝΠϧ࡞੒ { "name": "ABCϓϩδΣΫτ", "description": "ϓϩδΣΫτ։ൃ؀ڥͷఆٛͰ͢", "version": "1.0.0", "author":

    "࠽৔ɹଠ࿠", "main": "grunt.js", "dependencies": {}, "optionalDependencies": { "grunt": "~0.3.16" } } 57 QBDLBHFKTPOΛ࡞੒ QBDLBHFKTPO
  13. QBDLBHFKTPOʹอଘ # install --saveͰ # ϓϥάΠϯͷΠϯετʔϧ࣌ʹɺpackage.jsonͷґଘఆٛʹ௥Ճ % npm install --save

    grunt-xxxxx # remove --saveͰ # ΞϯΠϯετʔϧͱಉ࣌ʹɺpackage.jsonͷґଘఆ͔ٛΒ࡟আ % npm remove --save grunt-xxxxx 58 ࠇ͍ը໘
  14. ࣗ࡞ϓϥάΠϯͷઃఆͱϩʔυ grunt.initConfig({ sample: { dist: { src: ['js/**/*.js'], // ೖྗݩ

    dest: 'js/all.js', // ग़ྗઌ option: { // ΦϓγϣϯͳͲʢ೚ҙʣ hoge: 'fuga', peke: 'piyo' } } } }); // grunt.jsϑΝΠϧ಺ʹهड़ // tasksσΟϨΫτϦͷதΛ͢΂ͯಡΈࠐΈ grunt.loadTasks('tasks'); 76 HSVOUKT
  15. UBTLTTBNQMFKT module.exports = function(grunt) { grunt.registerMultiTask('sample', 'own task', function() {

    var option = this.data.option, src = this.file.src, dest = this.file.dest, // ඇಉظॲཧ͕ඞཁͳͱ͖ʹ࢖༻ doneΛcallbackʹ͢Δ // var done = this.async(); // path/to/**/*.js Λղܾͯ֘͠౰ϑΝΠϧΛ഑ྻͰฦ͢ srcFiles = grunt.file.expandFiles(src); /*...λεΫͷॲཧ...*/ }); }; 77 TBNQMFKT