feat(build): Implement custom bundle task (#894)

* Add link settings

* fix

* Rename to api-dist-js

* Correct import

* Update CI

* Implement bundleJS

* Add comments

* Add TODO

* Fix import

* Fix workflow

---------

Co-authored-by: Artsiom Shamsutdzinau <shamsartem@gmail.com>
This commit is contained in:
InversionSpaces 2023-09-18 09:53:25 +02:00 committed by GitHub
parent 3f916c78ab
commit 67d8151d94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 68 additions and 13 deletions

View File

@ -41,7 +41,7 @@ jobs:
apps: sbt
- name: scala-js build
run: sbt ";language-server-apiJS/fullOptJS;aqua-apiJS/fullLinkJS"
run: sbt ";language-server-apiJS/fullBundleJS;aqua-apiJS/fullBundleJS"
- name: Import secrets
uses: hashicorp/vault-action@v2.7.3

View File

@ -51,7 +51,7 @@ jobs:
- name: scala-js build
env:
SNAPSHOT: ${{ steps.version.outputs.id }}
run: sbt ";language-server-apiJS/fastOptJS;aqua-apiJS/fastLinkJS"
run: sbt ";language-server-apiJS/fastBundleJS;aqua-apiJS/fastBundleJS"
- name: Import secrets
uses: hashicorp/vault-action@v2.7.3

View File

@ -82,7 +82,7 @@ jobs:
apps: sbt
- name: aqua-api build
run: sbt "aqua-apiJS/fastLinkJS"
run: sbt "aqua-apiJS/fastBundleJS"
- name: Setup pnpm
uses: pnpm/action-setup@v2.4.0

4
.gitignore vendored
View File

@ -9,8 +9,8 @@ project/target
.DS_Store
language-server/language-server-npm/aqua-lsp-api.j*
api/api-npm/api-dist-js
language-server/language-server-npm/aqua-lsp-api.js
api/api-npm/aqua-api.js
integration-tests/src/compiled/*

View File

@ -1,4 +1,4 @@
import { AquaConfig, Aqua, Call, Input, Path } from "./api-dist-js/main.js";
import { AquaConfig, Aqua, Call, Input, Path } from "./aqua-api.js";
function getConfig({
constants = [],

View File

@ -7,7 +7,7 @@
"files": [
"index.js",
"index.d.ts",
"api-dist-js/main.js",
"aqua-api.js",
"meta-utils.js"
],
"prettier": {},

View File

@ -1,3 +1,5 @@
import BundleJS.*
val aquaVersion = "0.12.1"
val scalaV = "3.3.1"
@ -74,12 +76,10 @@ lazy val `language-server-api` = crossProject(JSPlatform, JVMPlatform)
lazy val `language-server-apiJS` = `language-server-api`.js
.settings(
// TODO: move to fast/fullLinkJS here
Compile / fastOptJS / artifactPath := baseDirectory.value / "../../language-server-npm" / "aqua-lsp-api.js",
Compile / fullOptJS / artifactPath := baseDirectory.value / "../../language-server-npm" / "aqua-lsp-api.js",
scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule)),
scalaJSUseMainModuleInitializer := true
)
.settings(addBundleJS("../../language-server-npm/aqua-lsp-api.js"))
.enablePlugins(ScalaJSPlugin)
.dependsOn(`js-exports`, `js-imports`)
@ -104,12 +104,11 @@ lazy val `aqua-api` = crossProject(JSPlatform, JVMPlatform)
lazy val `aqua-apiJS` = `aqua-api`.js
.settings(
Compile / fastLinkJS / scalaJSLinkerOutputDirectory := baseDirectory.value / "../../api-npm/api-dist-js",
Compile / fullLinkJS / scalaJSLinkerOutputDirectory := baseDirectory.value / "../../api-npm/api-dist-js",
scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.ESModule)),
scalaJSUseMainModuleInitializer := true,
Test / test := {}
)
.settings(addBundleJS("../../api-npm/aqua-api.js"))
.enablePlugins(ScalaJSPlugin)
.dependsOn(`js-exports`)

View File

@ -9,7 +9,7 @@ object Meta {
// it is needed for `createRequire` function
// TODO: Investigate if it is really needed
@js.native
@JSImport("../meta-utils.js", "metaUrl")
@JSImport("./meta-utils.js", "metaUrl")
val metaUrl: String = js.native
}

56
project/BundleJS.scala Normal file
View File

@ -0,0 +1,56 @@
import sbt.*
import sbt.Keys.*
import org.scalajs.linker.interface.Report
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport.*
/**
* Utility to add bundling js functionality to a project.
*/
object BundleJS {
// Bundle full js (result of fullLinkJS)
val fullBundleJS = taskKey[Unit]("Full bundle JS")
// Bundle fast js (result of fastLinkJS)
val fastBundleJS = taskKey[Unit]("Fast bundle JS")
/**
* Add full/fast bundle JS tasks to a project.
*
* @param outputFilePath **relative to baseDirectory** path to output file
* @return Seq of settings with tasks
*/
def addBundleJS(
outputFilePath: String // TODO: Accept `File`
) = Seq(
fullBundleJS := Def.taskDyn {
bundleJS(fullLinkJS, outputFilePath)
}.value,
fastBundleJS := Def.taskDyn {
bundleJS(fastLinkJS, outputFilePath)
}.value
)
private def bundleJS(
linkJSTask: TaskKey[Attributed[Report]],
outputFilePath: String
) = Def.taskDyn {
val logger = streams.value.log
val jsDir = (Compile / linkJSTask / scalaJSLinkerOutputDirectory).value
val linkResult = (Compile / linkJSTask).value
val outputFile = baseDirectory.value / outputFilePath
linkResult.data.publicModules.toList match {
case Nil =>
throw new RuntimeException("No public modules generated")
case _ :: _ :: _ =>
throw new RuntimeException("More than one public module generated")
case module :: Nil =>
val jsFile = jsDir / module.jsFileName
Def.task {
logger.info(s"Copying $jsFile to $outputFile")
IO.copyFile(jsFile, outputFile)
}
}
}
}