Save 37% off PRO during our Black Friday Sale! »

gulpをv3系から v4系にアップデートした話

gulpをv3系から v4系にアップデートした話

update gulp v3 => v4

199eea10363d8ce292adf0d3124704b9?s=128

nagasawaaaa

October 26, 2019
Tweet

Transcript

  1. HVMQΛWܥ͔Β WܥʹΞοϓσʔτͨ͠࿩ ௕ᖒݡʢ!OBHBTBXBBBBʣ ΏΔ8FCษڧձ!ࡳຈ

  2. ࣗݾ঺հ w ໊લɿ௕ᖒݡʢͳ͕͞Θ͞ͱ͠ʣ w 5XJUUFSɿ!OBHBTBXBBBB w ॴଐɿגࣜձࣾύϯηΤϯδχΞ w ׆ಈɿ+48PSLTIPQ4BQQPSPʢ+BWB4DSJQUͷษڧձʣ
 7VFKT/VYUKT΋͘΋͘ձ

    ΏΔ8FCษڧձ!ࡳຈ
  3. ࠓ೔࿩͢͜ͱ w HVMQWʹͳͬͯศརͩͳͱࢥͬͨ௥Ճػೳͷ঺հ w HVMQWHVMQWͷॻ͖ํͷҧ͍ ΏΔ8FCษڧձ!ࡳຈ

  4. ຊ೔ͷαϯϓϧίʔυ IUUQTHJUIVCDPNOBHBTBXBBBBHVMQWUPW ΏΔ8FCษڧձ!ࡳຈ

  5. ͦ΋ͦ΋HVMQͱ͸ʁ ΏΔ8FCษڧձ!ࡳຈ

  6. ݱ৔Ͱ࢖͑ΔHVMQೖ໳ୈճHVMQͱ͸Կ͔c$PEF(SJE IUUQTBQQDPEFHSJEOFUFOUSZHVMQ lHVMQ͸/PEFKTΛϕʔεͱͨ͠ϏϧυγεςϜϔϧύʔ Ͱ͢ɻҎલ$PEF(SJEͰ΋঺հͨ͠(SVOUͱࣅͨ໨తΛ ࣋ͬͯ࡞ΒΕͨπʔϧͰɺHVMQΛ࢖͑͹͞·͟·ͳ࡞ ۀΛࣗಈԽ͢Δ͜ͱ͕Ͱ͖·͢ɻz ΏΔ8FCษڧձ!ࡳຈ

  7. HVMQͰग़དྷΔ͜ͱ w FKTɺQVHɺ/VOKVDLTͳͲͷ)5.-ςϯϓϨʔτΤϯδϯΛ࢖ͬͨIUNMͷϏϧυ w 4BTTɺ4UZMVTͳͲͷ"MU$44ͷίϯύΠϧ w ίϯύΠϧ࣌ʹ"VUPQSFpYFSͰϕϯμʔϓϨϑΟοΫεΛࣗಈ෇༩ w ը૾ͷѹॖ w

    8FCQBDLΛ࢖ͬͯ+4Λόϯυϧ w CBCFMΛ࢖ͬͯ+4ΛτϥϯεύΠϧ w 1SFUUJFSͰ+4Λίʔυ੔ܗ w ϒϥ΢βͷࣗಈϦϩʔυʢ#SPXTFS4ZODͳͲʣ w ͦͷଞ ΏΔ8FCษڧձ!ࡳຈ
  8. ൿ఻ͷλϨͱԽ͢HVMQpMFKT ΏΔ8FCษڧձ!ࡳຈ

  9. ൿ఻ͷλϨԽ͢ΔݪҼ w HVMQpMFKT͸୭͔͕࡞ͬͨͷΛଞͷϓϩδΣΫτ͔Βͷ࢖͍ճ͕ͪ͠ w ৽ೖࣾһ΋ઌഐ͕࢖ͬͯΔͷΛงғؾͰ࢖͍ͬͯΔ w OQNJͯ͠OQNTUBSUୟ͚͹ͱΓ͋͑ͣಈ͔͘Βʙతͳʜ w Ξοϓσʔτ͠ͳͯ͘΋ຬ଍͍ͯ͠Δ w

    ͦ΋ͦ΋࡞ͬͨਓ͕ୀ৬ͯ͠ϝϯςφϯε͢Δਓ͕͍ͳ͘ͳͬͨ ΏΔ8FCษڧձ!ࡳຈ
  10. ͦͷ݁Ռ ΏΔ8FCษڧձ!ࡳຈ

  11. Wܥͷ··ࠓࢸΔ ˞ઌఔͷݪҼʹهࡌͨ͠Ұྫ͸ฐࣾΛྫʹͨ͠಺༰Ͱ͸͋Γ·ͤΜ WFSTJPO͕Wܥͷ࠷ޙ ΏΔ8FCษڧձ!ࡳຈ

  12. ͦͯ͠HVMQW͕೥݄ʹ ϦϦʔε͞Εͨ΋ͷͷ ۀ຿͕๩͗ͯ͢͠εϧʔ ΏΔ8FCษڧձ!ࡳຈ

  13. ๭/VYUԌ্ϓϩδΣΫτʹΑΓ ೥Ҏ্ଟ๩ͷ೔ʑΛૹ͍͕ͬͯͨɺ ϦϦʔεΛऴ͑ͯۀ຿ʹ༨༟͕ग़Δ ΏΔ8FCษڧձ!ࡳຈ

  14. ༨༟Ͱ͖ͨ͠ ϑϩϯτܥʢओʹ੩తαΠτ༻ʣͷ ςϯϓϨʔτΛߋ৽͢Δͧ ΏΔ8FCษڧձ!ࡳຈ

  15. WWͰେ͖͘มΘͬͨͱ͜Ζ ΏΔ8FCษڧձ!ࡳຈ

  16. JNQPSUFYQPSUߏจΛ࢖͑ΔΑ͏ʹͳͬͨ w FTN˞ϞδϡʔϧΛΠϯετʔϧ͢Δ w ϑΝΠϧ໊ΛHVMQpMFKTHVMQpMFFTNKTʹมߋ͢Δ CBCFMΛೖΕͨΓɺ5ZQF4DSJQUͰ΋ॻ͚·͕͢ɺ ࠷ۙͷ/PEFKTʢWҎ߱ͱ͔ʜ ʣ͸ͦΕΒ͕ఏڙ͢ΔߏจΛ αϙʔτ͍ͯ͠ΔͷͰFTNΛಋೖ͢Δ͚ͩͰྑ͍ͱݸਓతʹ͸ࢥ͍·͢ɻ ͨͩ͠ɺHVMQpMFKTΛܕ෇͖ݴޠͰॻ͘ͷͰ͋Ε͹

    5ZQF4DSJQUΛಋೖ͢Δͷ΋ΞϦͰ͢ɻ ΏΔ8FCษڧձ!ࡳຈ ˞FTN͸CBCFMෆཁɺόϯυϧෆཁͷ&4Ϟδϡʔϧϩʔμʔ ˞WܥͰ΋FTNೖΕͨΒग़དྷ͔ͨ΋͠Ε·ͤΜ͕ະݕূʜ
  17. ໊લΛมߋ͢Δ͚ͩͰHVMQ͕ Α͠ͳʹ΍ͬͯ͘ΕΔͷͰHVMQpMFFTNKTʹ FTNΛSFRVJSFPSJNQPSU͢Δඞཁ͸ͳ͍Ͱ͢ ΏΔ8FCษڧձ!ࡳຈ

  18. ͪͳΈʹ ΏΔ8FCษڧձ!ࡳຈ

  19. CBCFMΛ࢖ͬͯHVMQpMFKTΛॻ͜͏ͱ͢Δͱʁ w ϑΝΠϧ໊ΛHVMQpMFCBCFMKTʹมߋ͢Δ w ࣍ͷϞδϡʔϧΛΠϯετʔϧ͢Δ w !CBCFMDPSF !CBCFMSFHJTUFS !CBCFMQPMZpMM !CBCFMDPSFKT

    w CBCFMSD˞Λ࡞੒͠ઃఆΛ௥Ճ JNPSUFYQPSUͱBTZODBXBJU࢖͏৔߹΋όϕΔ˞৔߹͸ख͕͔͔ؒΔɻ HVMQެࣜυΩϡϝϯτʹ͸HVMQpMFCBCFMKTʹมߋͯ͠!CBCFMSFHJTUFSೖΕΖͱ͍͏ ͜ͱ͚ͩॻ͍ͯ͋Δɻ ΏΔ8FCษڧձ!ࡳຈ ˞CBCFMΛ࢖ͬͯτϥϯεύΠϧ͢Δ͜ͱ ˞CBCFMͷઃఆϑΝΠϧ
  20. 5ZQF4DSJQU͸ͦ΋ͦ΋ࢲ͕ ීஈۀ຿Ͱॻ͍͍ͯͳ͍ͷͰ ࠓճ͸ׂѪʜ ΏΔ8FCษڧձ!ࡳຈ

  21. JNPSUFYQPSUߏจWWൺֱ const gulp = require('gulp'); const runSequence = require('run-sequence'); gulp.task('css',

    () => { console.log('css'); }); gulp.task('js', () => { console.log('js'); }); gulp.task('imagemin', () => { console.log('imagemin'); }); gulp.task('default', cb => { return runSequence('imagemin', ['css', 'js'], cb); }); WHVMQpMFKT import gulp from 'gulp'; async function css () { console.log('css'); } async function js () { console.log('js'); } async function imagemin () { console.log('imagemin'); } const build = gulp.parallel(css, js, imagemin); export default build; WHVMQpMFFTNKT ΏΔ8FCษڧձ!ࡳຈ ˞ॻ͖ํͷࢀߟͰ͋ΓɺλεΫͷத਎͸ద౰Ͱ͢ɻ
  22. λεΫͷ࡞੒ํ๏͕γϯϓϧʹͳͬͨ w HVMQUBTL ͰλεΫΛొ࿥͠ͳͯ͘΋େৎ෉ʹͳͬͨ w ؔ਺ͷฦΓ஋Λ1SPNJTFʹ͢ΔɺTUFSBNΛฦ͢ɺίʔϧόοΫؔ ਺Λ౉ͯؔ͠਺಺ͰൃՐͤ͞Ε͹HVMQͷλεΫʹͳΔ˞ ΏΔ8FCษڧձ!ࡳຈ ˞ଞʹ΋͋Γ·͕͢ৄ͘͠͸υΩϡϝϯτΛࢀর͍ͩ͘͞
 ɹɹIUUQTHVMQKTDPNEPDTFOHFUUJOHTUBSUFEBTZODDPNQMFUJPO

  23. λεΫͷ࡞੒ํ๏WWൺֱ ΏΔ8FCษڧձ!ࡳຈ WHVMQpMFFTNKT import { src, dest, parallel } from

    'gulp'; // steram を返す従来に似たパターン function streamCss () { return src('src/**/*.css') .pipe(dest('public_html')); } // Promiseを返すパターン1 async function asyncJs () { console.log('js'); } // Promiseを返すパターン2 function promiseImagemin () { console.log('imagemin'); return Promise.resolve('resolveの値は無視される') } // 何も返さない代わりにError-first callbacksを発火させるパターン function callbackHtml (cb) { console.log('html'); cb(); } const build = parallel(streamCss, asyncJs, callbackHtml, promiseImagemin); export default build; WHVMQpMFKT const gulp = require('gulp'); const imagemin = require('gulp-imagemin'); const runSequence = require('run-sequence'); gulp.task('css', () => { return gulp.src('src/**/*.css') .pipe(gulp.dest('public_html')); }); gulp.task('js', () => { return gulp.src('src/**/*.js') .pipe(gulp.dest('public_html')); }); gulp.task('imagemin', () => { return gulp.src('src/**/*.{gif,jpg,png,svg}') .pipe(imagemin()) .pipe(gulp.dest('public_html')); }); gulp.task('default', cb => { return runSequence('imagemin', ['css', 'js'], cb); }); ˞ॻ͖ํͷࢀߟͰ͋ΓɺλεΫͷத਎͸ద౰Ͱ͢ɻ
  24. ύϒϦοΫλεΫͱϓϥΠϕʔτλεΫʹ ෼͚Δ͜ͱ͕ग़དྷΔΑ͏ʹͳͬͨ w HVMQpMFKTͰFYQPSU͠ͳ͚Ε͹ϓϥΠϕʔτλεΫʹͳΔ w ϓϥΠϕʔτλεΫ͸ΤϯυϢʔβʔ͕࣮ߦͰ͖ͳ͍ w ύϒϦοΫʹొ࿥͢Δʹ͸HVMQpMFKTͰFYQPSU͢Δ ΏΔ8FCษڧձ!ࡳຈ ࢀߟɿIUUQTHVMQKTDPNEPDTFOHFUUJOHTUBSUFEDSFBUJOHUBTLT

  25. ύϒϦοΫλεΫͱϓϥΠϕʔτλεΫ ΏΔ8FCษڧձ!ࡳຈ import { series, parallel } from 'gulp'; import

    clean from './tasks/clean'; // プライベート async function css () { console.log('css'); } // プライベート async function js () { console.log('js'); } // プライベート async function imagemin () { console.log('imagemin'); } // パブリック const build = series( clean, parallel(css, js, imagemin) ); // exportすることによってコマンドラインから使えるようになります export default build; export const cleanDir = clean; ˞ॻ͖ํͷࢀߟͰ͋ΓɺλεΫͷத਎͸ద౰Ͱ͢ɻ ˞QBDLBHFKTPOͷTDSJQUTʹHVMQΛ௥Ճ͍ͯ͠Δ૝ఆͷ࿩Ͱ͢ɻ ӈͷΑ͏ͳHVMQGVMFFTNKTͷ৔߹ɺ FYQPSU͍ͯ͠ΔCVJME σϑΥϧτλεΫ ͱ DMFBO%JSͱ͍͏λεΫ͕ίϚϯυϥΠϯ͔Βୟ͘͜ ͱ͕Ͱ͖ΔύϒϦοΫλεΫͱͯ͠ొ࿥͞Ε·͢ɻ ͦΕҎ֎ͷDTTɺKTɺJNBHFNJOͳͲ͸௚઀ FYQPSU͍ͯ͠ͳ͍ͨΊɺ࣍ͷΑ͏ͳίϚϯυ͕௨Γ ·ͤΜɻ $ npm run gulp $ npm run gulp css Task never defined: css // エラーとなる $ npm run gulp cleanDir
  26. ௚ྻॲཧɺฒྻॲཧʹ SVOTFRVFODF͕ෆཁʹͳͬͨ w ௚ྻॲཧ͢ΔTFSJFT ϝιου͕௥Ճ͞Εͨ w ฒྻॲཧ͢ΔQBSBMMFM ϝιου͕௥Ճ͞Εͨ ΏΔ8FCษڧձ!ࡳຈ ࢀߟɿIUUQTHVMQKTDPNEPDTFOBQJTFSJFT

    IUUQTHVMQKTDPNEPDTFOBQJQBSBMMFM
  27. QBSBMMFM TFSJFT ΏΔ8FCษڧձ!ࡳຈ ˞ॻ͖ํͷࢀߟͰ͋ΓɺλεΫͷத਎͸ద౰Ͱ͢ɻ WHVMQpMFFTNKT import { series, parallel }

    from 'gulp'; async function css () { console.log('css'); } async function js () { console.log('js'); } async function imagemin () { console.log('imagemin'); } const build = series( imagemin, parallel(css, js) ); export default build; WHVMQpMFKT const gulp = require('gulp'); const imagemin = require('gulp-imagemin'); const runSequence = require('run-sequence'); gulp.task('css', () => { return gulp.src('src/**/*.css') .pipe(gulp.dest('public_html')); }); gulp.task('js', () => { return gulp.src('src/**/*.js') .pipe(gulp.dest('public_html')); }); gulp.task('imagemin', () => { return gulp.src('src/**/*.{gif,jpg,png,svg}') .pipe(imagemin()) .pipe(gulp.dest('public_html')); }); gulp.task('default', cb => { return runSequence('imagemin', ['css', 'js'], cb); });
  28. ιʔεϚοϓͷग़ྗʹϓϥάΠϯ͕ෆཁʹͳͬͨ ΏΔ8FCษڧձ!ࡳຈ ˞ॻ͖ํͷࢀߟͰ͋ΓɺλεΫͷத਎͸ద౰Ͱ͢ɻ WHVMQpMFFTNKT import { src, dest } from

    'gulp'; import sass from 'gulp-sass'; async function scss () { return src('src/**/*.scss', { base: 'src', sourcemaps: true }) .pipe(sass()) .pipe(dest('public_html', { sourcemaps: './' })); } export const buildSass = scss; WHVMQpMFKT const gulp = require('gulp'); const sass = require('gulp-sass'); const sourcemaps = require('gulp-sourcemaps'); gulp.task('sass', () => { return gulp.src('src/**/*.scss', { base: 'src' }) .pipe(sourcemaps.init()) .pipe(sass()) .pipe(sourcemaps.write('./')) .pipe(gulp.dest('public_html')); }); WܥͰ͸TSDϝιουͱEFTUϝιουͷୈҾ਺ͷΦϓγϣϯʹ TPVSDFNBQTϓϩύςΟΛ౉ͤΔΑ͏ʹͳͬͨɻ USVFʹઃఆ͢ΔͱJOMJOFͰग़ྗ͞Εɺग़ྗ͢ΔύεΛจࣈྻͰઃఆ͢Δͱ֎෦ϑΝΠϧͰग़ྗ͞ΕΔɻ ࢀߟɿIUUQTHVMQKTDPNEPDTFOBQJTSDTPVSDFNBQT
  29. ଞʹ΋͋Δ͚Ͳৄ͘͠͸ ެࣜυΩϡϝϯτΛݟͯͶ IUUQTHVMQKTDPNEPDTFOBQJDPODFQUT

  30. ·ͱΊ

  31. ·ͱΊ w πʔϧ͸࢖͍෼͚͕େࣄɻHVMQ΋πʔϧͷ̍ͭɻ
 HVMQ͕ಘҙͳ͜ͱ͸HVMQʹɻ
 XFCQBDL͕ಘҙͳ͜ͱ͸XFCQBDLʹ΍ΒͤΔɻ w Ϟμϯͳ+4Ͱॻ͚ͯɺࠓ·ͰϓϥάΠϯͰิ͍ͬͯͨػೳ͕
 ௥Ճ͞ΕͨHVMQWʹΞοϓσʔτ͠Α͏ɻ w 1BSDFM͸ಋೖ΋؆୯Ͱૉ੖Β͍͠πʔϧ͚ͩͲɺ


    8&#αΠτ੍࡞ք۾Ͱ͸HVMQ͸·ͩ·ͩݱ໾ɻʢͷɺ͸ͣʜʣ
  32. ͝ਗ਼ௌ ͋Γ͕ͱ͏͍͟͝·ͨ͠