git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add compiler argument to specify namespace in browser
commite24efbcfbdeb7e0c3f5a6c6971bfbb2024db4334 authorGary Burgess <gary.burgess@gmail.com> dateTue 11 Feb 2014, 17:11 (13 years ago) parent5a782ce
5 files changed+19−8
psc/Main.hs+5−1 psci/Main.hs+1−1 src/Language/PureScript.hs+1−1 src/Language/PureScript/CodeGen/JS.hs+6−4 src/Language/PureScript/Options.hs+6−1
psc/Main.hs+5−1
@@ -94,8 +94,12 @@ noOpts :: Term Bool
9494noOpts = 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+
97101options :: Term P.Options
98−options = P.Options <$> tco <*> performRuntimeTypeChecks <*> magicDo <*> runMain <*> noOpts
102+options = P.Options <$> tco <*> performRuntimeTypeChecks <*> magicDo <*> runMain <*> noOpts <*> browserNamespace
99103
100104stdInOrInputFiles :: FilePath -> Term (Maybe [FilePath])
101105stdInOrInputFiles prelude = combine <$> useStdIn <*> (not <$> noPrelude) <*> inputFiles
psci/Main.hs+1−1
@@ -35,7 +35,7 @@ getPreludeFilename :: IO FilePath
3535getPreludeFilename = Paths.getDataFileName "prelude/prelude.purs"
3636
3737options :: P.Options
38−options = P.Options True False True True True
38+options = P.Options True False True True True "PS"
3939
4040completion :: [P.Module] -> CompletionFunc IO
4141completion ms = completeWord Nothing " \t\n\r" findCompletions
src/Language/PureScript.hs+1−1
@@ -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+6−4
@@ -342,10 +342,12 @@ statementToJs opts m e (If ifst) = ifToJs ifst
342342 elseToJs (ElseIf elif) = ifToJs elif
343343statementToJs 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+6−1
@@ -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--
4752defaultOptions :: Options
48−defaultOptions = Options False False False False False
53+defaultOptions = Options False False False False False "PS"