Share
Embed
Web Frontend ビルドツールの過去と現在と未来 / The Past, Present, and Future of Web Frontend Build Tools
やし
April 07, 2021
Transcript
61 slides
-
͢͜ͱ&͞ͳ͍͜ͱ • ͢͜ͱ • ϏϧυπʔϧͦΕͧΕͷత • Ͱ͖Δ͜ͱɺͰ͖ͳ͍͜ͱ • ͞ͳ͍͜ͱ •
ͦΕͧΕͷ͍ํࡉ͔͍ઃఆͳͲ -
ͬͨ͜ͱ • جຊతʹυΩϡϝϯτΛಡΉ • πʔϧͷతɺ࡞ΒΕͨܦҢɺίϯηϓτʹண • ։ൃڥ༻ͷઃఆͱຊ൪༻ͷઃఆΛͬͯΈΔ(ׂѪ 🙇) • →ϏϧυՌͷࡉ͔͍࠷దԽ͠ͳ͍
• create-react-app ͔ΒϏϧυπʔϧΛআ͍ͨΞϓϦ(ʁ)Λ࡞ͯ͠ྲྀ༻ -
ର • Browserify • webpack • Parcel • Rollup •
esbuild • Vite • Snowpack • SWC -
webpackΛௐͨͷͷ • ͳͥ webpack ͕ੜ·Εͨͷ͔ʁʹରͯ͠ͷ͕͑ݟ͔ͭΒͳ ͔ͬͨ • webpack ͕ੜ·Εͨ࣌ͷ web
frontend ͷঢ়گʹཱͪฦΔ • webpack ΑΓલʹ͋ͬͨ Browserify ΛௐͯΈΔ͜ͱʹ -
Browserifyͳͥੜ·Εͨͷ͔ • http://browserify.org/ Browserify lets you require('modules') in the browser
by bundling up all of your dependencies. • https://github.com/browserify/browserify require('modules') in the browser -
Browserifyͳͥੜ·Εͨͷ͔ • ࣌ Node.js Ͱॻ͚ͨ require ΛɺϒϥβͰಈ͔͢ js Ͱॻ ͚ΔΑ͏ʹ͍ͨ͠
• ϒϥβͰಈ͔͢ js ɺϞδϡʔϧׂͯ͠ॻ͖͍ͨ -
Browserifyͷ՝ • js Ҏ֎ͷϑΝΠϧ(css, png ͳͲ)ΛόϯυϧͰ͖ͳ͍ • → plugin Ͱ͓ͦΒ͘Մೳɺ͋͑ͯର֎ͱ͍ͯ͠Δͷ͔
• serve ػೳ͕ແ͍ • → gulp ͳͲͷλεΫϥϯφʔͱΈ߹Θͤͯ͏ඞཁ͕͋ͬ ͨ -
webpackͱ • https://webpack.js.org/concepts/ At its core, webpack is a static
module bundler for modern JavaScript applications. • https://webpack.js.org/concepts/#loaders Out of the box, webpack only understands JavaScript and JSON fi les. (→v5͔Β asset management ػೳʹΑΓɺը૾ϑΥϯτղऍՄೳʹ) -
webpackͱ • https://webpack.js.org/concepts/why-webpack/#wouldnt-it- be-nice (தུ)This is why webpack exists. It's
a tool that lets you bundle your JavaScript applications (supporting both ESM and CommonJS), and it can be extended to support many different assets such as images, fonts and stylesheets. -
webpackͳͥੜ·Εͨͷ͔ • a static module bundler for modern JavaScript applications
͕ ཉ͔ͬͨ͠ • → require Λ͑ΔΑ͏ʹ͢Δ͚ͩʹཹ·Βͳ͍ • (Loader ʹΑΓɺ) js Ҏ֎ͷ༷ʑͳܗࣜͷϑΝΠϧΛϏϧυՄೳ • Ұํɺdev server ʹؔ͢ΔݴٴݟͨΒͳ͔ͬͨ -
webpackͷ՝ • frontend application ͷڊେԽʹΑΔϏϧυͷԼ͕ݦஶ • webpack.con fi g.js ॻ͘ͷਏ͍(ͱݴΘΕ͍ͯΔ)
• →͍Ζ͍ΖͰ͖Δ͜ͱͷτϨʔυΦϑͰ͋Δ • όʔδϣϯΞοϓʹΑΓഁյతʹγϯλοΫε͕มΘΔ • ࡉ͔͍᠘(Loader ͷద༻ॱ͕ྻͷඌ͔Β)͕͋Δɺetc... -
Parcelͱ • https://parceljs.org/ • 2017~ • ઃఆෆཁ(ʂ)ͷϞδϡʔϧόϯυϥ • ֦ுࢠ͔ΒඞཁͳϥΠϒϥϦΛࣗಈͰΠϯετʔϧ •
.babelrc ͳͲΛࣗಈͰಡΈʹ͍͘ • v2 Λઈࢍ։ൃத -
Parcelͳͥੜ·Εͨͷ͔ • https://parceljs.org/ Blazing fast bundle times Parcel uses worker
processes to enable multicore compilation, and has a fi lesystem cache for fast rebuilds even after a restart. • ϕϯνϚʔΫࡌ͍ͤͯΔˠΛڧௐʁ -
Parcelͳͥੜ·Εͨͷ͔ • https://parceljs.org/ Parcel has out of the box support
for JS, CSS, HTML, fi le assets, and more - no plugins needed. Code is automatically transformed using Babel, PostCSS, and PostHTML when needed - even node_modules. • →ඪ४Ͱ js ͱ json ͔͠ղऍͰ͖ͳ͍ webpack ͱҟͳΔ -
Parcelͳͥੜ·Εͨͷ͔ • ϏϧυΛ͍ͨ͘͠ • → Worker process ͷར༻ɺΩϟογϡ • ϏϧυઃఆΛָʹ͍ͨ͠
• → Zero con fi guration -
Parcelͷ՝ • Zero con fi gurationɺͱ͍͑… • Production ༻ʹ࠷దԽΛֻ͚͍ͨ •
→ࡉ͔͍νϡʔχϯάͰ͖ͳ͍ • sandbox Ͱͷӡ༻ʹ͕͘ຊ൪ʹ࠾༻͠ʹ͍͘ҹ -
ࢀߟ: ECMAScript versions 7FS 0 ffi DJBM/BNF ֓ཁ &4 &$."4DSJQU
࠷ॳ &4 &$."4DSJQU &4 &$."4DSJQU &4 &$."4DSJQU "CBOEPOFE &4 &$."4DSJQU VTFTUSJDU "SSBZNBQ %BUFOPXͳͲ &4 &$."4DSJQU MFU DPOTU $MBTT 1SPNJTF &4NPEVMFT &$."4DSJQU "SSBZJODMVEFTͳͲ தུ &$."4DSJQU #JH*OU OVMMJTIDPBMFTDJOHͳͲ IEͷน -
Rollupͱ • https://rollupjs.org/guide/en/#overview It uses the new standardized format for
code modules included in the ES6 revision of JavaScript, instead of previous idiosyncratic solutions such as CommonJS and AMD. ES modules let you freely and seamlessly combine the most useful individual functions from your favorite libraries. • ES6 ج४ͳͷͰɺES modules ͕͑Δ • →ґଘؔͷղܾ͕͍͢͠ -
Rollupͱ • https://rollupjs.org/guide/en/#the-why Rollup allows you to write your code
using the new module system, and will then compile it back down to existing supported formats such as CommonJS modules, AMD modules, and IIFE-style scripts. • چܗࣜ(CommonJS ͳͲ)ʹͤΔ • https://rollupjs.org/guide/en/#compatibility Rollup can import existing CommonJS modules through a plugin. • (Plugin ʹΑΓɺ) CommonJS (چܗࣜͷϞδϡʔϧ)ͷΠϯϙʔταϙʔτ -
Rollupͳͥੜ·Εͨͷ͔ • (js ϑΝΠϧʹ͓͚Δ)ґଘؔͷղܾʹओ؟Λஔ͍͍ͯͦ͏ • ͜ͷʹ͓͍ͯ Browserify ͱྨࣅ͍ͯ͠Δ • ඪ४Λ
ES6 ʹͨ͜͠ͱͰɺΑΓΩϨΠʹґଘؔΛղܾ • ඪ४Ͱ js ͔͠ղऍͰ͖ͳ͍(plugin Ͱ֦ுՄೳ) • ͜ͷʹ͓͍ͯ webpack ͱྨࣅ͍ͯ͠Δ • ඪ४ͰͰ͖Δ͜ͱΛ੍ݶ֦ͭͭ͠ுੑΛߴΊΔํੑ -
esbuildͱ • https://esbuild.github.io/ • 2017(2020?)~ (ݱࡏ(2021)࣌Ͱβ൛) • Figma ࣾͷ CTO
͕ҰਓͰ։ൃ • ͱʹ͔͍͘ js όϯυϥ -
esbuildͳ͍ͥͷ͔ • https://esbuild.github.io/faq/#why-is-esbuild-fast It's written in Go and compiles to
native code. • →ࣄલίϯύΠϧ&ฒྻॲཧ͕ॻ͖͍͔͢Β Everything in esbuild is written from scratch. • →αʔυύʔςΟϥΠϒϥϦͷґଘ͕ແ͍ -
esbuildͳͥੜ·Εͨͷ͔ • https://esbuild.github.io/faq/#upcoming-roadmap I don't think esbuild should become an
all-in-one solution for all frontend needs. • https://esbuild.github.io/faq/#production-readiness Think of esbuild as a "linker" for the web. It knows how to transform and bundle JavaScript and CSS. But the details of how your source code ends up as plain JavaScript or CSS may need to be 3rd-party code. • →είʔϓΛݶఆ͓ͯ͠Γɺ͋ΒΏΔχʔζΛຬͨ͢πʔϧʹͳΖ͏ͱ͍ͯ͠ͳ͍ -
esbuildͳͥੜ·Εͨͷ͔ • https://esbuild.github.io/faq/#production-readiness I'm building esbuild because I fi nd
it fun to build and because it's the tool I'd want to use. • →ͱʹ͔͘Ϗϧυ(όϯυϧ)ͷ͍πʔϧ͕ཉ͔ͬͨ͠ • https://esbuild.github.io/faq/#upcoming-roadmap I also hope esbuild inspires other build tools to dramatically improve performance by overhauling their implementations so that everyone can bene fi t, not just those that use esbuild. • → esbuild ͷੜ͕ɺଞͷπʔϧͷύϑΥʔϚϯεվળͷ͖͔͚ͬʹͳͬͯ΄͍͠ -
esbuildͷ՝ • watch ͱ serve ྆ํͷαϙʔτ͕͋Δ͕ซ༻Ͱ͖ͳ͍ • hot reload ͷαϙʔτͳ͍
• plugin API ݱࡏ(2021)ະ࣮ • css ͷղऍݱࡏ(2021) WIP ͰରԠ • →·ͩ·ͩػೳ͕Γͯͳ͍ -
Viteͱ • https://vitejs.dev/ • 2020~ • Vue ͱಉ͡࡞ऀ • ։ൃڥͰ(΄΅)όϯυϧ͠ͳ͍
• ඪ४Ͱ ts, vue, jsx, css ͳͲΛαϙʔτ -
Viteͳͥੜ·Εͨͷ͔ • https://vitejs.dev/guide/why.html#the-problems However, as we start to build more
and more ambitious applications, the amount of JavaScript we are dealing with also increased exponentially. It is not uncommon for large scale projects to contain thousands of modules. We are starting to hit a performance bottleneck for JavaScript based tooling Vite aims to address these issues by leveraging new advancements in the ecosystem: the availability of native ES modules in the browser, and the rise of JavaScript tools written in compile-to-native languages. • →։ൃڥʹ͓͚ΔϏϧυͷԼΛɺES modules js Ͱͳ͍πʔϧͷར༻ʹΑͬͯղ ܾ͍ͨ͠ -
ViteͷΈ • ΞϓϦέʔγϣϯͷϞδϡʔϧΛʮdependenciesʯͱʮsource codeʯʹ͚Δ • ʮdependenciesʯ: plain js (ม͕ෆཁ)͔ͭߋ৽ස͕͍ •
→ esbuild Λ༻͍ͯࣄલόϯυϧ • ʮsource codeʯ: not plain js (ม͕ඞཁ)͔ͭߋ৽ස͕ߴ͍ • →όϯυϧͤͣɺES modules Λհͯ͠ϒϥβʹ serve (։ൃ࣌Ϟμϯϒϥβར༻લఏ) • →։ൃαʔόʔͷॳճىಈΛ͘͢Δ -
ViteͷΈ • ϑΝΠϧͷฤूΛؚΉ(ͦ͘͠ͷۙลͷ)ϞδϡʔϧͷΈΛߋ৽͢Δ • HTTP headers Λར༻͠ɺϦϩʔυ࣌ͷ࠶ಡΈࠐΈΛ࠷খݶʹ͢Δ • source code:
304 Not Modi fi ed • dependencies: Cache-Control: max-age=31536000,immutable • →։ൃαʔόʔͷ࠶ಡΈࠐΈΛ͘͢Δ -
ViteͷΈ • https://vitejs.dev/guide/why.html#why-bundle-for-production Even though native ESM is now widely
supported, shipping unbundled ESM in production is still inef fi cient (even with HTTP/2) due to the additional network round trips caused by nested imports. • ຊ൪ڥʹ͓͍ͯɺVite όϯυϧΛߦ͏ • Rollup Λར༻ • কདྷతʹ esbuild Λ͏Α͏ʹͳΔ͔ʁ -
Snowpackͱ • https://www.snowpack.dev/ • 2020~ • Pika ϓϩδΣΫτͷҰ෦ https://www.pika.dev •
͏Ұͭ Skypack CDN https://www.skypack.dev/ -
Snowpackͱ • ։ൃڥʹ͓͍ͯຊ൪ڥʹ͓͍ͯόϯυϧ͠ͳ͍ • → Vite ͱҟͳΔ • ඪ४Ͱ ts,
jsx, css ͳͲΛαϙʔτ • → Vite ͱಉ͡ -
Snowpackͱ • Unbundled development • ґଘؔΛอͬͨ··ɺݸʑͷϑΝΠϧΛผʑʹϒϥβʹ৴͢Δ • Ϗϧυͷ͞ɺΩϟογϡͷ͠қ͞ɺϓϩδΣΫτͷαΠζʹΑΔӨڹΛݮͰ ͖ΔͷϝϦοτΛಘΒΕΔ •
npm depencencies ʹؔͯ͠ಉ༷ • dev server ϒϥβ͔ΒͷϦΫΤετ͕͋ͬͨͱ͖ʹॳΊͯରϑΝΠϧΛϏ ϧυ͢Δ -
Snowpackͱ • ຊ൪ڥ༻ͷϏϧυʹ esbuild ͕ΘΕΔ • ҰԠ At the moment,
we only recommended this for smaller projects. ͱஅ͍ͬͯΔ • ࠷దԽ༻ͷϓϥάΠϯʹ webpack (ਪ)ͱ Rollup ͕͋Δ -
Snowpackͳͥੜ·Εͨͷ͔ • https://www.pika.dev/ We're on a mission to make the
web 90% faster. • όϯυϧ͕ཁΒͳ͍ੈքΛࢦ͢ • Snowpack: ϓϩμΫτίʔυͷόϯυϧΛແ͘͢ • Skypack CDN: npm dependencies ͷόϯυϧΛແ͘͢ -
SWCͱ • https://swc.rs/ • Speedy Web Compiler ͷུ • Rust
• js, ts ͷมΛ୲͏ • →Ϗϧυπʔϧͱ͍͏ΑΓ js ίϯύΠϥ(Babel ͱಉׂ͡) • spack ͱ͍͏όϯυϧπʔϧಉ͍ࠝͯ͠Δ -
SWCͳͥੜ·Εͨͷ͔ • https://swc.rs/blog/2020/09/08/swc-1.2.23/#performance- improvement swc becomes faster and it will
be much faster with the next version. Its performance is similar to it of esbuild, even though there are so many low-hanging fruits. • → esbuild Λҙ͍ࣝͯͦ͠͏ -
SWCͳͥੜ·Εͨͷ͔ • https://swc.rs/docs/spack-basic/#con fi g- fi le spack can be
con fi gured with spack.con fi g.js. Con fi g fi le for the spack is almost simillar to webpack. In future, I'll add a webpack-compatible plugin system. • → webpack Λҙ͍ࣝͯͦ͠͏ -
SWCͳͥੜ·Εͨͷ͔ • js (AltJS)ͷίϯύΠϧΛվળ͍ͨ͠ • → esbuild ʹ͍ۙύϑΥʔϚϯε • webpack
ͷόϯυϧΛվળ͍ͨ͠(spack) • → con fi g Λ͍ۙ͠ͷʹͨ͠Γɺޓੑͷ͋ΔϓϥάΠϯγεςϜΛಋೖ͠Α ͏ͱ͍ͯ͠Δ • → webpack ར༻ΛऔΓࠐΈ͍ͨʁ -
࠷ۙͷϏϧυπʔϧ͕ੜ·Εͨഎܠ • ैདྷͷπʔϧ(webpack, Parcel)Ͱɺ web frontend application ͷڊେԽʹ͍ɺ։ൃڥʹ͓͚ΔϏϧυ(DX)ͷԼ͕ݦஶ ʹͳ͖ͬͯͨ •
Ϟμϯϒϥβʹ͓͍ͯ ES modules ͷػೳ͕͑ΔΑ͏ʹ ͳ͖ͬͯͨ • js (Node.js)ͷݶքΛ͍͑ͨ -
Ϗϧυπʔϧͷͱ͜Ε͔Β • ։ൃڥͰόϯυϧ͠ͳ͍ํ • ຊ൪ڥʹ͓͚Δόϯυϧʹ͍ͭͯҙݟ͕ผΕ͍ͯΔ • ࣮ࡍ IE ͕ϦϏϯάσουঢ়ଶ •
શʹόϯυϧෆཁʹͳΔະདྷ·ͩઌ͔ • js Ͱͳ͍πʔϧग़ݱ͖͍ͯͯ͠Δ -
ॴײ • frontend ͷྺ࢙Λ֞ؒݟΕΔྑ͍ػձʹͳΓ·ͨ͠ • ݩͷঢ়گΛݟͨײ͡ɺ࠷ۙͷπʔϧͷதͰ Vite ͷࢥ͕ͪΐ͏Ͳ ྑͦ͞͏ •
༻ײͳ͔ͳ͔ྑ͔ͬͨ • esbuild ͱ Rollup Λར༻͍ͯ͠ΔͨΊɺ͜ΕΒซ͓͓ͤͯ͑ͯ͘͞ ͱྑͦ͞͏