Skip to content

Commit 314961e

Browse files
committed
wip
1 parent c96f310 commit 314961e

3 files changed

Lines changed: 44 additions & 16 deletions

File tree

app/Command/Compile.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,22 @@ targetParser =
133133
. T.unpack
134134
. T.strip
135135

136+
ffiExtensions :: Opts.Parser [String]
137+
ffiExtensions = Opts.option targetParser $
138+
Opts.long "ffi-exts"
139+
<> Opts.value ["js"]
140+
<> Opts.help
141+
( "Specifies comma-separated file extensions to consider for foriegn module implementations. "
142+
<> "Defaults to js"
143+
)
144+
136145
options :: Opts.Parser P.Options
137146
options =
138147
P.Options
139148
<$> verboseErrors
140149
<*> (not <$> comments)
141150
<*> (handleTargets <$> codegenTargets)
151+
<*> (S.fromList <$> ffiExtensions)
142152
where
143153
-- Ensure that the JS target is included if sourcemaps are
144154
handleTargets :: [P.CodegenTarget] -> S.Set P.CodegenTarget

src/Language/PureScript/Make.hs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ make' MakeOptions{..} ma@MakeActions{..} ms = do
367367
BuildPlan.markComplete buildPlan moduleName result
368368

369369
-- | Infer the module name for a module by looking for the same filename with
370-
-- a .js extension.
370+
-- a .js or .ts extension.
371371
inferForeignModules
372372
:: forall m
373373
. MonadIO m
@@ -379,8 +379,16 @@ inferForeignModules =
379379
inferForeignModule :: Either RebuildPolicy FilePath -> m (Maybe FilePath)
380380
inferForeignModule (Left _) = return Nothing
381381
inferForeignModule (Right path) = do
382-
let jsFile = replaceExtension path "js"
383-
exists <- liftIO $ doesFileExist jsFile
384-
if exists
382+
let
383+
jsFile = replaceExtension path "js"
384+
tsFile = replaceExtension path "ts"
385+
existsJs <- liftIO $ doesFileExist jsFile
386+
387+
if existsJs
385388
then return (Just jsFile)
386-
else return Nothing
389+
else do
390+
existsTs <- liftIO $ doesFileExist tsFile
391+
if existsTs
392+
then return (Just tsFile)
393+
else return Nothing
394+

src/Language/PureScript/Make/Actions.hs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ import Language.PureScript.Make.Cache (CacheDb, ContentHash, cacheDbIsCurrentVer
5151
import Language.PureScript.Names (Ident(..), ModuleName, runModuleName)
5252
import Language.PureScript.Options (CodegenTarget(..), Options(..))
5353
import Language.PureScript.Pretty.Common (SMap(..))
54+
import Language.PureScript.PSString (mkString)
5455
import Paths_purescript qualified as Paths
5556
import SourceMap (generate)
5657
import SourceMap.Types (Mapping(..), Pos(..), SourceMapping(..))
5758
import System.Directory (getCurrentDirectory)
58-
import System.FilePath ((</>), makeRelative, splitPath, normalise, splitDirectories)
59+
import System.FilePath ((</>), makeRelative, splitPath, normalise, splitDirectories, takeExtension)
5960
import System.FilePath.Posix qualified as Posix
6061
import System.IO (stderr)
6162
import 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)
375377
checkForeignDecls :: CF.Module ann -> FilePath -> Make (Either MultipleErrors (ForeignModuleType, S.Set Ident))
376378
-- checkForeignDecls :: CF.Module ann -> FilePath -> Make (ForeignModuleType, S.Set Ident
377379
checkForeignDecls 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

Comments
 (0)