git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add compiler argument to specify namespace in browser
e24efbcGary Burgess
psc/Main.hs+5−1
@@ -94,8 +94,12 @@ noOpts :: Term Bool
94noOpts = value $ flag $ (optInfo [ "no-opts" ])
94noOpts = value $ flag $ (optInfo [ "no-opts" ])
95 { optDoc = "Skip the optimization phase." }
95 { optDoc = "Skip the optimization phase." }
96
96
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+
97options :: Term P.Options
101options :: Term P.Options
98−options = P.Options <$> tco <*> performRuntimeTypeChecks <*> magicDo <*> runMain <*> noOpts
102+options = P.Options <$> tco <*> performRuntimeTypeChecks <*> magicDo <*> runMain <*> noOpts <*> browserNamespace
99
103
100stdInOrInputFiles :: FilePath -> Term (Maybe [FilePath])
104stdInOrInputFiles :: FilePath -> Term (Maybe [FilePath])
101stdInOrInputFiles prelude = combine <$> useStdIn <*> (not <$> noPrelude) <*> inputFiles
105stdInOrInputFiles prelude = combine <$> useStdIn <*> (not <$> noPrelude) <*> inputFiles
psci/Main.hs+1−1
@@ -35,7 +35,7 @@ getPreludeFilename :: IO FilePath
35getPreludeFilename = Paths.getDataFileName "prelude/prelude.purs"
35getPreludeFilename = Paths.getDataFileName "prelude/prelude.purs"
36
36
37options :: P.Options
37options :: P.Options
38−options = P.Options True False True True True
38+options = P.Options True False True True True "PS"
39
39
40completion :: [P.Module] -> CompletionFunc IO
40completion :: [P.Module] -> CompletionFunc IO
41completion ms = completeWord Nothing " \t\n\r" findCompletions
41completion ms = completeWord Nothing " \t\n\r" findCompletions
src/Language/PureScript.hs+1−1
@@ -67,4 +67,4 @@ compile opts ms = do
67 Left "Main.main is undefined"
67 Left "Main.main is undefined"
68 return $ js ++ [JSApp (JSAccessor "main" (JSAccessor "Main" (JSVar "_ps"))) []]
68 return $ js ++ [JSApp (JSAccessor "main" (JSAccessor "Main" (JSVar "_ps"))) []]
69 | otherwise -> return js
69 | otherwise -> return js
70− return (prettyPrintJS [(wrapExportsContainer js')], exts, env)
70+ return (prettyPrintJS [(wrapExportsContainer opts js')], exts, env)
src/Language/PureScript/CodeGen/JS.hs+6−4
@@ -342,10 +342,12 @@ statementToJs opts m e (If ifst) = ifToJs ifst
342 elseToJs (ElseIf elif) = ifToJs elif
342 elseToJs (ElseIf elif) = ifToJs elif
343statementToJs opts m e (Return value) = JSReturn (valueToJs opts m e value)
343statementToJs opts m e (Return value) = JSReturn (valueToJs opts m e value)
344
344
345−wrapExportsContainer :: [JS] -> JS
345+wrapExportsContainer :: Options -> [JS] -> JS
346−wrapExportsContainer modules = JSApp (JSFunction Nothing ["_ps"] $ JSBlock $ (JSStringLiteral "use strict") : modules) [exportSelector]
346+wrapExportsContainer opts modules = JSApp (JSFunction Nothing ["_ps"] $ JSBlock $ (JSStringLiteral "use strict") : modules) [exportSelector]
347+ where
347− where exportSelector = JSConditional (JSBinary And (JSBinary NotEqualTo (JSTypeOf $ JSVar "module") (JSStringLiteral "undefined")) (JSAccessor "exports" (JSVar "module")))
348+ exportSelector = JSConditional (JSBinary And (JSBinary NotEqualTo (JSTypeOf $ JSVar "module") (JSStringLiteral "undefined")) (JSAccessor "exports" (JSVar "module")))
348 (JSAccessor "exports" (JSVar "module"))
349 (JSAccessor "exports" (JSVar "module"))
349 (JSConditional (JSBinary NotEqualTo (JSTypeOf $ JSVar "window") (JSStringLiteral "undefined"))
350 (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 [])))
351 (JSApp (JSFunction Nothing [] $ JSBlock [JSThrow $ JSStringLiteral "PureScript doesn't know how to export modules in the current environment"]) []))
352 (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+6−1
@@ -39,10 +39,15 @@ data Options = Options {
39 -- Skip all optimizations
39 -- Skip all optimizations
40 --
40 --
41 , optionsNoOptimizations :: Bool
41 , optionsNoOptimizations :: Bool
42+ -- |
43+ -- Specify the namespace that PureScript modules will be exported to when running in the
44+ -- browser.
45+ --
46+ , optionsBrowserNamespace :: String
42 } deriving Show
47 } deriving Show
43
48
44-- |
49-- |
45-- Default compiler options
50-- Default compiler options
46--
51--
47defaultOptions :: Options
52defaultOptions :: Options
48−defaultOptions = Options False False False False False
53+defaultOptions = Options False False False False False "PS"