@@ -34,7 +34,7 @@ import Prelude ()
34import Prelude.Compat
34import Prelude.Compat
36import Data.List (nub, stripPrefix)
36import Data.List (nub, stripPrefix)
37−import Data.Maybe (mapMaybe, catMaybes, fromMaybe)
37+import Data.Maybe (mapMaybe, catMaybes)
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)
@@ -134,13 +134,13 @@ printErrorMessage (ErrorInModule mid e) =
134 name ++ " (" ++ showModuleType ty ++ ")"
134 name ++ " (" ++ showModuleType ty ++ ")"
136-- | Calculate the ModuleIdentifier which a require(...) statement imports.
136-- | Calculate the ModuleIdentifier which a require(...) statement imports.
137−checkImportPath :: Maybe FilePath -> String -> ModuleIdentifier -> S.Set String -> Either String ModuleIdentifier
137+checkImportPath :: String -> ModuleIdentifier -> S.Set String -> Either String ModuleIdentifier
138−checkImportPath _ "./foreign" m _ =
138+checkImportPath "./foreign" m _ =
139 Right (ModuleIdentifier (moduleName m) Foreign)
139 Right (ModuleIdentifier (moduleName m) Foreign)
140−checkImportPath requirePath name _ names
140+checkImportPath name _ names
141− | Just name' <- stripPrefix (fromMaybe "../" requirePath) name
141+ | Just name' <- stripPrefix "../" name
142 , name' `S.member` names = Right (ModuleIdentifier name' Regular)
142 , name' `S.member` names = Right (ModuleIdentifier name' Regular)
143−checkImportPath _ name _ _ = Left name
143+checkImportPath name _ _ = Left name
145-- | Compute the dependencies of all elements in a module, and add them to the tree.
145-- | Compute the dependencies of all elements in a module, and add them to the tree.
@@ -222,8 +222,8 @@ trailingCommaList (JSCTLNone l) = commaList l
223-- Each type of module element is matched using pattern guards, and everything else is bundled into the
223-- Each type of module element is matched using pattern guards, and everything else is bundled into the
224-- Other constructor.
224-- Other constructor.
225−toModule :: forall m. (MonadError ErrorMessage m) => Maybe FilePath -> S.Set String -> ModuleIdentifier -> JSAST -> m Module
225+toModule :: forall m. (MonadError ErrorMessage m) => S.Set String -> ModuleIdentifier -> JSAST -> m Module
226−toModule requirePath mids mid top
226+toModule mids mid top
227 | JSAstProgram smts _ <- top = Module mid <$> traverse toModuleElement smts
227 | JSAstProgram smts _ <- top = Module mid <$> traverse toModuleElement smts
228 | otherwise = err InvalidTopLevel
228 | otherwise = err InvalidTopLevel
@@ -231,7 +231,7 @@ toModule requirePath mids mid top
232 toModuleElement :: JSStatement -> m ModuleElement
232 toModuleElement :: JSStatement -> m ModuleElement
233 toModuleElement stmt
233 toModuleElement stmt
234− | Just (importName, importPath) <- matchRequire requirePath mids mid stmt
234+ | Just (importName, importPath) <- matchRequire mids mid stmt
235 = pure (Require stmt importName importPath)
235 = pure (Require stmt importName importPath)
236 toModuleElement stmt
236 toModuleElement stmt
237 | Just (exported, name, decl) <- matchMember stmt
237 | Just (exported, name, decl) <- matchMember stmt
@@ -291,12 +291,11 @@ getExportedIdentifiers mname top
292-- Matches JS statements like this:
292-- Matches JS statements like this:
293-- var ModuleName = require("file");
293-- var ModuleName = require("file");
294−matchRequire :: Maybe FilePath
295− -> S.Set String
294+matchRequire :: S.Set String
296 -> ModuleIdentifier
295 -> ModuleIdentifier
297 -> JSStatement
296 -> JSStatement
298 -> Maybe (String, Either String ModuleIdentifier)
297 -> Maybe (String, Either String ModuleIdentifier)
299−matchRequire requirePath mids mid stmt
298+matchRequire mids mid stmt
300 | JSVariable _ jsInit _ <- stmt
299 | JSVariable _ jsInit _ <- stmt
301 , [JSVarInitExpression var varInit] <- commaList jsInit
300 , [JSVarInitExpression var varInit] <- commaList jsInit
302 , JSIdentifier _ importName <- var
301 , JSIdentifier _ importName <- var
@@ -304,7 +303,7 @@ matchRequire requirePath mids mid stmt
304 , JSMemberExpression req _ argsE _ <- jsInitEx
303 , JSMemberExpression req _ argsE _ <- jsInitEx
305 , JSIdentifier _ "require" <- req
304 , JSIdentifier _ "require" <- req
306 , [ Just importPath ] <- map fromStringLiteral (commaList argsE)
305 , [ Just importPath ] <- map fromStringLiteral (commaList argsE)
307− , importPath' <- checkImportPath requirePath importPath mid mids
306+ , importPath' <- checkImportPath importPath mid mids
308 = Just (importName, importPath')
307 = Just (importName, importPath')
309 | otherwise
308 | otherwise
310 = Nothing
309 = Nothing
@@ -590,16 +589,15 @@ bundle :: (MonadError ErrorMessage m)
590 -> [ModuleIdentifier] -- ^ Entry points. These module identifiers are used as the roots for dead-code elimination
589 -> [ModuleIdentifier] -- ^ Entry points. These module identifiers are used as the roots for dead-code elimination
591 -> Maybe String -- ^ An optional main module.
590 -> Maybe String -- ^ An optional main module.
592 -> String -- ^ The namespace (e.g. PS).
591 -> String -- ^ The namespace (e.g. PS).
593− -> Maybe FilePath -- ^ The require path prefix
594 -> m String
592 -> m String
595−bundle inputStrs entryPoints mainModule namespace requirePath = do
593+bundle inputStrs entryPoints mainModule namespace = do
596 input <- forM inputStrs $ \(ident, js) -> do
594 input <- forM inputStrs $ \(ident, js) -> do
597 ast <- either (throwError . ErrorInModule ident . UnableToParseModule) pure $ parse js (moduleName ident)
595 ast <- either (throwError . ErrorInModule ident . UnableToParseModule) pure $ parse js (moduleName ident)
598 return (ident, ast)
596 return (ident, ast)
600 let mids = S.fromList (map (moduleName . fst) input)
598 let mids = S.fromList (map (moduleName . fst) input)
602− modules <- traverse (fmap withDeps . uncurry (toModule requirePath mids)) input
600+ modules <- traverse (fmap withDeps . uncurry (toModule mids)) input
604 let compiled = compile modules entryPoints
602 let compiled = compile modules entryPoints
605 sorted = sortModules (filter (not . isModuleEmpty) compiled)
603 sorted = sortModules (filter (not . isModuleEmpty) compiled)