Closed5

brainf**k インタープリタを作る

ゆきくらげゆきくらげ

とりあえず環境構築

npm init
npm install -D spago purescript esbuild tailwindcss @tailwindcss/jit npm-run-all live-server  
npx spago init
npx spago build

entry.js

const Main = require("./output/Main/index.js")

Main.main()

tailwind.config.js

module.exports = {
  mode: 'jit',
  content: [
    './public/index.html',
    './src/**/*.purs',
  ],
}

tailwind.css

@tailwind base;

@tailwind components;

@tailwind utilities;

package.json

...

  "scripts": {
    "test:spago": "npx spago test",
    "test": "run-s test:*",
    "bundle:spago": "npx spago bundle-app --main Main --to ./public/index.js",
    "bundle:tailwind": "npx tailwindcss -i ./tailwind.css -o ./public/style.css",
    "bundle": "run-s bundle:*",
    "watch:esbuild": "npx esbuild entry.js --bundle --outfile=./public/index.js --watch",
    "watch:tailwind": "npx tailwindcss -i ./tailwind.css -o ./public/style.css -w",
    "watch:live-server": "cd public && npx live-server --port=8080",
    "watch": "run-p watch:*"
  },

...

.gitignore

...

/public/index.js
/public/style.css

ゆきくらげゆきくらげ

GitHub Actions の設定

.github/workflows/main.yml

name: Build and deploy website

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Setup Node.js
      uses: actions/setup-node@v1
      with:
        node-version: 14.x
    - name: Install NPM packages
      run: npm ci
    - name: Build website
      run: npm run bundle
    - name: Delete essential files from .gitignore
      run: sed -i '/index.js/d' .gitignore && sed -i '/style.css/d' .gitignore
    - name: Deploy website
      uses: peaceiris/actions-gh-pages@v3
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        publish_dir: public

GitHub のサイトから Pages を設定

ここまでは定形

ゆきくらげゆきくらげ

開発の進め方

基本的に npm run watch をして PS を書いていく
あとは Push すれば勝手にデプロイされる

npx spago install ... した後は npx spago build する

ゆきくらげゆきくらげ

Halogen Hooks の導入

npx spago install halogen-hooks
npx spago build

src/Main.purs

module Main where

import Prelude

import BrainFk.Components.Body as Body
import Data.Foldable (traverse_)
import Effect (Effect)
import Effect.Aff (launchAff_)
import Halogen.Aff (awaitLoad, selectElement)
import Halogen.VDom.Driver (runUI)
import Web.DOM.ParentNode (QuerySelector(..))

main :: Effect Unit
main = launchAff_ do
  awaitLoad
  body <- selectElement $ QuerySelector "body"
  traverse_ (runUI Body.component unit) body

src/Brainfk/Components/Body.purs

module Brainfk.Components.Body where

import Prelude

import Halogen (Component)
import Halogen.HTML (text)
import Halogen.Hooks as Hooks

component :: forall query input output m. Component query input output m
component = Hooks.component \_ _ -> Hooks.do
  Hooks.pure $ text "Hello, world!!"
このスクラップは2022/10/21にクローズされました