@@ -33,8 +33,8 @@ module Language.PureScript.Bundle (
33import Prelude ()
33import Prelude ()
34import Prelude.Compat
34import Prelude.Compat
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
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
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.
@@ -209,8 +210,8 @@ withDeps (Module modulePath es) = Module modulePath (map expandDeps es)
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
@@ -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)
544 let mids = S.fromList (map (moduleName . fst) input)
546 let mids = S.fromList (map (moduleName . fst) input)
546− modules <- traverse (fmap withDeps . uncurry (toModule mids)) input
548+ modules <- traverse (fmap withDeps . uncurry (toModule requirePath mids)) input
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)