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

例如,我想使用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

能够将我的purescript代码构建到逻辑javascript文件中会很好。例如,src/LoginForm.purs可以构建为site/loginForm.jssrc/RegistrationForm.purs可以构建为site/registrationForm.js

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

最佳答案

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处理事情中提取出来的,因此绝对是正确的。

关于purescript - 如何组成纯净的卤素应用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33516671/

10-13 09:11