【问题标题】:how to componentize purescript-halogen application如何组件化 purescript-halogen 应用程序
【发布时间】:2016-02-04 15:23:50
【问题描述】:

我想使用 Purescript 的 Halogen 编写前端的特定组件。

例如,我想使用卤素创建一个注册表单。它看起来像下面这样:

module RegistrationForm where

import Prelude

...

-- | The state of the application
newtype State = State { email :: String, password :: String }

derive instance genericState :: Generic State
instance showState :: Show State where show = gShow
instance eqState :: Eq State where eq = gEq

initialState :: State
initialState = State { email: "", password: "" }

-- | Inputs to the state machine
data Input a = FormSubmit a
             | UpdateEmail String a
             | UpdatePassword String a
             | NoAction a

type AppEffects eff = HalogenEffects (ajax :: AJAX, console :: CONSOLE | eff)

ui :: forall eff . Component State Input (Aff (AppEffects eff))
ui = component render eval
  where
    render :: Render State Input
    render (State state) = H.div_
        [ H.h1_ [ H.text "Register" ]
        , ...
        ]

    eval :: Eval Input State Input (Aff (AppEffects eff))
    eval (NoAction next) = pure next
    eval (FormSubmit next) = do
        ...
    eval (UpdateEmail email next) = do
        ...
    eval (UpdatePassword password next) = do
        ...

runRegistrationForm :: Eff (AppEffects ()) Unit
runRegistrationForm = runAff throwException (const (pure unit)) $ do
    { node: node, driver: driver } <- runUI ui initialState
    appendTo ".registration" node

同样,我有一个 LoginForm 模块,用于处理将用户登录到应用程序。

我想知道如何组织我的源代码、构建我的源代码以及从 Javascript 调用我的 Purescript 代码

目前,我的源代码组织如下:

$ cd my-application/
$ tree
.
├── bower.json
├── README.md
├── site/
│   └── index.html
├── src/
│   ├── LoginForm.purs
│   └── RegistrationForm.purs
└── test/
    └── Test.purs

但是,由于我没有Main.purs,我无法执行以下任何操作来构建我的源代码:

$ pulp build --to site/app.js
$ pulp browserify --to site/app.js
$ pulp server

如果能够将我的纯脚本代码构建成逻辑 javascript 文件,那就太好了。例如,src/LoginForm.purs 可以构建为site/loginForm.jssrc/RegistrationForm.purs 可以构建为site/registrationForm.js

然后,我可以根据需要在我的实际 html 页面中包含 loginForm.jsregistrationForm.js

【问题讨论】:

    标签: purescript halogen


    【解决方案1】:

    Pulp 并未真正涵盖此用例,它仅适用于只有一个 Main 的应用。

    我建议使用gulp 设置来实现这一点,使用类似这样的 gulpfile:

    "use strict";
    
    var gulp = require("gulp"),
        purescript = require("gulp-purescript"),
        webpack = require("webpack-stream");
    
    var sources = [
      "src/**/*.purs",
      "bower_components/purescript-*/src/**/*.purs",
    ];
    
    var foreigns = [
      "src/**/*.js",
      "bower_components/purescript-*/src/**/*.js"
    ];
    
    gulp.task("make", function() {
      return purescript.psc({
        src: sources,
        ffi: foreigns
      });
    });
    
    var mkBundleTask = function (name, main) {
    
      gulp.task("prebundle-" + name, ["make"], function() {
        return purescript.pscBundle({
          src: "output/**/*.js",
          output: "tmp/js/" + name + ".js",
          module: main,
          main: main
        });
      });
    
      gulp.task("bundle-" + name, ["prebundle-" + name], function () {
        return gulp.src("tmp/js/" + name + ".js")
          .pipe(webpack({
            resolve: { modulesDirectories: ["node_modules"] },
            output: { filename: name + ".js" }
          }))
          .pipe(gulp.dest("site/js"));
      });
    
      return "bundle-" + name;
    };
    
    gulp.task("bundle", [
      mkBundleTask("loginForm", "LoginForm"),
      mkBundleTask("registrationForm", "RegistrationForm")
    ]);
    
    gulp.task("default", ["bundle"]);
    

    这可能不太正确,但我是从我们如何使用 SlamData 中提取出来的,所以它绝对是正确的。

    【讨论】:

      【解决方案2】:

      这可以通过pulp 实现,使用--to--main 选项:

      pulp build --main LoginForm -O --to site/loginForm.js
      pulp build --main RegistrationForm -O --to site/registrationForm.js
      

      当然,LoginFormRegistrationForm 模块都需要导出一个名为 main 的值才能正常工作。

      【讨论】:

      • 这个解决方案的问题是需要你运行pulp 两次。我需要额外的gulpfileMakefile 来指定pulp 的所有不同调用。我还不如直接使用gulp
      • 这里的正常做法是把它放在 package.json 的“脚本”中。该解决方案确实具有明显的优势,即两行而不是约 40 行。
      • 我对 Javascript 生态系统不是很熟悉。您能否编辑您的答案并添加一个示例package.json 来展示它的外观?或者甚至只是添加一个指向描述它的页面的链接?
      • Pulp 不能单独完成,不(但 npm 不是构建工具)。我不使用纸浆服务器,所以我不知道。
      猜你喜欢
      • 1970-01-01
      • 2016-03-17
      • 2017-11-16
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 2017-03-06
      相关资源
      最近更新 更多