æ¬èšäºã¯ Angular Advent Calendar 2023 ã®24æ¥ç®ã®èšäºã§ãâ
23æ¥ç®ã¯@ic_lifewoodããã§25æ¥ç®ã¯@lacolacoããã§ãâ
ææ°ã®Angularã®æ©èœããµãã ãã«äœ¿ã£ãŠã
Angular Advent Calendarã®èšäºãããªãŒã®é£Ÿã(ãªãŒãã¡ã³ã)ã«ãããµã€ããã€ãããŸããââ
- https://ver-1000000.github.io/angular-advent-calendar-2023/
- GitHub: https://github.com/ver-1000000/angular-advent-calendar-2023/
ããŸãããããã¿ã®ãããµã€ãã§ã¯ãªããããããŸãããã
ãèŠãç®ããèŠä»¶ãããã«ããããããšãç®æããŠäœæããŸããã
ä»ã®æ¹ã®Advent Calendarèšäºã«èšåãã€ã€ãå®è£
ã®è©³çŽ°ã解説ããŠãããŸãã®ã§ã
å®éã«è§Šããªããèªã¿é²ããŠããã ããã°ãšæããŸãâ
ä»ã®èšäºã§èšåãããŠããå®è£
Prettier & ESLint
Angularãå ¥ãããæåã«èšå®ããŠããããããªã³ã¿ãŒãšãã©ãŒããã¿ã®èšå®ã§ãã
次ã®ã³ãã³ãã§å°å
¥ããããšãçæããã.eslintrc.json
ãç·šéããŸããã
$ npm i -D prettier eslint-plugin-prettier eslint-config-prettier
$ # commit
$ ng add @angular-eslint/schematics
åºæ¬çã«ãææ°ã®recommendedãªèšå®ã䜿ããããã³ã²ã³ãªã®ã§ã
prettierãšã®å
±åã®ããã®èšå®ä»¥å€ã«ãŒã«ã®å€æŽãªã©ã¯ç¹ã«è¡ã£ãŠããŸããã
(ãã¢ããããŒãã®ãã³ã«ã«ãŒã«ã®ååãèšå®ãã¡ã€ã«ã®æ§é ãå€ãã£ãããšãæ€ç¥ããã®ãé£ãããŠå«ãããªãïŒããšããæ°æã¡ããã)
ããããæçšãªãªããã€ã³ã«ãŒã«ã¯ããããããã®ã§ãå°å ¥ã®éã¯éœåºŠäžèããããšããå§ãããŸãã
-
Angular ESLintã®å°å
¥ãšæšãã«ãŒã«6éž(2023) | @komura-c
- æ¹ããŠçºããŠã¿ããšã©ããæ¬åœã«æçšã§ããâŠâŠã
Angular CDK
æ®éã«Webãµã€ããäœæãããšãCDKã®äœ¿ãã©ããã¯çµæ§å€ãã®ã§ãããä»åã¯ãã€ã¢ãã°ã®äœæã®ã¿ã«å©çšããŠããŸãã
UIé¢ã¯éåžžã«èãã§ãããåããŠã¢ã¯ã»ã·ããªãã£ãªã©ã®ãµããŒããªã©ã¯æåãã
å®è£
ã®éã¯ç©æ¥µçã«å©çšãããã©ã€ãã©ãªã§ããã
(ãã®ç»é¢ãèŠãã ãã§ããã¢ãŒãã«ãéãããšãã«ãUIäžã®æåã®èŠçŽ ã«ãã©ãŒã«ã¹ãåœãŠããç²ãªã¢ã¯ã»ã·ããªãã£ãã€ã³ããå¬ãã)
ã¡ãªã¿ã«ãDialogã¯å
éšçã«å©çšããŠããOverlayãªã©ãã
å°çšã®CSSãèªã¿èŸŒãŸãªããšãªãã®ã¹ã¿ã€ã«ãåœãŠãããŠããªãç¶æ
ã«ãªãã®ã§ã
次ã®äžè¡ãstyles.cssã«æžããŠãããŸãããã
@import '@angular/cdk/overlay-prebuilt.css';
ãªããDialogã®Documentã«ã¯ãã®ããšãèšèŒãããŠããŸããã
CDK Dialogã«ã€ããŠããã詳ãã説æã¯æ¬¡ã®èšäºãã©ããã
NgOptimizedImage
ãéçºè ãããŸãæèããªããŠãç»å衚瀺ãæé©åããŠããããããã§ãã
é©åœã«äœ¿ã£ãŠã¿ããš(NgOptimizedImage
ãimportããŠimgã¿ã°ã®srcãngSrcã«ããã ã)ã
å®è¡æã«èŠåãåºããŠãããã®ã§ã察話çã«å®è£
ã§ããŸãã
ãã詳ãã説æã¯æ¬¡ã®èšäºãã©ããã
ngSrcãèšè¿°ããéã¯ã«ãŒãããå§ãŸããã¹ãæžããªãããã«æ³šæïŒ
assetsãã£ã¬ã¯ããªã«å
¥ããŠããç»åãåç
§ããããã«
/assets/xxx.png
ãªã©ãšæžããšãbuildæã«base-hrefãèšå®ããŠããå Žå
ãã¹ã®è§£æ±ºãã§ããªããªã£ãŠããŸãããã
assets/xxx.png
ã./assets/xxx.png
ã®ããã«æžãå¿
èŠãããã
çè ã¯ãããã€æã«ç»åãèªã¿èŸŒãŸããªãèªäœã«é¥ã£ãŠå°ãã ãããããŸããð¢
SSG(SSR)
v17ããã¯Server Side Generation(Rendering)ãããããžã§ãã«ç°¡åã«ãªããŸããâ
ãããžã§ã¯ãäœææã察話çã«ssrãµããŒããONã«ãããèãããã®ã§ã
ããã§æå¹åããããšã¯npm run build
ã§SSGã®çæç©ãäœæãããŸãã
æ¬ãµã€ãã§ã¯SSRãªãã·ã§ã³ããªã³ã«ããŠGitHub Pagesã«ãããã€ããŠããã®ã§ã
ãã§ã«äžåºŠã¬ã³ããªã³ã°ãããçµæãè¿ã£ãŠããããã«ãªã£ãŠããŸãã
èæ¯ã®ããŒãã£ã¯ã«ã®äœçœ®ããªãŒãã¡ã³ãã®è²ãã©ã³ãã ã§å€ããããã«ãªã£ãŠããã®ã§ã
JSããªãã«ããç¶æ
ã§ããŒãžã®ãªããŒããèµ°ããããšéããåãããããã§ãã
ä»ã«ãSSRã«ã€ããŠã次ã®èšäºã§ãããã詳ããæžãããŠããŸãã
- Angularã§ã¯ãããSSRå ¥é | @nishitaku
- 2024幎ããå§ããAngular Universal (SSR) ã®æ¬çªéçš | æµ·éœ
SSGãå©çšããå€éšãªãœãŒã¹ã®ãã£ãã·ã³ã°
ä»åããªãŒãã¡ã³ãã衚瀺ããããã«ãã¢ããªå ã®HttpClientã§Qiitaã®RSSãã£ãŒãããèšäºæ å ±ã25åååŸããŠããŸãã
ãšããããå®éã«ãã©ãŠã¶ãæŽæ°ãããšã該åœã®éä¿¡ã¯èµ°ããŸããã
Whatâs next for Server Side Rendering in Angularã®èšäºã«ããã°ã
Weâve also made some updates to HttpClient. It will now cache requests made on the server to avoid re-fetching that same data again on the client.
ãšã®ããšãªã®ã§ãã©ãããSSRã¢ãŒããæå¹ã«ãããšããã©ã«ãã§ããæãã«ãã£ãã·ã¥ããŠãããããã§ãïŒã¹ãã¹ã²ãŒãŒïŒïŒïŒ
æ¬ãµã€ãã§ã¯mainãã©ã³ããžã®pushæã«ãGitHub Actionsã®CIäžã§ãã«ããèµ°ãã®ã§ã
ãã®ã¿ã€ãã³ã°ã§éä¿¡æ
å ±ãæ°žç¶åãããããã§ããã
Control Flow
ã³ã³æè¿ã®ã¢ããããŒãã§ã¯å€§ç®çååãšããããšã§è©±é¡ã®Control Flowã§ããã
Advent Calendarã§ããã¯ãã¿ãªããèšåãããŠããŠã泚ç®åºŠã䌺ããŸãã
- 2023 幎ã«å ¥ã£ã Angular ã®ããŸããŸãªã¢ããããŒã | kasaharu
- Angularã«ãããçµã¿èŸŒã¿å¶åŸ¡ãããŒã®å°å ¥ãšãã®èæ¯ | ãã£ã
- Angular v17 ã§æ°ããå¶åŸ¡ãããŒæ§æããã£ãŠããïŒãã€ã°ã¬ãŒã·ã§ã³ã³ãã³ããè©ŠããŠã¿ãã | @kozy4324
- Control FlowãšViewã¬ã³ããªã³ã° | noxi515
- Prettier ã® Angular ãµããŒãã®ä»çµã¿ãš built-in control flow å¯Ÿå¿ | Sosuke Suzuki
æ¬ãµã€ãã§ãæ©é䜿ã£ãŠã¿ãŸããã
switchæãªããã¯ããªãã·ã³ãã«ã§é©åãªæãã®æžãæ¹ãã§ããŠããæ°æã¡ã«ãªã£ãŠããã§ããã
trackByãéåžžã«æžããããã§ãã
âŠâŠãã ãControlFlowã®å°å
¥ã«ãã£ãŠhtmlå
ã«@
èšå·ã䜿ããªããªã£ãç¹ã«ã€ããŠã
ããã解決ããããã®åç
§æååã調æ»ããã®ã«å°ãããããŸããã
ããšããšãã®äºè±¡èªäœã¯ç¥èãšããŠç¥ã£ãŠããã®ã§ããã
ãåç
§æååããšããèšèãé ã«åºãŠããªããŠ
ã¢ããããŒã¯ã®åç
§äœ(@
)ã調ã¹ãããâŠâŠã
ã¢ããããŒã¯ã¯ãã³ãã¬ãŒãã«æžããšã³ã³ãã€ã«ãšã©ãŒãåºãã®ã§ããã
ãã¬ãŒã¹ã®çŽåã«æžããŠããŸããæ§æã®ããŒã¹ã圱é¿ããŠ
次ã®ãããªãšã©ãŒå
容ã«ãªã£ãŠããŸããé©åãªå¯Ÿå¿æ¹æ³ããã°ããããããŸããã§ããð¢ã
|6 col 51 error| ngtsc:Error:-995002:Unrecognized block @.
|14 col 6 error| ngtsc:Error:-995002:Unexpected character "EOF" (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.)
|14 col 6 error| ngtsc:Error:-995002:Invalid ICU message. Missing '}'.
|6 col 51 error| ngtsc:Error:-995002:Unclosed block ""
æ¯èŒçäœããªãç®æã«@
ãèšè¿°ããå Žåã¯ã次ã®ãããªãšã©ãŒãåºãŠå¯Ÿå¿æ¹æ³ãããããããã¿ããã§ãã
|1 col 1 error| ngtsc:Error:-995002:Unrecognized block @.
|1 col 1 error| ngtsc:Error:-995002:Incomplete block "". If you meant to write the @ character, you should use the "@" HTML entity instead.
æ°ãã@Input
ãã³ã¬ãŒã¿ã®ãªãã·ã§ã³
å°å³ã«å¬ããInputã®ãªãã·ã§ã³æ©èœãæ©é䜿ã£ãŠã¿ãŸããâ
å€ãããŒãžã§ã³ã®Angularã䜿ã£ãŠãããšãæ°ããInputãªãâŠâŠããšæãããšãå€ã
ãããã§ãããã
åçŽã§ããéåžžã«åŒ·åãªAPIã ãšæããŸãã
ä»ã®èšäºã§ããŸãèšåãããŠããªãå®è£ ã»å°æ
SVG
ä»åãã€ã©ã¹ãã¿ãããªUIãå®è£
ããã«ããã£ãŠãSVGãå€çšããããšã«ããŸããã
ãããã£ããµã€ãã®æ§ç¯ã ãšã
SVGã«ããå®è£
ãDOMã®ã»ãã³ãã£ã¯ã¹ãæ±ããªãæ°ãããŠãŠã¹ããªãã§ãããâŠâŠã
(ãã€ã©ã¹ãã®äžã®ã¯ãªã¹ãã¹ããªãŒã®æããè¡šãã¿ã°ãšãã«divã䜿ããããªã)
ãã ããSVGã«ããå®è£
ã¯åºæ¬çã«åº§æšã®çµ¶å¯Ÿæå®ã«ãªã£ãŠããŸãã®ã§ã
ããã¯è³¢ããããªããšãããŸãããã
çŸåšã¯ã³ãŒãã®äžã«å€ãã®ããžãã¯ãã³ããŒãããã®ã§ããããé€å»ããŠããã®ã¯èª²é¡ã§ãã
Angular Pipeãšã䜿ããšãããããããŸãã
æ°ããAngularãšã®çžæ§ã«ã€ããŠã§ããã䜿ãå¿å°ã¯æãšå€ããããšãããšããã§ããã
Control Flowãªã©ã§åé¡ãèµ·ããããªãšäžå®ã§ãããã
æ¡å€ãããªããšé²ããããšãã§ããŸããð®ã
GitHub Pagesãžã®ãã¹ãã£ã³ã°
çè
ã¯çµæ§GitHub Actionsãã€ãã£ãŠGitHub Pagesã«ãã¹ãã£ã³ã°ããŠããã®ã§ããã
ä»åã¯ããã§çµæ§è©°ãŸã£ãŠããŸããŸããâŠâŠã
çµè«çã«ã¯ãSettings > Actions > General > Workflow permissions
ã®æš©éã匱ãã£ãããšãåé¡ã§ããã
ä»ãŸã§ããã觊ã£ãããšãªãã£ããã§ãæè¿ããã©ã«ãèšå®ã匱ããªã£ããã§ãããã
ã¡ãªã¿ã«ãããŸãåã£ãŠããªãã®ã§ããGitHub Pagesã«ãããã€ããã¯ãŒã¯ãããŒã®å€æŽã¯ãããªæãã§ãã
ãã€ã³ã
- actionsçšã®ymlãæžã
- ä»åã¯peaceiris/actions-gh-pages@v3ãå©çš
-
publish_dir
ã«ã¯SSGã§çæããããã«ãçµæã®å Žæãæå®ãã- Angular CLIã§æ®éã«äœæããå Žå
.dist/APP_ID/browser
ã®ãã¹ãæå®ããŠãããšãã
- Angular CLIã§æ®éã«äœæããå Žå
-
build
ã³ãã³ãã§--base-href
ãèšå®ããŠãã- ã¡ãããšãããªãenvrionment.tsãšãã§ããæãã«ããã®ããªïŒ
- ngSrcã®çµ¶å¯Ÿãã¹ã«ã¯æ°ãã€ãã
-
Settings > Pages > Build and deployment
ã®èšå®ãé©åã«å€æŽãã- Source:
Deployment from a branch
- Branch:
gh-pages
/ (root)
- Source:
Signal API
äžéšã§ãããSignal APIã«ããå®è£ ãé 匵ã£ãŠã¿ãŸããâŠâŠâ
æ¡å€ãä»åã¯Signal APIã䞻軞ãšããAdvent Calendarã®èšäºã¯ãããŸããã§ããã®ã§ã
@lacolacoããã®æ¬¡ã®èšäºã«ããSimple PDS
ãã¿ãŒã³ãåèã«ããŠçµãã§ã¿ãŠãŸãã
å®éã«å®è£
ããŠã¿ããšããªãã»ã©ãã£ããããã«ã¢âŠâŠ ããæãã§ãð¥ã
ãã®èŸºã¯ã³ã¡ã³ããåãã«æžããŠãã®ã§ããã²ã³ãŒãã®æ¹ãã芧ãã ããâ
æéããªããŠå®è£ ãéã«åããªãã£ããã©çã蟌ã¿ããã£ããã€
ã«ãŒãã£ã³ã°(View Transitions API)
åœåã¯èšäºã®è©³çŽ°è¡šç€ºããã€ã¢ãã°ã§ã¯ãªããã«ãŒãã£ã³ã°ãå©çšããŠè¡šç€ºããããšããŠããŸããã
ãã®æã®é·ç§»ã«View Transitions APIãå©çšãããã£ãã®ã§ããâŠâŠã
UIçã«é·ç§»ãèžãŸããã¢ã€ãã£ã¢ãããã£ãšåºãŠãããå·¥æ°çã«èŠéããŸããð¢ã
Deferrable views
ã¯ã©ã€ã¢ã³ãã§ã®åçãªååŸãè¡ããã°@defer
ãšãã®åºçªããããããªã®ã§äœ¿çšããã€ããã ã£ãã®ã§ããã
ããå®è£
ããŠã¿ããšãæ¬ã¢ããªã§ã¯åºæ¬çã«éä¿¡ã«ãªã¢ã«ã¿ã€ã æ§ãæ±ããããŸããã
SSGãåæã«éä¿¡ããã£ãã·ã¥ããããé 延èªã¿èŸŒã¿ã®å¿ èŠããããŸããã§ããðœã
ãã¹ã/CIåšã
npm run test
ã¯éãããã«ããŠããŸãããã«ãã¬ããžã¯ã»ãŒ0ã¿ãããªãããªã®ã§ãããå°ãäœãããæžããŠããããã
ããšããšãããããã¹ãã®èªååãšprettier/eslintã®ãã§ãã¯ãCIã§åããããã§ããã
CSP_NONCEã«ããCSSã€ã³ãžã§ã¯ã·ã§ã³å¯Ÿç
ãŠãªãªããå®å
šã«ç¥ããŸããã§ããâŠâŠã
éåžžã«ç°¡åãªã®ã§ãåºæ¬çã«ã¯æèããŠæå¹ã«ããããã«ãããã§ããã
ããšãã
Advent Calendarã«ç»é²ããŠããã«ãSSGãå©çšãããããäœã£ãŠãä»ã®äººã®èšäºãå
šéšèªãã§é¢é£ããŠãããªãã€ããªãã¡ã¬ã³ã¹ããŠããããšããæéãããŠãã®ã§ããã
å®è£
ã®çæãå
éãã«ããŸãã£ãçµæã24æ¥åœæ¥ã«æ³£ããªããå
šéšå®è£
ããããšã«ãªããŸããâŠâŠã
ããããªããâŠâŠ
éã«èšãã°1æ¥ã§ããã£ãœãã¢ããªãã»ãŒãã«ã¹ã¯ã©ããã§ã§ããã®ã¯ãªããªãã®ææã§ã¯ãªãã§ããããâ
ãããå·¥æ°ã®ãã¡åå以äžã¯Webãã¶ã€ã³ãšSVGã®äœæã«è²»ãããŠããããã§ããããAngularã®çç£åã¯é©ããããŸããðª
ããªã®è¯ããšãããŸã§ã¯é²ããŸããããæéããªããŠå®è£
ãéã«åããªãã£ããã©çã蟌ã¿ããã£ããã€ãããããããããã«ããŸã è²ã
課é¡ãæ®ããŠããŸãã
Issueãªã©ã¯æŽåããŠããŸããããã©ããã§æ¹ä¿®ããŠãããããããªãšæããŸãã(PRãæè¿)
ããã§ã¯ã¿ãªããŸãððªð¹ð¹ð ðð®ð»ð»ð ðð±ð»ð²ðŒðœð¶ðªðŒ...ð«