代码之家  ›  专栏  ›  技术社区  ›  wizzup

如何在浏览器中从“pulp init”运行代码?

  •  4
  • wizzup  · 技术社区  · 9 年前

    我成功获得了pulp的helloworld purescript 通过运行 nodejs

    $ pulp init
    ...
    
    $ pulp run
    * Building project in /data/works/beta_projects/js_games/processing/hello-ps
    * Build successful.
    Hello sailor!
    

    接下来,我想做 Hello sailor! 通过将消息注入 div 内部文本

    <html>
      <body>
        <div id="output" />
      </body>
    </html>
    

    我需要做哪些步骤?


    pulp server 命令并在 http://localhost:1337/ 无需提供 index.html

    有 pulp browserify 命令打印出我希望包含在 指数html 文件

    1 回复  |  直到 9 年前
        1
  •  6
  •   marcusficner    9 年前

    我只是从PureScript开始,所以肯定有更好的答案,但这是可行的。

    index.html

    <!doctype html>
    <html>
      <body>    
        <script src="/jquery-3.2.1.min.js"></script>
        <script src="/app.js"></script>
        <div id="output"></div>
      </body>
    </html>
    

    实际修改本例使用的DOM https://github.com/purescript-contrib/purescript-jquery ,因此您必须确保在 app.js 正在加载。

    purescript jquery似乎是使用DOM的最简单选项。其他选项包括

    这个 Main.purs 看起来像这样:

    module Main where
    
    import Control.Monad.Eff (Eff)
    import Control.Monad.Eff.Console (CONSOLE)
    import Control.Monad.Eff.JQuery (append, create, body, ready, setText, find)
    import DOM (DOM)
    import Prelude hiding (append)
    
    main :: forall eff. Eff ( dom :: DOM
                            , console :: CONSOLE
                            | eff
                            ) Unit
    main =
      ready $ do
        -- Get the document body
        body <- body
    
        -- Find the div
        div <- find "#output" body
    
        -- Create a paragraph to display a greeting
        greeting <- create "<p>"    
        append greeting div
    
        setText "Hello Sailor!" greeting
    

    跑 pulp browserify 然后 pulp server