@@ -51,11 +51,12 @@ import Language.PureScript.Make.Cache (CacheDb, ContentHash, cacheDbIsCurrentVer
5151import Language.PureScript.Names (Ident (.. ), ModuleName , runModuleName )
5252import Language.PureScript.Options (CodegenTarget (.. ), Options (.. ))
5353import Language.PureScript.Pretty.Common (SMap (.. ))
54+ import Language.PureScript.PSString (mkString )
5455import Paths_purescript qualified as Paths
5556import SourceMap (generate )
5657import SourceMap.Types (Mapping (.. ), Pos (.. ), SourceMapping (.. ))
5758import System.Directory (getCurrentDirectory )
58- import System.FilePath ((</>) , makeRelative , splitPath , normalise , splitDirectories )
59+ import System.FilePath ((</>) , makeRelative , splitPath , normalise , splitDirectories , takeExtension )
5960import System.FilePath.Posix qualified as Posix
6061import System.IO (stderr )
6162import Language.PureScript.Make.IdeCache ( sqliteExtern , sqliteInit )
@@ -299,11 +300,12 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
299300 lift $ writeJSONFile coreFnFile json
300301 when (S. member JS codegenTargets) $ do
301302 foreignInclude <- case mn `M.lookup` foreigns of
302- Just _
303+ Just path
303304 | not $ requiresForeign m -> do
304305 return Nothing
305306 | otherwise -> do
306- return $ Just " ./foreign.js"
307+ let ext = if takeExtension path == " .ts" then " .ts" else " .js"
308+ return $ Just (mkString $ T. pack $ " ./foreign" ++ ext)
307309 Nothing | requiresForeign m -> throwError . errorMessage' (CF. moduleSourceSpan m) $ MissingFFIModule mn
308310 | otherwise -> return Nothing
309311 rawJs <- J. moduleToJs m foreignInclude
@@ -375,12 +377,19 @@ data ForeignModuleType = ESModule | CJSModule deriving (Show)
375377checkForeignDecls :: CF. Module ann -> FilePath -> Make (Either MultipleErrors (ForeignModuleType , S. Set Ident ))
376378-- checkForeignDecls :: CF.Module ann -> FilePath -> Make (ForeignModuleType, S.Set Ident
377379checkForeignDecls m path = do
378- jsStr <- T. unpack <$> readTextFile path
379-
380- let
381- parseResult :: Either MultipleErrors JS. JSAST
382- parseResult = first (errorParsingModule . Bundle. UnableToParseModule ) $ JS. parseModule jsStr path
383- traverse checkFFI parseResult
380+ if takeExtension path == " .js"
381+ then do
382+ jsStr <- T. unpack <$> readTextFile path
383+
384+ let
385+ parseResult :: Either MultipleErrors JS. JSAST
386+ parseResult = first (errorParsingModule . Bundle. UnableToParseModule ) $ JS. parseModule jsStr path
387+ traverse checkFFI parseResult
388+ else do
389+ -- We cannot parse non-JS files to check for exports
390+ -- Instead return a successful ES module result without validation
391+ let foreignIdents = S. fromList (CF. moduleForeign m)
392+ return $ Right (ESModule , foreignIdents)
384393
385394 where
386395 mname = CF. moduleName m
@@ -495,5 +504,6 @@ ffiCodegen' foreigns codegenTargets makeOutputPath m = do
495504 where
496505 requiresForeign = not . null . CF. moduleForeign
497506
498- copyForeign path mn =
499- for_ makeOutputPath (\ outputFilename -> copyFile path (outputFilename mn " foreign.js" ))
507+ copyForeign path mn = do
508+ let ext = takeExtension path
509+ for_ makeOutputPath (\ outputFilename -> copyFile path (outputFilename mn (" foreign" ++ ext)))
0 commit comments