0

When I use pulp build -O -t html/main.js and then pulp build -O -I test -m Test.Main -t html/testmain.js (i.e. building main and test) I get two different js output. In the former case, the format is

// Generated by psc-bundle 0.8.2.0
var PS = { };
(function(exports) {
  // Generated by psc version 0.8.2.0
  "use strict";
  var Prelude = require("../Prelude");
  var Control_Monad_Eff = require("../Control.Monad.Eff");
  exports["main"] = main;
})(PS["Main"] = PS["Main"] || {});
PS["Main"].main();

Please note the require. In the latter case, the require is not in place at all

// Generated by psc-bundle 0.8.2.0
var PS = { };
(function(exports) {
  /* global exports */
  "use strict";

  exports.concatArray = function (xs) {
    return function (ys) {
      return xs.concat(ys);
    };
  };

  exports.showNumberImpl = function (n) {
    /* jshint bitwise: false */
    return n === (n | 0) ? n + ".0" : n.toString();
  };

})(PS["Prelude"] = PS["Prelude"] || {});
(function(exports) {
// Generated by psc version 0.8.2.0
  "use strict";
  var $foreign = PS["Prelude"];
  var Semigroupoid = function (compose) {
      this.compose = compose;
  };

Both examples are shorten, but the point is that require is used in the first time, while not used in the second time. The issue is that I am not able to run the version using require in the browser due to this error

ReferenceError: require is not defined

When I included require.js into page, I got this error

Error: Module name "../Prelude" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded

Thus my question is, what can be done to run the first case in browser.

user2039784
  • 199
  • 9
  • From the error message it seems that Prelude is a dependency of require, so you need to load it before require.js. I do not know, I just guess. – Lajos Arpad Apr 16 '16 at 11:16
  • I think this cannot work. require.js needs to loaded first because it is used by main.js. What you suggest leads to ReferenceError: require is not defined. – user2039784 Apr 16 '16 at 11:28
  • Does Prelude has the dependency of require? – Lajos Arpad Apr 16 '16 at 12:04
  • If you mean Prelude.js as can be found in bower_components\purscript-prelude in my case, then no, it has not. – user2039784 Apr 16 '16 at 12:13

2 Answers2

0

You probably need to use the --browserify option to build the first case for the browser.

gb.
  • 4,629
  • 1
  • 20
  • 19
  • That is true. But the size of the output generated using browserify seems to be much bigger comparing to that generated by build. Anyway, I would like to know why build leads to two different outputs when building main and test. – user2039784 Apr 16 '16 at 13:04
  • I think the difference is due to the `-m` flag in the second example, the first one has no entry point... I could be wrong though. It might be worth updating your `pulp` and `psc` to the latest versions too, it seems `browserify` isn't even present in the `--help` of the latest `pulp`, so perhaps my advice is totally wrong! – gb. Apr 16 '16 at 14:06
  • If no entry point specified, it should be Main by default. However, I tried this: pulp build -O -m Main -t html/main.js, but did not helped. – user2039784 Apr 16 '16 at 15:00
0

My guess would be that this comes from running builds with different --require-path options; once with the old default, which was an empty string, and once with ../. This would lead to psc-bundle not realising it needed to include Prelude and Control.Monad.Eff properly in the first case. psc-bundle should replace those require calls with references to the other modules, so that the code works in browsers.

There are a few different ways this can happen, and the compiler has been updated now in a way that should make the probability of this happening again much lower, so I wouldn't spend too much time worrying about exactly how this has occurred.

If none of the above makes any sense to you, don't worry; I think you just need to do the following to fix this:

  • Update to the latest version of psc (0.8.3 changed the --require-path default to ../, so any version after 0.8.3 should do, but you will want the latest version in most cases)
  • Delete your output directory
  • Compile everything again.
hdgarrood
  • 2,141
  • 16
  • 23