Skip to content

Commit e24efbc

Browse files
committed
Add compiler argument to specify namespace in browser
1 parent 5a782ce commit e24efbc

5 files changed

Lines changed: 19 additions & 8 deletions

File tree

psc/Main.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ noOpts :: Term Bool
9494
noOpts = value $ flag $ (optInfo [ "no-opts" ])
9595
{ optDoc = "Skip the optimization phase." }
9696

97+
browserNamespace :: Term String
98+
browserNamespace = value $ opt "PS" $ (optInfo [ "browser-namespace" ])
99+
{ optDoc = "Specify the namespace that PureScript modules will be exported to when running in the browser." }
100+
97101
options :: Term P.Options
98-
options = P.Options <$> tco <*> performRuntimeTypeChecks <*> magicDo <*> runMain <*> noOpts
102+
options = P.Options <$> tco <*> performRuntimeTypeChecks <*> magicDo <*> runMain <*> noOpts <*> browserNamespace
99103

100104
stdInOrInputFiles :: FilePath -> Term (Maybe [FilePath])
101105
stdInOrInputFiles prelude = combine <$> useStdIn <*> (not <$> noPrelude) <*> inputFiles

psci/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ getPreludeFilename :: IO FilePath
3535
getPreludeFilename = Paths.getDataFileName "prelude/prelude.purs"
3636

3737
options :: P.Options
38-
options = P.Options True False True True True
38+
options = P.Options True False True True True "PS"
3939

4040
completion :: [P.Module] -> CompletionFunc IO
4141
completion ms = completeWord Nothing " \t\n\r" findCompletions

src/Language/PureScript.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ compile opts ms = do
6767
Left "Main.main is undefined"
6868
return $ js ++ [JSApp (JSAccessor "main" (JSAccessor "Main" (JSVar "_ps"))) []]
6969
| otherwise -> return js
70-
return (prettyPrintJS [(wrapExportsContainer js')], exts, env)
70+
return (prettyPrintJS [(wrapExportsContainer opts js')], exts, env)

src/Language/PureScript/CodeGen/JS.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ statementToJs opts m e (If ifst) = ifToJs ifst
342342
elseToJs (ElseIf elif) = ifToJs elif
343343
statementToJs opts m e (Return value) = JSReturn (valueToJs opts m e value)
344344

345-
wrapExportsContainer :: [JS] -> JS
346-
wrapExportsContainer modules = JSApp (JSFunction Nothing ["_ps"] $ JSBlock $ (JSStringLiteral "use strict") : modules) [exportSelector]
347-
where exportSelector = JSConditional (JSBinary And (JSBinary NotEqualTo (JSTypeOf $ JSVar "module") (JSStringLiteral "undefined")) (JSAccessor "exports" (JSVar "module")))
345+
wrapExportsContainer :: Options -> [JS] -> JS
346+
wrapExportsContainer opts modules = JSApp (JSFunction Nothing ["_ps"] $ JSBlock $ (JSStringLiteral "use strict") : modules) [exportSelector]
347+
where
348+
exportSelector = JSConditional (JSBinary And (JSBinary NotEqualTo (JSTypeOf $ JSVar "module") (JSStringLiteral "undefined")) (JSAccessor "exports" (JSVar "module")))
348349
(JSAccessor "exports" (JSVar "module"))
349350
(JSConditional (JSBinary NotEqualTo (JSTypeOf $ JSVar "window") (JSStringLiteral "undefined"))
350-
(JSAssignment (JSAccessor "PS" (JSVar "window")) (JSBinary Or (JSAccessor "PS" (JSVar "window")) (JSObjectLiteral [])))
351+
(JSAssignment (JSAccessor browserNamespace (JSVar "window")) (JSBinary Or (JSAccessor browserNamespace (JSVar "window")) (JSObjectLiteral [])))
351352
(JSApp (JSFunction Nothing [] $ JSBlock [JSThrow $ JSStringLiteral "PureScript doesn't know how to export modules in the current environment"]) []))
353+
browserNamespace = optionsBrowserNamespace opts

src/Language/PureScript/Options.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ data Options = Options {
3939
-- Skip all optimizations
4040
--
4141
, optionsNoOptimizations :: Bool
42+
-- |
43+
-- Specify the namespace that PureScript modules will be exported to when running in the
44+
-- browser.
45+
--
46+
, optionsBrowserNamespace :: String
4247
} deriving Show
4348

4449
-- |
4550
-- Default compiler options
4651
--
4752
defaultOptions :: Options
48-
defaultOptions = Options False False False False False
53+
defaultOptions = Options False False False False False "PS"

0 commit comments

Comments
 (0)