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 apps: sbt
- name: scala-js build - 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 - name: Import secrets
uses: hashicorp/vault-action@v2.7.3 uses: hashicorp/vault-action@v2.7.3

View File

@ -51,7 +51,7 @@ jobs:
- name: scala-js build - name: scala-js build
env: env:
SNAPSHOT: ${{ steps.version.outputs.id }} 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 - name: Import secrets
uses: hashicorp/vault-action@v2.7.3 uses: hashicorp/vault-action@v2.7.3

View File

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

4
.gitignore vendored
View File

@ -9,8 +9,8 @@ project/target
.DS_Store .DS_Store
language-server/language-server-npm/aqua-lsp-api.j* language-server/language-server-npm/aqua-lsp-api.js
api/api-npm/api-dist-js api/api-npm/aqua-api.js
integration-tests/src/compiled/* 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({ function getConfig({
constants = [], constants = [],

View File

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

View File

@ -1,3 +1,5 @@
import BundleJS.*
val aquaVersion = "0.12.1" val aquaVersion = "0.12.1"
val scalaV = "3.3.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 lazy val `language-server-apiJS` = `language-server-api`.js
.settings( .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)), scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule)),
scalaJSUseMainModuleInitializer := true scalaJSUseMainModuleInitializer := true
) )
.settings(addBundleJS("../../language-server-npm/aqua-lsp-api.js"))
.enablePlugins(ScalaJSPlugin) .enablePlugins(ScalaJSPlugin)
.dependsOn(`js-exports`, `js-imports`) .dependsOn(`js-exports`, `js-imports`)
@ -104,12 +104,11 @@ lazy val `aqua-api` = crossProject(JSPlatform, JVMPlatform)
lazy val `aqua-apiJS` = `aqua-api`.js lazy val `aqua-apiJS` = `aqua-api`.js
.settings( .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)), scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.ESModule)),
scalaJSUseMainModuleInitializer := true, scalaJSUseMainModuleInitializer := true,
Test / test := {} Test / test := {}
) )
.settings(addBundleJS("../../api-npm/aqua-api.js"))
.enablePlugins(ScalaJSPlugin) .enablePlugins(ScalaJSPlugin)
.dependsOn(`js-exports`) .dependsOn(`js-exports`)

View File

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