@@ -51,11 +51,12 @@ import Language.PureScript.Make.Cache (CacheDb, ContentHash, normaliseForCache)
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 )
6162
@@ -256,11 +257,12 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
256257 lift $ writeJSONFile coreFnFile json
257258 when (S. member JS codegenTargets) $ do
258259 foreignInclude <- case mn `M.lookup` foreigns of
259- Just _
260+ Just path
260261 | not $ requiresForeign m -> do
261262 return Nothing
262263 | otherwise -> do
263- return $ Just " ./foreign.js"
264+ let ext = if takeExtension path == " .ts" then " .ts" else " .js"
265+ return $ Just (mkString $ T. pack $ " ./foreign" ++ ext)
264266 Nothing | requiresForeign m -> throwError . errorMessage' (CF. moduleSourceSpan m) $ MissingFFIModule mn
265267 | otherwise -> return Nothing
266268 rawJs <- J. moduleToJs m foreignInclude
@@ -331,12 +333,19 @@ data ForeignModuleType = ESModule | CJSModule deriving (Show)
331333-- in its corresponding foreign module.
332334checkForeignDecls :: CF. Module ann -> FilePath -> Make (Either MultipleErrors (ForeignModuleType , S. Set Ident ))
333335checkForeignDecls m path = do
334- jsStr <- T. unpack <$> readTextFile path
335-
336- let
337- parseResult :: Either MultipleErrors JS. JSAST
338- parseResult = first (errorParsingModule . Bundle. UnableToParseModule ) $ JS. parseModule jsStr path
339- traverse checkFFI parseResult
336+ if takeExtension path == " .js"
337+ then do
338+ jsStr <- T. unpack <$> readTextFile path
339+
340+ let
341+ parseResult :: Either MultipleErrors JS. JSAST
342+ parseResult = first (errorParsingModule . Bundle. UnableToParseModule ) $ JS. parseModule jsStr path
343+ traverse checkFFI parseResult
344+ else do
345+ -- We cannot parse non-JS files to check for exports
346+ -- Instead return a successful ES module result without validation
347+ let foreignIdents = S. fromList (CF. moduleForeign m)
348+ return $ Right (ESModule , foreignIdents)
340349
341350 where
342351 mname = CF. moduleName m
@@ -451,5 +460,6 @@ ffiCodegen' foreigns codegenTargets makeOutputPath m = do
451460 where
452461 requiresForeign = not . null . CF. moduleForeign
453462
454- copyForeign path mn =
455- for_ makeOutputPath (\ outputFilename -> copyFile path (outputFilename mn " foreign.js" ))
463+ copyForeign path mn = do
464+ let ext = takeExtension path
465+ for_ makeOutputPath (\ outputFilename -> copyFile path (outputFilename mn (" foreign" ++ ext)))
0 commit comments