Skip to content

Commit bfb4c0e

Browse files
committed
Merge pull request purescript#1164 from purescript/require-path-prefix
Allow a prefix to be specified for require() paths
2 parents 2c43ca9 + 75b0284 commit bfb4c0e

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

psc-make/Main.hs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
108114
noTco :: Parser Bool
109115
noTco = switch $
110116
long "no-tco"
@@ -113,18 +119,18 @@ noTco = switch $
113119
noMagicDo :: Parser Bool
114120
noMagicDo = 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

118124
noOpts :: Parser Bool
119125
noOpts = switch $
120126
long "no-opts"
121-
<> help "Skip the optimization phase."
127+
<> help "Skip the optimization phase"
122128

123129
comments :: Parser Bool
124130
comments = switch $
125131
short 'c'
126132
<> long "comments"
127-
<> help "Include comments in the generated code."
133+
<> help "Include comments in the generated code"
128134

129135
verboseErrors :: Parser Bool
130136
verboseErrors = 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

151160
pscMakeOptions :: Parser PSCMakeOptions
152161
pscMakeOptions = PSCMakeOptions <$> many inputFile

psci/Make.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import qualified Language.PureScript.CoreFn as CF
4343
import IO (mkdirp)
4444

4545
options :: 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

4848
modulesDir :: FilePath
4949
modulesDir = ".psci_modules" ++ pathSeparator : "node_modules"

src/Language/PureScript/CodeGen/JS.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import Language.PureScript.Options
4444
import Language.PureScript.Traversals (sndM)
4545
import 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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ data Mode = Compile | Make
2727
--
2828
data ModeOptions mode where
2929
CompileOptions :: String -> [String] -> [String] -> ModeOptions Compile
30-
MakeOptions :: ModeOptions Make
30+
MakeOptions :: Maybe FilePath -> ModeOptions Make
3131

3232
browserNamespace :: ModeOptions Compile -> String
3333
browserNamespace (CompileOptions ns _ _) = ns
@@ -38,6 +38,9 @@ entryPointModules (CompileOptions _ ms _) = ms
3838
codeGenModules :: ModeOptions Compile -> [String]
3939
codeGenModules (CompileOptions _ _ ms) = ms
4040

41+
requirePath :: ModeOptions Make -> Maybe FilePath
42+
requirePath (MakeOptions mp) = mp
43+
4144
deriving instance Show (ModeOptions mode)
4245

4346
-- |
@@ -85,4 +88,4 @@ defaultCompileOptions = Options False False Nothing False False False (CompileOp
8588
-- Default make options
8689
--
8790
defaultMakeOptions :: Options Make
88-
defaultMakeOptions = Options False False Nothing False False False MakeOptions
91+
defaultMakeOptions = Options False False Nothing False False False (MakeOptions Nothing)

0 commit comments

Comments
 (0)