Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): Implement custom bundle task #894

Merged
merged 11 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/*

Expand Down
2 changes: 1 addition & 1 deletion api/api-npm/index.js
Original file line number Diff line number Diff line change
@@ -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 = [],
Expand Down
2 changes: 1 addition & 1 deletion api/api-npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"files": [
"index.js",
"index.d.ts",
"api-dist-js/main.js",
"aqua-api.js",
"meta-utils.js"
],
"prettier": {},
Expand Down
9 changes: 4 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import BundleJS.*

val aquaVersion = "0.12.1"

val scalaV = "3.3.1"
Expand Down Expand Up @@ -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`)

Expand All @@ -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`)

Expand Down
2 changes: 1 addition & 1 deletion js/js-imports/src/main/scala/aqua/js/Npm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
56 changes: 56 additions & 0 deletions project/BundleJS.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}