forked from scala-cli/scala-js-cli
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support remapping imports at link time (#47)
* Maps work - struggling with the facade, though * success! * add back previuos tests * Dont fold over ir files
- Loading branch information
Showing
5 changed files
with
202 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
/.bsp/ | ||
out/ | ||
.idea/ | ||
.metals | ||
.vscode | ||
.bloop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.scalajs.cli.internal | ||
|
||
import com.github.plokhotnyuk.jsoniter_scala.core._ | ||
import com.github.plokhotnyuk.jsoniter_scala.macros._ | ||
import org.scalajs.linker.interface.IRFile | ||
import java.io.File | ||
import com.armanbilge.sjsimportmap.ImportMappedIRFile | ||
|
||
object ImportMapJsonIr { | ||
|
||
type Scope = Map[String, String] | ||
|
||
final case class ImportMap( | ||
val imports: Map[String, String], | ||
val scopes: Option[Map[String, Scope]] | ||
) | ||
|
||
object ImportMap { | ||
implicit val codec: JsonValueCodec[ImportMap] = JsonCodecMaker.make | ||
} | ||
|
||
def remapImports(pathToImportPath: File, irFiles: Seq[IRFile]): Seq[IRFile] = { | ||
val path = os.Path(pathToImportPath) | ||
val importMapJson = if(os.exists(path))( | ||
readFromString[ImportMap](os.read(path)) | ||
) else { | ||
throw new AssertionError(s"importmap file at path ${path} does not exist.") | ||
} | ||
if (importMapJson.scopes.nonEmpty) { | ||
throw new AssertionError("importmap scopes are not supported.") | ||
} | ||
val importsOnly : Map[String, String] = importMapJson.imports | ||
|
||
val remapFct = importsOnly.toSeq.foldLeft((in: String) => in){ case(fct, (s1, s2)) => | ||
val fct2 : (String => String) = (in => in.replace(s1, s2)) | ||
(in => fct(fct2(in))) | ||
} | ||
|
||
irFiles.map{ImportMappedIRFile.fromIRFile(_)(remapFct)} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters