Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

TypeScriptでつくるNode.jsパッケージ

 TypeScriptでつくるNode.jsパッケージ

Fukuoka.js #8「TypeScript」

Yusuke Hirao

May 15, 2019
Tweet

More Decks by Yusuke Hirao

Other Decks in Technology

Transcript

  1. ©2019 Yusuke Hirao ίʔυΛॻ͘લʹܾΊ͓ͯ͘͜ͱ ϦϙδτϦͷσΟϨΫτϦߏ੒ +BWB4DSJQUϑΝΠϧ΍ ܕఆٛϑΝΠϧʢ*.d.tsʣͳͲͷॻ͖ग़͠ઌ w OQNͰެ։͢ΔϑΝΠϧ܊ΛೖΕΔɻ w

    HJUͰ͸আ֎͢Δɻ w ໊લ͸libͰ΋distͰ΋ԿͰ΋͓޷ΈͰɻϞδϡʔϧʹύεࢀ রͰ͖Δύοέʔδʢ5SFF4IBLJOHͷͨΊͩͬͨΓʣʹ͍ͨ͠৔ ߹ɺσΟϨΫτϦ໊ͪΐͬͱॏཁɻʢྫKBDPKTʣ import toHiragana from "jaco/fn/toHiragana"; toHiragana("ΧλΧφΛͻΒͳ͕ʹ");
  2. ©2019 Yusuke Hirao ίʔυΛॻ͘લʹܾΊ͓ͯ͘͜ͱ ॻ͖ग़͢+4ͷόʔδϣϯʢtargetʣ es5 es2017 esnext  ϝϦοτ

    ͲΜͳ؀ڥͰ΋ಈ͘ /PEFKTWҎ্ ͳΒಈ͘ import.metaରԠ ʢ&4.ʹඞਢʣ  σϝϦοτ async/await΍ for..ofͳͲ͕ ϨΨγʔίʔυม׵͞ΕΔ ϒϥ΢βͰ࢖͏৔߹͸ CBCFMͳͲ͕ඞཁʹͳΔ ී௨ʹ࢖͏ͳΒ CBCFMͳͲ͕ඞཁ
  3. ©2019 Yusuke Hirao ίʔυΛॻ͘લʹܾΊ͓ͯ͘͜ͱ ॻ͖ग़͢+4ͷϞδϡʔϧγεςϜʢmoduleʣ commonjs esnext ͦͷଞ  ϝϦοτ

    ݱࡏͷඪ४ import.metaରԠ ʢ&4.ʹඞਢʣ ͠ΒΜ  σϝϦοτ &4.Ͱ࢖͑ͳ͍ ී௨ʹ࢖͏ͳΒ CBCFMͳͲ͕ඞཁ Θ͔ΒΜ
  4. ©2019 Yusuke Hirao ઃఆ $ yarn init { "name": "@yusukehirao/sample",

    "version": "0.1.0-alpha.0", "main": "lib/index.js", "author": "Yusuke Hirao <[email protected]>", "license": "MIT" } ࣮ݧͷࡍ͸4DPQFEQBDLFHFʹ͠·͠ΐ͏ ઌʹܾΊͨσΟϨΫτϦ಺ͷΤϯτϦʔϑΝΠϧ import sample from "@yusukehirao/sample"; ௨ৗͷimport࣌ͷࢀরઌʹͳΔ package.json
  5. ©2019 Yusuke Hirao ઃఆ $ yarn add -D typescript ίϯύΠϥΛΠϯετʔϧ

    { "name": "@yusukehirao/sample", "version": "0.1.0-alpha.0", "main": "lib/index.js", "author": "Yusuke Hirao <[email protected]>", "license": "MIT", "devDependencies": { "typescript": "^3.4.5" } } package.json
  6. ©2019 Yusuke Hirao ઃఆ $ ./node_modules/.bin/tsc —init { "compilerOptions": {

    "target": "es2017", "module": "commonjs", "declaration": true, "declarationMap": true, "sourceMap": true, "outDir": "./lib", "rootDir": "./src", "strict": true, "esModuleInterop": true } } tsconfig.json ܾΊͨॻ͖ग़͠ઌͱιʔείʔυஔ͖৔ ܾΊͨ&4όʔδϣϯϞδϡʔϧγεςϜ
  7. ©2019 Yusuke Hirao ઃఆ $ ./node_modules/.bin/tsc —init { "compilerOptions": {

    "target": "es2017", "module": "commonjs", "declaration": true, "declarationMap": true, "sourceMap": true, "outDir": "./lib", "rootDir": "./src", "strict": true, "esModuleInterop": true } } tsconfig.json ܕఆٛϑΝΠϧͱιʔεϚοϓϑΝΠϧͷ ੜ੒Λ༗ޮʹ͓ͯ͘͠
  8. ©2019 Yusuke Hirao ઃఆ ίϯύΠϧίϚϯυΛొ࿥ tsc ίϚϯυΛొ࿥͓ͯ͘͠ { "name": "@yusukehirao/sample",

    "version": "0.1.0-alpha.0", "main": "lib/index.js", "author": "Yusuke Hirao <[email protected]>", "license": "MIT", "scripts": { "build": "tsc", "prepack": "tsc" }, "devDependencies": { "typescript": "^3.4.5" } } package.json w Φϓγϣϯ͸tsconfig.jsonʹॻ͍͓͚ͯ ͹جຊ͍Βͳ͍ɻ w prepackεΫϦϓτ͸ύοέʔδެ։࣌ͷ Ϗϧυ๨Ε๷ࢭɻ
  9. ©2019 Yusuke Hirao ઃఆ ܕఆٛϑΝΠϧΛొ࿥ ΤϯτϦʔϑΝΠϧͷܕఆٛϑΝΠϧͷύε { "name": "@yusukehirao/sample", "version":

    "0.1.0-alpha.0", "main": "lib/index.js", "types": "lib/index.d.ts", "author": "Yusuke Hirao <[email protected]>", "license": "MIT", "scripts": { "build": "tsc", "prepack": "tsc" }, "devDependencies": { "typescript": "^3.4.5" } } package.json w ܕ৘ใΛఏڙͰ͖Δɻ w Α͘ @types/xxxxxͰࢀর͢Δ %FpOJUFMZ5ZQFE%FpOJUFMZ5ZQFEʹ13 ૹΔඞཁͳ͍ɻ
  10. ©2019 Yusuke Hirao ύϒϦογϡ͢Δ $ touch .npmignore ύοέʔδʹؚ·ͳ͍ϑΝΠϧͷআ֎ઃఆΛ͢Δ src tsconfig.json

    tsconfig.tsbuildinfo .npmignore w package.jsonͷfilesʹύοέʔδʹؚΊ ͍ͨϑΝΠϧͷϗϫΠτϦετΛॻ͍ͯ΋Α ͍ɻ w .npmignore͔files͕ͳ͍ͱɺ.gitignore Λআ֎ઃఆͱΈͳ͢ͷͰɺॻ͖ग़͠ઌͷσΟϨ ΫτϦ͕ύοέʔδʹೖΒͳ͍൵ࢂͳ͜ͱʹͳ Δ