git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add --require-path options to psc-bundle
commit2c7b5341877b279a56401ab35707269c57f6eb2d authorNathan Faubion <nathan@n-son.com> dateTue 3 Nov 2015, 19:50 (11 years ago) parent09f4c2d
2 files changed+24−13
psc-bundle/Main.hs+10−1 src/Language/PureScript/Bundle.hs+14−12
psc-bundle/Main.hs+10−1
@@ -48,6 +48,7 @@ data Options = Options
4848 , optionsEntryPoints :: [String]
4949 , optionsMainModule :: Maybe String
5050 , optionsNamespace :: String
51+ , optionsRequirePath :: Maybe FilePath
5152 } deriving Show
5253
5354-- | Given a filename, assuming it is in the correct place on disk, infer a ModuleIdentifier.
@@ -74,7 +75,7 @@ app Options{..} = do
7475
7576 let entryIds = (map (`ModuleIdentifier` Regular) optionsEntryPoints)
7677
77− bundle input entryIds optionsMainModule optionsNamespace
78+ bundle input entryIds optionsMainModule optionsNamespace optionsRequirePath
7879
7980-- | Command line options parser.
8081options :: Parser Options
@@ -83,6 +84,7 @@ options = Options <$> some inputFile
8384 <*> many entryPoint
8485 <*> optional mainModule
8586 <*> namespace
87+ <*> optional requirePath
8688 where
8789 inputFile :: Parser FilePath
8890 inputFile = strArgument $
@@ -113,6 +115,13 @@ options = Options <$> some inputFile
113115 <> Opts.value "PS"
114116 <> showDefault
115117 <> 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"
116125
117126-- | Make it go.
118127main :: IO ()
src/Language/PureScript/Bundle.hs+14−12
@@ -33,8 +33,8 @@ module Language.PureScript.Bundle (
3333import Prelude ()
3434import Prelude.Compat
3535
36−import Data.List (nub)
37−import Data.Maybe (mapMaybe, catMaybes)
36+import Data.List (nub, stripPrefix)
37+import Data.Maybe (mapMaybe, catMaybes, fromMaybe)
3838import Data.Generics (everything, everywhere, mkQ, mkT)
3939import Data.Graph
4040import Data.Version (showVersion)
@@ -138,12 +138,13 @@ node (NN n) = n
138138node (NT n _ _) = n
139139
140140-- | Calculate the ModuleIdentifier which a require(...) statement imports.
141−checkImportPath :: String -> ModuleIdentifier -> S.Set String -> Maybe ModuleIdentifier
142−checkImportPath "./foreign" m _ =
141+checkImportPath :: Maybe FilePath -> String -> ModuleIdentifier -> S.Set String -> Maybe ModuleIdentifier
142+checkImportPath _ "./foreign" m _ =
143143 Just (ModuleIdentifier (moduleName m) Foreign)
144−checkImportPath name _ names
145− | name `S.member` names = Just (ModuleIdentifier name Regular)
146−checkImportPath _ _ _ = Nothing
144+checkImportPath requirePath name _ names
145+ | Just name' <- stripPrefix (fromMaybe "" requirePath) name
146+ , name' `S.member` names = Just (ModuleIdentifier name' Regular)
147+checkImportPath _ _ _ _ = Nothing
147148
148149-- | Compute the dependencies of all elements in a module, and add them to the tree.
149150--
@@ -209,8 +210,8 @@ withDeps (Module modulePath es) = Module modulePath (map expandDeps es)
209210--
210211-- Each type of module element is matched using pattern guards, and everything else is bundled into the
211212-- Other constructor.
212−toModule :: forall m. (Applicative m, MonadError ErrorMessage m) => S.Set String -> ModuleIdentifier -> JSNode -> m Module
213−toModule mids mid top
213+toModule :: forall m. (Applicative m, MonadError ErrorMessage m) => Maybe FilePath -> S.Set String -> ModuleIdentifier -> JSNode -> m Module
214+toModule requirePath mids mid top
214215 | JSSourceElementsTop ns <- node top = Module mid <$> traverse toModuleElement ns
215216 | otherwise = err InvalidTopLevel
216217 where
@@ -226,7 +227,7 @@ toModule mids mid top
226227 , JSIdentifier "require" <- node req
227228 , JSArguments _ [ impS ] _ <- node impP
228229 , JSStringLiteral _ importPath <- node impS
229− , Just importPath' <- checkImportPath importPath mid mids
230+ , Just importPath' <- checkImportPath requirePath importPath mid mids
230231 = pure (Require n importName importPath')
231232 toModuleElement n
232233 | JSVariables var [ varIntro ] _ <- node n
@@ -535,15 +536,16 @@ bundle :: forall m. (Applicative m, MonadError ErrorMessage m)
535536 -> [ModuleIdentifier] -- ^ Entry points. These module identifiers are used as the roots for dead-code elimination
536537 -> Maybe String -- ^ An optional main module.
537538 -> String -- ^ The namespace (e.g. PS).
539+ -> Maybe FilePath -- ^ The require path prefix
538540 -> m String
539−bundle inputStrs entryPoints mainModule namespace = do
541+bundle inputStrs entryPoints mainModule namespace requirePath = do
540542 input <- forM inputStrs $ \(ident, js) -> do
541543 ast <- either (throwError . ErrorInModule ident . UnableToParseModule) pure $ parse js (moduleName ident)
542544 return (ident, ast)
543545
544546 let mids = S.fromList (map (moduleName . fst) input)
545547
546− modules <- traverse (fmap withDeps . uncurry (toModule mids)) input
548+ modules <- traverse (fmap withDeps . uncurry (toModule requirePath mids)) input
547549
548550 let compiled = compile modules entryPoints
549551 sorted = sortModules (filter (not . isModuleEmpty) compiled)