LoginSignup
2
0

More than 1 year has passed since last update.

Riot.jsを始めてみた

Last updated at Posted at 2020-12-13

ソースコード部分はRiot.jsによるExamplesを筆者が改変しました。MITライセンスとなります。使用する際はライセンス表記を残す、もしくは本記事のソースは参考程度にするようにしてください。

本記事はRiot.js Advent Calendar 2020およびなんか適当な Advent Calendar 2020に14日目の記事として参加しています。

npm公式のドキュメントにあるサンプルアプリケーションのToDoを動かしたときに、予めビルドする場合の記述が少なく若干ハマったので記事にしました。なお、記事のソースコード部分はMITライセンスです。詳細は末尾に記載してあります。

環境構築

エディタはVSCodeを使います。設定についてはVSCodeでRiot.jsのシンタックスハイライト+フォーマットの方に記述してあります。

1​. npmを導入しておきます。

2​. 公式のtemplateからinitします。Templateはparcelを選択しました。

npm init riot

3​. 試しに一回実行してみます。

npm i
npm run start

ファイルの作成

準備

ディレクトリ構造は以下のようになっているはずです。

.
├── LICENSE
├── index.html
├── node_modules
│   └── (省略)
├── package-lock.json
├── package.json
├── readme.md
├── src
│   ├── components
│   │   ├── global
│   │   │   ├── my-component
│   │   │   │   ├── my-component.riot
│   │   │   │   └── my-component.spec.js
│   │   │   └── sidebar
│   │   │       ├── sidebar.riot
│   │   │       └── sidebar.spec.js
│   │   └── includes
│   │       └── user
│   │           ├── user.riot
│   │           └── user.spec.js
│   ├── index.js
│   └── register-global-components.js
├── style.css
└── project_name
    └── register-riot-extension.js

とりあえずsrc/componets配下はいらないので削除してしまいます。

index.html

以下のように記述します。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello Riot.js</title>
    <link rel="stylesheet" href="todo.css">
</head>

<body>
    <div id="todo" />
    <script src="src/index.js"></script>
</body>


</html>

src/index.js

import "@riotjs/hot-reload";
import { component } from "riot";
import Todo from "./components/todo.riot";
component(Todo)(document.getElementById("todo"), {
  title: "I want to behave!",
  items: [
    { title: "Avoid excessive coffeine", done: true },
    { title: "Hidden item", hidden: true },
    { title: "Be less provocative" },
    { title: "Be nice to people" },
  ],
});

src/componets/todo.riot

公式ドキュメントそのままです。ソースはここにあります。
https://github.com/riot/examples/blob/gh-pages/todo-app/todo.riot

todo.css

こちらについても特に手を加えていません。以下からコピーします。
https://github.com/riot/examples/blob/gh-pages/todo-app/todo.css

実行

npm run startして表示されたリンクをブラウザで開けば見えるはずです。

解説

ハマったポイントがいくつかあったのでそこについて解説しておきます。なお、初心者ですのでもし間違いがあればご指摘の程よろしくお願いします。

importの名前の付け方

import Todo from "./components/todo.riot";

ここで.riotファイルを読み込んでいます。import TodoTodoはタグ名の先頭を大文字にしたものです。実際にScript部分で名前を定義しているわけではありません(多分)。

index.htmlとsrc/index.jsとの対応関係

<div id="todo" />
component(Todo)(document.getElementById("todo"), {

以上のindex.htmlの13行目とindex.jsの4行目が対応しています。
JSでidを指定してあげて、HTMLではそれを呼び出す感じにするとうまく行きます。

まとめ

npm公式のドキュメントにあるサンプルアプリケーションのToDoを予めビルドしてを動かす場合の書き方を記事にしました。Riot.jsは使い始めたばかりですがかなり便利そうなので、また何か成果があったら記事にします。

追記:
とか思ったんですが、普通に「クイックスタート」の項にあるコードとほぼ変わりませんでしたね。一応記事自体はこのままとっておきます。

ライセンス

ソースコード部分はRiot.jsによるExamplesを筆者が改変しました。MITライセンスとなります。使用する際はライセンス表記を残す、もしくは本記事のソースは参考程度にするようにしてください。

MIT License

Copyright (c) 2020 Riot.js

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0