Skip to content

Commit

Permalink
Add skikod8.mjs variant for using skiko.wasm in D8 (shell environment) (
Browse files Browse the repository at this point in the history
#1026)

It's for internal usage only (for example in benchmarks).

- skikoWasmJar will include a new file - skikod8.mjs
- The new file skikod8.mjs won't be used by any actual apps (Compose
apps). The apps won't try to download it or use it.
- This file can be used by manually replace skiko.mjs usages (we'll do
it using a gradle task in benchmarks project)

___
This step is required for https://youtrack.jetbrains.com/issue/CMP-6942
  • Loading branch information
eymar authored Feb 19, 2025
1 parent 852c96b commit a55b230
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
)
}

val configureCommon: LinkSkikoWasmTask.(outputES6: Boolean, prefixPath: String) -> Unit = { outputES6, prefixPath ->
val configureCommon: LinkSkikoWasmTask.(outputES6: Boolean, prefixPath: String, forD8: Boolean) -> Unit = { outputES6, prefixPath, isForD8 ->
val osArch = OS.Wasm to Arch.Wasm

dependsOn(compileWasm)
Expand All @@ -78,19 +78,20 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
buildTargetArch.set(osArch.second)
buildVariant.set(buildType)
if (outputES6) buildSuffix.set("es6")
if (isForD8) buildSuffix.set("d8")

libFiles = project.fileTree(unpackedSkia) { include("**/*.a") }
objectFiles = project.fileTree(compileWasm.map { it.outDir.get() }) {
include("**/*.o")
}

val wasmFileName = if (outputES6) {
"skikomjs.wasm"
if (isForD8) "skikod8.wasm" else "skikomjs.wasm"
} else {
"skiko.wasm" // to keep it compatible with older apps
}
val jsFileName = if (outputES6) {
"skikomjs.mjs"
if (isForD8) "skikod8.mjs" else "skikomjs.mjs"
} else {
"skiko.js" // to keep it compatible with older apps
}
Expand Down Expand Up @@ -129,6 +130,9 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
// "-s", "EXPORT_ALL=1",
)
)
if (isForD8) {
addAll(listOf("-s", "ENVIRONMENT=shell"))
}
}

if (skiko.isWasmBuildWithProfiling) add("--profiling")
Expand All @@ -149,6 +153,7 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
val originalContent = jsFile.readText()
val newContent = originalContent.replace("_org_jetbrains", "org_jetbrains")
.replace("skikomjs.wasm", "skiko.wasm")
.replace("skikod8.wasm", "skiko.wasm")
.replace(isEnvironmentNodeCheckRegex, "if (false) {") // to make webpack erase this part
jsFile.writeText(newContent)

Expand All @@ -157,7 +162,8 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
// It's identical to skiko.wasm and we use skiko.wasm in `skikoWasmJar`task
outDir.file(wasmFileName).get().asFile.delete()

outDir.file(jsFileName).get().asFile.renameTo(outDir.asFile.get().resolve("skiko.mjs"))
val renameTo = if (isForD8) "skikod8.mjs" else "skiko.mjs"
outDir.file(jsFileName).get().asFile.renameTo(outDir.asFile.get().resolve(renameTo))
}
}
}
Expand All @@ -167,12 +173,19 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
val wasmJsTarget = kotlin.wasmJs()
val main by wasmJsTarget.compilations
dependsOn(main.compileTaskProvider)
configureCommon(true, setupMjs.normalize().absolutePath)
configureCommon(true, setupMjs.normalize().absolutePath, false)
}

val linkWasmD8WithES6 by tasks.registering(LinkSkikoWasmTask::class) {
val wasmJsTarget = kotlin.wasmJs()
val main by wasmJsTarget.compilations
dependsOn(main.compileTaskProvider)
configureCommon(true, setupMjs.normalize().absolutePath, true)
}

val linkWasm by tasks.registering(LinkSkikoWasmTask::class) {
dependsOn(linkWasmWithES6)
configureCommon(false, "src/jsWasmMain/resources/setup.js")
configureCommon(false, "src/jsWasmMain/resources/setup.js", false)
}

// skikoWasmJar is used by task name
Expand All @@ -181,6 +194,7 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
// We produce jar that contains .js of wrapper/bindings and .wasm with Skia + bindings.
val wasmOutDir = linkWasm.map { it.outDir }
val wasmEsOutDir = linkWasmWithES6.map { it.outDir }
val wasmD8OutDir = linkWasmD8WithES6.map { it.outDir }

from(wasmOutDir) {
include("*.wasm")
Expand All @@ -190,6 +204,9 @@ fun SkikoProjectContext.createWasmLinkTasks(): LinkWasmTasks = with(this.project
from(wasmEsOutDir) {
include("*.mjs")
}
from(wasmD8OutDir) {
include("*.mjs")
}

archiveBaseName.set("skiko-wasm")
doLast {
Expand Down

0 comments on commit a55b230

Please sign in to comment.