Skip to content

Purs bundle drops var but keeps const and let #4039

@vapaj

Description

@vapaj

Description

When bundling a foreign module, the bundler drops global variables if they are not directly used in an exported function. Interestingly, it seems to depend on how the variable is defined.

Let's say we have a simple program:

foreign import foo :: Effect Int

main :: Effect Unit
main = do
  x <- foo
  log $ show x

And a foreign module:

var a = 1;
exports.foo = function() {
  return a + 1;
}

Everything works as expected. However, if instead of exporting an anonymous function, we would use a named function which we set the value of foo to:

var a = 1;
function foo_() {
  return a + 1;
}
exports.foo = foo_;

When bundling the app and running it with node, the following error occurs:

$ spago build
$ purs bundle "output/*/*.js" -m Main --main Main -o index.js
$ node .

index.js:47
    return a + 1;
    ^

ReferenceError: a is not defined

However, if we declare a with const or let, we still seem to have a in place:

const a = 1;
function foo_() {
  return a + 1;
}
exports.foo = foo_;
$ spago build
$ purs bundle "output/*/*.js" -m Main --main Main -o index.js
$ node .

2

To Reproduce

Test with code provided, then spago build, purs bundle "output/*/*.js" -m Main --main Main -o index.js (which is basically spago bundle-app) and run with node .

Expected behavior

Global variable should not be dropped.

Additional context

PureScript version

0.13.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions