Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.d/fix_bundle-toplevel-function-calls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Do not remove function declarations that are referenced by top-level function calls when bundling
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ If you would prefer to use different terms, please use the section below instead
| [@noraesae](https://github.com/noraesae) | Hyunje Jun | [MIT license](http://opensource.org/licenses/MIT) |
| [@nullobject](https://github.com/nullobject) | Josh Bassett | [MIT license](http://opensource.org/licenses/MIT) |
| [@osa1](https://github.com/osa1) | Ömer Sinan Ağacan | MIT license |
| [@ozkutuk](https://github.com/ozkutuk) | Berk Özkütük | [MIT license](http://opensource.org/licenses/MIT) |
| [@paf31](https://github.com/paf31) | Phil Freeman | [MIT license](http://opensource.org/licenses/MIT) |
| [@parsonsmatt](https://github.com/parsonsmatt) | Matt Parsons | [MIT license](http://opensource.org/licenses/MIT) |
| [@passy](https://github.com/passy) | Pascal Hartig | [MIT license](http://opensource.org/licenses/MIT) |
Expand Down
9 changes: 7 additions & 2 deletions src/Language/PureScript/Bundle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ guessModuleIdentifier filename = ModuleIdentifier (takeFileName (takeDirectory f
data Visibility
= Public
| Internal
| TopLevelCall
deriving (Show, Eq, Ord)

-- | A piece of code is identified by its module, its name, and whether it is an internal variable
Expand Down Expand Up @@ -478,6 +479,10 @@ matchMember stmt
| JSFunction a0 jsIdent a1 args a2 body _ <- stmt
, JSIdentName _ name <- jsIdent
= pure (Internal, name, JSFunctionExpression a0 jsIdent a1 args a2 body)
-- foo(...args);
| JSMethodCall jsIdent a0 args a1 _ <- stmt
, JSIdentifier _ name <- jsIdent
= pure (TopLevelCall, name, JSCallExpression jsIdent a0 args a1)
-- exports.foo = expr; exports["foo"] = expr;
| JSAssignStatement e (JSAssign _) decl _ <- stmt
, Just name <- exportsAccessor e
Expand Down Expand Up @@ -543,8 +548,8 @@ compile modules entryPoints = filteredModules
-- | The set of vertices whose connected components we are interested in keeping.
entryPointVertices :: [Vertex]
entryPointVertices = catMaybes $ do
(_, k@(mid, _, Public), _) <- verts
guard $ mid `elem` entryPoints
(_, k@(mid, _, visibility), _) <- verts
guard $ mid `elem` entryPoints && visibility == Public || visibility == TopLevelCall
return (vertexFor k)

-- | The set of vertices reachable from an entry point
Expand Down
6 changes: 6 additions & 0 deletions tests/purs/bundle/FunctionDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ exports.qux = qux;
var fs = require('fs');
var source = fs.readFileSync(__filename, 'utf-8');
exports.fooIsEliminated = !/^ *var foo/m.test(source);

function localFunction() {
return true;
}

localFunction(); // this should cause localFunction to be bundled