首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将卤素应用组件化

如何将卤素应用组件化
EN

Stack Overflow用户
提问于 2015-11-04 08:07:48
回答 2查看 820关注 0票数 5

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

例如,我希望使用Halogen创建一个注册表单。它看起来如下所示:

代码语言:javascript
复制
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代码

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

代码语言:javascript
复制
$ cd my-application/
$ tree
.
├── bower.json
├── README.md
├── site/
│   └── index.html
├── src/
│   ├── LoginForm.purs
│   └── RegistrationForm.purs
└── test/
    └── Test.purs

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

代码语言:javascript
复制
$ 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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-04 11:48:53

Pulp并没有真正涵盖这个用例,它只针对只有一个Main的应用程序。

我建议使用gulp设置来实现这一点,使用类似于以下内容的吞咽文件:

代码语言:javascript
复制
"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
EN

Stack Overflow用户

发布于 2015-11-26 02:46:23

这在pulp中是可能的,使用--to--main选项:

代码语言:javascript
复制
pulp build --main LoginForm -O --to site/loginForm.js
pulp build --main RegistrationForm -O --to site/registrationForm.js

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33516671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档