git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add --require-path options to psc-bundle
2c7b534Nathan Faubion
psc-bundle/Main.hs+10−1
@@ -48,6 +48,7 @@ data Options = Options
48 , optionsEntryPoints :: [String]
48 , optionsEntryPoints :: [String]
49 , optionsMainModule :: Maybe String
49 , optionsMainModule :: Maybe String
50 , optionsNamespace :: String
50 , optionsNamespace :: String
51+ , optionsRequirePath :: Maybe FilePath
51 } deriving Show
52 } deriving Show
52
53
53-- | Given a filename, assuming it is in the correct place on disk, infer a ModuleIdentifier.
54-- | Given a filename, assuming it is in the correct place on disk, infer a ModuleIdentifier.
@@ -74,7 +75,7 @@ app Options{..} = do
74
75
75 let entryIds = (map (`ModuleIdentifier` Regular) optionsEntryPoints)
76 let entryIds = (map (`ModuleIdentifier` Regular) optionsEntryPoints)
76
77
77− bundle input entryIds optionsMainModule optionsNamespace
78+ bundle input entryIds optionsMainModule optionsNamespace optionsRequirePath
78
79
79-- | Command line options parser.
80-- | Command line options parser.
80options :: Parser Options
81options :: Parser Options
@@ -83,6 +84,7 @@ options = Options <$> some inputFile
83 <*> many entryPoint
84 <*> many entryPoint
84 <*> optional mainModule
85 <*> optional mainModule
85 <*> namespace
86 <*> namespace
87+ <*> optional requirePath
86 where
88 where
87 inputFile :: Parser FilePath
89 inputFile :: Parser FilePath
88 inputFile = strArgument $
90 inputFile = strArgument $
@@ -113,6 +115,13 @@ options = Options <$> some inputFile
113 <> Opts.value "PS"
115 <> Opts.value "PS"
114 <> showDefault
116 <> showDefault
115 <> help "Specify the namespace that PureScript modules will be exported to when running in the browser."
117 <> help "Specify the namespace that PureScript modules will be exported to when running in the browser."
118+
119+ requirePath :: Parser FilePath
120+ requirePath = strOption $
121+ short 'r'
122+ <> long "require-path"
123+ <> Opts.value ""
124+ <> help "The path prefix used in require() calls in the generated JavaScript"
116
125
117-- | Make it go.
126-- | Make it go.
118main :: IO ()
127main :: IO ()
src/Language/PureScript/Bundle.hs+14−12
@@ -33,8 +33,8 @@ module Language.PureScript.Bundle (
33import Prelude ()
33import Prelude ()
34import Prelude.Compat
34import Prelude.Compat
35
35
36−import Data.List (nub)
36+import Data.List (nub, stripPrefix)
37−import Data.Maybe (mapMaybe, catMaybes)
37+import Data.Maybe (mapMaybe, catMaybes, fromMaybe)
38import Data.Generics (everything, everywhere, mkQ, mkT)
38import Data.Generics (everything, everywhere, mkQ, mkT)
39import Data.Graph
39import Data.Graph
40import Data.Version (showVersion)
40import Data.Version (showVersion)
@@ -138,12 +138,13 @@ node (NN n) = n
138node (NT n _ _) = n
138node (NT n _ _) = n
139
139
140-- | Calculate the ModuleIdentifier which a require(...) statement imports.
140-- | Calculate the ModuleIdentifier which a require(...) statement imports.
141−checkImportPath :: String -> ModuleIdentifier -> S.Set String -> Maybe ModuleIdentifier
141+checkImportPath :: Maybe FilePath -> String -> ModuleIdentifier -> S.Set String -> Maybe ModuleIdentifier
142−checkImportPath "./foreign" m _ =
142+checkImportPath _ "./foreign" m _ =
143 Just (ModuleIdentifier (moduleName m) Foreign)
143 Just (ModuleIdentifier (moduleName m) Foreign)
144−checkImportPath name _ names
144+checkImportPath requirePath name _ names
145+ | Just name' <- stripPrefix (fromMaybe "" requirePath) name
145− | name `S.member` names = Just (ModuleIdentifier name Regular)
146+ , name' `S.member` names = Just (ModuleIdentifier name' Regular)
146−checkImportPath _ _ _ = Nothing
147+checkImportPath _ _ _ _ = Nothing
147
148
148-- | Compute the dependencies of all elements in a module, and add them to the tree.
149-- | Compute the dependencies of all elements in a module, and add them to the tree.
149--
150--
@@ -209,8 +210,8 @@ withDeps (Module modulePath es) = Module modulePath (map expandDeps es)
209--
210--
210-- Each type of module element is matched using pattern guards, and everything else is bundled into the
211-- Each type of module element is matched using pattern guards, and everything else is bundled into the
211-- Other constructor.
212-- Other constructor.
212−toModule :: forall m. (Applicative m, MonadError ErrorMessage m) => S.Set String -> ModuleIdentifier -> JSNode -> m Module
213+toModule :: forall m. (Applicative m, MonadError ErrorMessage m) => Maybe FilePath -> S.Set String -> ModuleIdentifier -> JSNode -> m Module
213−toModule mids mid top
214+toModule requirePath mids mid top
214 | JSSourceElementsTop ns <- node top = Module mid <$> traverse toModuleElement ns
215 | JSSourceElementsTop ns <- node top = Module mid <$> traverse toModuleElement ns
215 | otherwise = err InvalidTopLevel
216 | otherwise = err InvalidTopLevel
216 where
217 where
@@ -226,7 +227,7 @@ toModule mids mid top
226 , JSIdentifier "require" <- node req
227 , JSIdentifier "require" <- node req
227 , JSArguments _ [ impS ] _ <- node impP
228 , JSArguments _ [ impS ] _ <- node impP
228 , JSStringLiteral _ importPath <- node impS
229 , JSStringLiteral _ importPath <- node impS
229− , Just importPath' <- checkImportPath importPath mid mids
230+ , Just importPath' <- checkImportPath requirePath importPath mid mids
230 = pure (Require n importName importPath')
231 = pure (Require n importName importPath')
231 toModuleElement n
232 toModuleElement n
232 | JSVariables var [ varIntro ] _ <- node n
233 | JSVariables var [ varIntro ] _ <- node n
@@ -535,15 +536,16 @@ bundle :: forall m. (Applicative m, MonadError ErrorMessage m)
535 -> [ModuleIdentifier] -- ^ Entry points. These module identifiers are used as the roots for dead-code elimination
536 -> [ModuleIdentifier] -- ^ Entry points. These module identifiers are used as the roots for dead-code elimination
536 -> Maybe String -- ^ An optional main module.
537 -> Maybe String -- ^ An optional main module.
537 -> String -- ^ The namespace (e.g. PS).
538 -> String -- ^ The namespace (e.g. PS).
539+ -> Maybe FilePath -- ^ The require path prefix
538 -> m String
540 -> m String
539−bundle inputStrs entryPoints mainModule namespace = do
541+bundle inputStrs entryPoints mainModule namespace requirePath = do
540 input <- forM inputStrs $ \(ident, js) -> do
542 input <- forM inputStrs $ \(ident, js) -> do
541 ast <- either (throwError . ErrorInModule ident . UnableToParseModule) pure $ parse js (moduleName ident)
543 ast <- either (throwError . ErrorInModule ident . UnableToParseModule) pure $ parse js (moduleName ident)
542 return (ident, ast)
544 return (ident, ast)
543
545
544 let mids = S.fromList (map (moduleName . fst) input)
546 let mids = S.fromList (map (moduleName . fst) input)
545
547
546− modules <- traverse (fmap withDeps . uncurry (toModule mids)) input
548+ modules <- traverse (fmap withDeps . uncurry (toModule requirePath mids)) input
547
549
548 let compiled = compile modules entryPoints
550 let compiled = compile modules entryPoints
549 sorted = sortModules (filter (not . isModuleEmpty) compiled)
551 sorted = sortModules (filter (not . isModuleEmpty) compiled)