git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1164 from purescript/require-path-prefix

Allow a prefix to be specified for require() paths

commitbfb4c0ecc5094672e98b8003812064b71e2b394f authorGary Burgess <gary.burgess@gmail.com> dateSun 14 Jun 2015, 19:10 (11 years ago) parent2c43ca975b0284
4 files changed+23−9
psc-make/Main.hs+13−4 psci/Make.hs+1−1 src/Language/PureScript/CodeGen/JS.hs+4−2 src/Language/PureScript/Options.hs+5−2
psc-make/Main.hs+13−4
@@ -105,6 +105,12 @@ outputDirectory = strOption $
105105 <> showDefault
106106 <> help "The output directory"
107107
108+requirePath :: Parser (Maybe FilePath)
109+requirePath = optional $ strOption $
110+ short 'r'
111+ <> long "require-path"
112+ <> help "The path prefix to use for require() calls in the generated JavaScript"
113+
108114noTco :: Parser Bool
109115noTco = switch $
110116 long "no-tco"
@@ -113,18 +119,18 @@ noTco = switch $
113119noMagicDo :: Parser Bool
114120noMagicDo = switch $
115121 long "no-magic-do"
116− <> help "Disable the optimization that overloads the do keyword to generate efficient code specifically for the Eff monad."
122+ <> help "Disable the optimization that overloads the do keyword to generate efficient code specifically for the Eff monad"
117123
118124noOpts :: Parser Bool
119125noOpts = switch $
120126 long "no-opts"
121− <> help "Skip the optimization phase."
127+ <> help "Skip the optimization phase"
122128
123129comments :: Parser Bool
124130comments = switch $
125131 short 'c'
126132 <> long "comments"
127− <> help "Include comments in the generated code."
133+ <> help "Include comments in the generated code"
128134
129135verboseErrors :: Parser Bool
130136verboseErrors = switch $
@@ -146,7 +152,10 @@ options = P.Options <$> noTco
146152 <*> noOpts
147153 <*> verboseErrors
148154 <*> (not <$> comments)
149− <*> pure P.MakeOptions
155+ <*> additionalOptions
156+ where
157+ additionalOptions =
158+ P.MakeOptions <$> requirePath
150159
151160pscMakeOptions :: Parser PSCMakeOptions
152161pscMakeOptions = PSCMakeOptions <$> many inputFile
psci/Make.hs+1−1
@@ -43,7 +43,7 @@ import qualified Language.PureScript.CoreFn as CF
4343import IO (mkdirp)
4444
4545options :: P.Options P.Make
46−options = P.Options False False Nothing False False False P.MakeOptions
46+options = P.Options False False Nothing False False False (P.MakeOptions Nothing)
4747
4848modulesDir :: FilePath
4949modulesDir = ".psci_modules" ++ pathSeparator : "node_modules"
src/Language/PureScript/CodeGen/JS.hs+4−2
@@ -44,6 +44,8 @@ import Language.PureScript.Options
4444import Language.PureScript.Traversals (sndM)
4545import qualified Language.PureScript.Constants as C
4646
47+import System.FilePath.Posix ((</>))
48+
4749-- |
4850-- Generate code in the simplified Javascript intermediate representation for all declarations in a
4951-- module.
@@ -66,7 +68,7 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign = do
6668 let exps' = JSObjectLiteral $ map (runIdent &&& JSVar . identToJs) standardExps
6769 ++ map (runIdent &&& foreignIdent) foreignExps
6870 return $ case additional of
69− MakeOptions -> moduleBody ++ [JSAssignment (JSAccessor "exports" (JSVar "module")) exps']
71+ MakeOptions _ -> moduleBody ++ [JSAssignment (JSAccessor "exports" (JSVar "module")) exps']
7072 CompileOptions ns _ _ | not isModuleEmpty ->
7173 [ JSVariableIntroduction ns
7274 (Just (JSBinary Or (JSVar ns) (JSObjectLiteral [])) )
@@ -84,7 +86,7 @@ moduleToJs (Module coms mn imps exps foreigns decls) foreign = do
8486 importToJs mn' = do
8587 additional <- asks optionsAdditional
8688 let moduleBody = case additional of
87− MakeOptions -> JSApp (JSVar "require") [JSStringLiteral (runModuleName mn')]
89+ MakeOptions path -> JSApp (JSVar "require") [JSStringLiteral (maybe id (</>) path $ runModuleName mn')]
8890 CompileOptions ns _ _ -> JSAccessor (moduleNameToJs mn') (JSVar ns)
8991 return $ JSVariableIntroduction (moduleNameToJs mn') (Just moduleBody)
9092
src/Language/PureScript/Options.hs+5−2
@@ -27,7 +27,7 @@ data Mode = Compile | Make
2727--
2828data ModeOptions mode where
2929 CompileOptions :: String -> [String] -> [String] -> ModeOptions Compile
30− MakeOptions :: ModeOptions Make
30+ MakeOptions :: Maybe FilePath -> ModeOptions Make
3131
3232browserNamespace :: ModeOptions Compile -> String
3333browserNamespace (CompileOptions ns _ _) = ns
@@ -38,6 +38,9 @@ entryPointModules (CompileOptions _ ms _) = ms
3838codeGenModules :: ModeOptions Compile -> [String]
3939codeGenModules (CompileOptions _ _ ms) = ms
4040
41+requirePath :: ModeOptions Make -> Maybe FilePath
42+requirePath (MakeOptions mp) = mp
43+
4144deriving instance Show (ModeOptions mode)
4245
4346-- |
@@ -85,4 +88,4 @@ defaultCompileOptions = Options False False Nothing False False False (CompileOp
8588-- Default make options
8689--
8790defaultMakeOptions :: Options Make
88−defaultMakeOptions = Options False False Nothing False False False MakeOptions
91+defaultMakeOptions = Options False False Nothing False False False (MakeOptions Nothing)