ããã°ããªãTypeScriptç°å¢ã»ãã
è²ã ãã£ã¦ã·ã³ãã«ãªtypescriptã®éçºç°å¢ãã¤ãããã¨ãã¦æ¶èãã話ã§ã
å°è¦æ¨¡ãªããã¸ã§ã¯ããã·ã¥ã£ã¦æ¸ããã·ã³ãã«ãªç°å¢ãã»ãã
- ã§ããã¦ããªã£ã¦ã¦ã»ãã
- ãããããã¦ãã¨ã¯...
- æè¿ã®ããã³ãã¨ã³ãã®äººã¯ä½ãè¨ã£ã¦ãã®ãå ¨ç¶ããããªããä¾åããã±ã¼ã¸ãå¤ããã¦æ··ä¹±ãã
- ã§ãã¡ãã£ã¨ã¯ãã¦ããªã£ã¦ã¦ã»ãã
試è¡é¯èª¤ããçµæ
- npm scripts + browserify + tsify + watchify ã§æ§æãããã¨ã«ãããï¼
- æ¬ä½ã1è¡ãæ¸ããªãã¾ã¾æ¥ä»ãå¤ãã£ã¦ããï¼
- ãããã¦ããªãã¦ããããæ¬ä½ãæ¸ããã
æ§æ
ã¨ããããbuildããã¨è²ã ãªãã®ãdistã«éãããæ§æã«ãã
âââ dist (static-assets) â âââ bundle.js â âââ bundle.css â âââ index.html âââ src â âââ ts â â âââ app.ts â â âââ ui.ts â â âââ renderer.ts â âââ css â â âââ custom.css â âââ html â â âââ index.html âââ package.json âââ tsconfig.json
package.json
ã¾ã npm init
ã㦠package.json
ãä½ãï¼
dependencies 㨠devDependencies
å¿
è¦ãªããã±ã¼ã¸ãå
¥ããï¼
npm install ${package} --save
ããã° package.json
ã«èªå追è¨ãããï¼
--save
㯠dependencies
ï¼å®è¡ã«å¿
è¦ãªããã±ã¼ã¸ç¾¤ï¼, --save-dev
ã¯devDependencies
ï¼éçºã«å¿
è¦ãªããã±ã¼ã¸ç¾¤ï¼ ã«å
¥ãï¼
$ npm install browserify tsify typescript watchify catw http-server --save-dev $ npm install jquery @types/jquery skeleton-css --save
ä»åã¯éçºã¾ããã®ããã±ã¼ã¸ä»¥å¤ã« jqueryï¼+åå®ç¾©ãã¡ã¤ã«ï¼ 㨠cssãã¬ã¼ã ã¯ã¼ã¯ã¨ã㦠skeleton ããã£ããã«ãããï¼
tsconfig.json
ããã¯typescriptããã©ã³ã¹ãã¤ã«ããã¨ãã®ãªãã·ã§ã³ãã¾ã¨ã¾ã£ãè¨å®ãã¡ã¤ã«ã§ï¼å¾è¿°ã® tsify ãããã¤ãåç
§ããï¼
tsconfig.json · TypeScript
ãã£ã typescript ãinstallããæã«å
¥ãã tscã使ã£ã¦ node_modules/.bin/tsc --init
ããã°ä»¥ä¸ã®ãã㪠tsconfig.json ãèªåçæãããï¼ã§ããjsã¯ã³ã¡ã³ãæ¶ãã¦ã·ã¥ãã¨ãªã£ã¦ã¦ã»ããã®ã§removeComments
ã¯è¿½å ãã*1
{ "compilerOptions": { "module": "commonjs", "target": "es5", "noImplicitAny": false, "sourceMap": false, "removeComments": true } }
ãã«ããã¼ã«
æåã¯gulp - npmã§ã²ã¨ã¨ããæ¸ãã¦ã¿ãã®ã ãï¼è²ã 試è¡é¯èª¤ãã¦ããã¡ã«
ãªããããmakefileããã·ã§ã«ã¹ã¯ãªããã§ããããã...
ã¨ããæ°æã¡ã«ãªã£ã¦ãããã¨ã¨ï¼gulp.watch
㨠watchify
ã¾ããã§ã¢ã¤ã¢ã¤ããããçµå± npm scripts ã§ç´ã«æ¸ããã¨ã«ããï¼ç¨®ã
ã®ãã¼ã«ã¯å»ãã¦ãnpmãæ»ã¬ãã¨ã¯ã¾ããªãã ããï¼
npm で依存もタスクも一元化する - Qiita
[意訳]私がGulpとGruntを手放した理由 - Qiita
npm scripts
package.json ã® scripts: ã§ã¯ï¼npm run ${task}
ã§èµ°ãããã¿ã¹ã¯ãå®ç¾©ãããã¨ãã§ããï¼ã¿ã¹ã¯ã®ä¸èº«ã¯ã·ã§ã«ã¹ã¯ãªãããã¶ã¡æ¸ãã¦è¯ãï¼ãã¨ãã°
package.json
"scripts": { "clean": "rm -rf dist/", "build:ts": "browserify ./src/ts/app.ts -p [tsify] | uglifyjs -c > dist/bundle.js" }
ã®ããã«æ¸ã㦠npm run clean
ã¨ãnpm run build:ts
ããã¨æ¸ããã¾ã¾ã®ã³ãã³ããå®è¡ãããï¼ã¾ãï¼node_modules/.bin/browserify
ã®ãããªãã¹ãæ¸ããªãã¦ã--save
ã--save-dev
ã§å
¥ãããã¼ã«ã¯ç´ã«å¼ã³åºããï¼
- ã·ã§ã«ã³ãã³ãç´æ¸ããã¦ãã¾ãã¨Winã©ãããã®
- ãã ã®æååã«ãªã£ã¦ãã¾ããã管çããã¥ãã
- åé·ãªè¨è¿°ãå¢ãã
- å¯èªæ§ãä½ã etc.
ã®ãããªåé¡ã¯ç¢ºãã«ããã®ã ãï¼ä»åå°è¦æ¨¡ç¨ã¨ãããã¨ã§scriptsã ãã§ãã£ã¦ã¿ããã¨ã«ããï¼
éçºãã¼ã«
browserify
CommonJSä»æ§ã§æ¸ãããrequire('module')
ããã©ã¦ã¶ä¸ã§åãããããã«ããã¢ã¸ã¥ã¼ã«ã·ã¹ãã ï¼ã¢ã¸ã¥ã¼ã«éã®ä¾åé¢ä¿ã解決ãã¦ãã³ãã«ãã¦ãããï¼
$ browserify main.js > bundle.js
ã®ããã«ããã¨ï¼main.jsï¼CommonJSä»æ§ã§æ¸ãããjsï¼ããã©ã¦ã¶ã§åãjsã«å¤æãã¦stdoutã«ã¶ã¡åãã¦ãããã®ã§ãã®ã¾ã¾bundle.jsã«æ¸ãåºãæãã«ãªãï¼ãããï¼ä»åã¯typescriptãã¾ãjsã«ããªããã°ãªããªã*2ã®ã§ï¼å¾è¿°ã®tsifyãåããã¦ä½¿ãï¼
tsify
TypeScriptããã©ã³ã¹ãã¤ã«ããããã®browserifyãã©ã°ã¤ã³ï¼ããã¨åããã¦ä½¿ããã¨ã§ï¼TypeScriptãjsã«ãã©ã³ã¹ãã¤ã«ãã¦CommonJSä»æ§ã®jsããã©ã¦ã¶ã§åãjsã«ããã®ãä¸åº¦ã«ãã£ã¦ãããï¼*3
$ browserify main.ts -p [tsify] > bundle.js
ã®ããã«ï¼tsifyããã©ã°ã¤ã³æå®ããbrowserifyã«ãããã¨main.tsï¼typescriptï¼ããã©ã¦ã¶ã§åãjsã«å¤æãã¦stdoutã«ã¶ã¡åãã¦ãããã®ã§ãã®ã¾ã¾bundle.jsã«æ¸ãåºãæãã«ãªãï¼
[]
㯠tsifyã«ãªãã·ã§ã³ã渡ãã¨ãå¿
è¦ã§[tsify --noImplicitAny]
ã¿ããã«ã§ããï¼ãªãã·ã§ã³ãç¡ãã¨ãã¯çç¥å¯è½ï¼ãªãã·ã§ã³ã¯åºæ¬çã«tscã¨åããã®ã渡ããï¼
watchify
ãã¡ã¤ã«ç£è¦ãã¦å·®åbrowserifyããããã®ãã¼ã«ï¼ã ã¨æã£ã¦ãï¼ï¼
watchify main.js -o 'bundle.js'
ã®ããã«ããã¨ï¼main.jsãå¤æ´ãããã¨ãï¼bundle.jsãåæã«browserifyãã¦ããã*4ï¼ã¾ãï¼åºåå ã«ã¯ã³ãã³ããè¨å®ãããã¨ãå¯è½ï¼
watchify main.js -o 'uglifyjs -cm > static/bundle.min.js'
catw
ç£è¦æ©è½ä»ãã®catã§ï¼ä»åã¯cssé£çµãããããã®ã«ä½¿ãï¼cssã¨htmlå¨ãã¯è²ããªnpmããã±ã¼ã¸ãçºãã¦ããã¾ãã¡ã©ãããã¹ããªã®ãããããããªãã£ããã¨ããããcssãå¤æ´ãããé£çµãã¦distã«ã¶ã¡éãæãã«ãã
catw main.css custom.css -v -o 'dist/bundle.css'
ã¿ããã«ãã㨠mainã¨customãç£è¦ãï¼å¤æ´ãããã°é£çµãã¦ãã¦bundle.cssã«ã¶ã¡åãã¦ãããï¼
www.npmjs.com
åå®ç¾©ãã¡ã¤ã«
typescriptã§ã¯jsã®ã©ã¤ãã©ãªããã®ã¾ã¾èªããªãã®ã§åå®ç¾©ãã¡ã¤ã«ãå¿ è¦ãªã®ã ãï¼ä»å¹´ãªãªã¼ã¹ãããtypescript2.0ããã¯npmã ãã§ãã®åå®ç¾©ãã¡ã¤ã«ã管çã§ããããã«ãªã£ãï¼ãã¨ãã°
$ npm install jquery @types/jquery jqueryui @types/jqueryui --save
ã®ããã« åå®ç¾©ãã¡ã¤ã«ãã¤ã³ã¹ãã¼ã«ããã ãã§ï¼ç¹å¥ãªãã¼ã«ã宣è¨ç¡ãã«tsã§jquery, jquery-uiã®ã©ã¤ãã©ãªã使ãã*5ï¼typingsã¨ãtsdã¯ãããããªãã£ã½ãï¼
src/ts/app.ts
import * as $ from "jquery"; //=> node_modules/@types/jquery/index.d.ts ãè¦ã«è¡ã£ã¦ããã
The Future of Declaration Files | TypeScript
npmã§ã¤ã³ã¹ãã¼ã«å¯è½ãªtypesããã±ã¼ã¸ã¯ããããæ¤ç´¢ã§ããï¼
TypeScript Types Search
tsconfig.json ã® typeRootsã§ããã£ã¦ã
By default all visible â@typesâ packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on.
typeRootsã¯ä»¥ä¸ã®ããã«æ示çã«æå®ã§ãããï¼node_modules/@typesã¯æå®ããªãã¦ãåæã«è¦ã«è¡ã£ã¦ãããï¼
tsconfig.json
{ "compilerOptions": { "typeRoots" : ["./mytypes"] } }
ã¾ãï¼typeRootsãæå®ããã¨ããã«è¨è¿°ãã以å¤ã®ãã¹ã¯è¦ã«è¡ããªããªã£ã¦ãã¾ãï¼ä¸ã®ä¾ã§ã¯./mytypes
ã®ã¿ãè¦ã«è¡ãï¼node_modules/@types
ã¯è¦ã«è¡ããªããªãã®ã§æ³¨æããï¼
tsconfig.json · TypeScript
types
@typesã«åå®ç¾©ãã¡ã¤ã«ãå
¥ãããå¾ã¯ import * as hoge from fuga
ããã°ç°¡åã«èªã¿è¾¼ãã¦ä¾¿å©ã ãã©ï¼æå³ããªãããã±ã¼ã¸ãèªã¿è¾¼ãã§ãã¾ã£ããå°ãã¨æã*6ï¼ãã®å ´åï¼
tsconfig.json
{ "compilerOptions": { "types" : ["jquery", "jqueryui"] } }
ã®ããã«æ示çã«èªã¿è¾¼ã¿ããåå®ç¾©ãæå®ããã°ï¼ãã®ããã±ã¼ã¸ã®ã¿èªã¿è¾¼ã¿ã許å¯ãããã¨ãã§ããï¼types:[]
ã¨ããã°@types/*
ãèªåã§è¦ã«è¡ãã®ãç¡å¹åãããã¨ã«ãªãï¼
ãã¾ã®package.json
ãããªæãã«ãªã£ãï¼
åºæ¬çã«ã¯ npm run dev
ãã¦éçºãããã¨ã«ãã¦ãï¼
typescriptã½ã¼ã¹, css, html ãå¤æ´ãããã¨distãæ´æ°ãããï¼
{ "name": "kirakira-ol", "version": "0.0.1", "description": "marunouchi building", "scripts": { "clean": "rm -rf dist/", "prebuild": "npm run clean && mkdir dist", "build:css": "cat ./node_modules/skeleton-css/css/* ./src/css/custom.css > dist/bundle.css", "build:html": "cat ./src/html/index.html > dist/index.html", "build:ts": "browserify ./src/ts/app.ts -p [tsify] > dist/bundle.js", "build": "npm run prebuild && npm run build:css && npm run build:html && npm run build:ts", "watch:ts": "watchify ./src/ts/app.ts -p [tsify] -v --debug -o dist/bundle.js", "watch:css": "catw ./src/css/custom.css ./node_modules/skeleton-css/css/* -v -o dist/bundle.css", "watch:html": "catw ./src/html/index.html -v -o 'dist/index.html'", "watch": "npm run watch:ts & npm run watch:css & npm run watch:html", "serve": "./node_modules/.bin/http-server dist/ -p 8000", "dev": "npm run serve & npm run watch" }, "devDependencies": { "browserify": "^13.1.1", "catw": "^1.0.1", "http-server": "^0.9.0", "tsify": "^2.0.7", "typescript": "^2.1.4", "watchify": "^3.8.0" }, "dependencies": { "@types/jquery": "^2.0.34", "jquery": "^3.1.1", "skeleton-css": "^2.0.4" } }
追è¨
ããã®ããã£ãï¼
www.npmjs.com
npm-run-all ã使ã£ã¦ buildï¼watch ãã¾ã¨ãã¦ã¿ãï¼
$ npm install --save-dev npm-run-all
package.json
"scripts": { "clean": "rm -rf dist/", "prebuild": "npm run clean && mkdir dist", "build:css": "cat ./node_modules/skeleton-css/css/* ./src/css/custom.css > dist/bundle.css", "build:html": "cat ./src/html/index.html > dist/index.html", "build:ts": "browserify ./src/ts/app.ts -p [tsify] | uglifyjs -c > dist/bundle.js", "build": "npm run prebuild && npm-run-all build:*", "watch:ts": "watchify ./src/ts/app.ts -p [tsify] -v --debug -o dist/bundle.js", "watch:css": "catw ./src/css/custom.css ./node_modules/skeleton-css/css/* -v -o dist/bundle.css", "watch:html": "catw ./src/html/index.html -v -o 'dist/index.html'", "watch": "npm-run-all --parallel watch:*", "serve": "http-server dist/ -p 8000", "dev": "npm run serve & npm run watch" }
ææ³
- æããã¦ããã¯ã·ã³ãã«ã§ã·ã¥ãã¨æ¸ããç°å¢ãªã®ã
- npm scriptsããªãã ããã 段ã è²ã ãããããªã£ã¦ãã¦ããããæãã¦ãã
- cssã¨htmlã¯ã©ãããã®ãããæããªã®ããã¾ãã¡ããããªã
- å°è¦æ¨¡ãªãtsãé¸æããã¨ãES6+babelã§è¯ãã®ã§ã¯ãªãã. å°è¦æ¨¡ã§åã¯å¿ è¦ãªã®ã
- è²ã ãªæ å ±ã氾濫ãã¦æ··ä¹±ããã®ã§ï¼çµå±å°éã«manãããå ¬å¼èªãã ãã½ã¼ã¹èªããã¨ã«ãªã£ã
- ããã³ãã¨ã³ããã°ãã¡ããã ã£ãã®ã§åå¼·ã«ãªã£ã
åè
春からはじめるモダンJavaScript / ES2015 - Qiita
レトロエンジニアのための近代Webフロントエンド事情 - Qiita
TypeScriptを使ってECMAScript 2015時代のJavaScriptを先取りしよう! | HTML5Experts.jp
JavaScriptのモジュールシステムの歴史と現状 - Qiita
*1:ãã¨ã§uglifyjsã¨ãã«ãããã
*3:tscã§ãã©ã³ã¹ãã¤ã«ããçµæã®jsãbrowserifyã«ããã¦ãããæ°ãããï¼
*4:ãªã«è¨ã£ã¦ããããããªããªã£ã¦ãã
*5:æ¬ä½ã¯å¿ è¦. ã㨠jquery plugins ã® jqueryä¾åã解決ããã«ã¯å¥é browserify-shim ãå¿ è¦ã ãï¼ä»åã¯çç¥ããï¼
*6:ã°ãã¼ãã«ç©ºéãæ±ããããã¨ãå±æ§ãã¦ãã