Skip to content

Commit 0ad4e45

Browse files
committed
Add task option to sbt plugin
1 parent 7791944 commit 0ad4e45

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,19 @@ The plugin supports the following configuration options:
5959
export default defineConfig({
6060
plugins: [
6161
scalaJsSbtPlugin({
62-
// path to the directory containing the sbt build
62+
// Path to the directory containing the sbt build
6363
// default: '.'
6464
cwd: '.',
6565

66-
// sbt project ID from within the sbt build to get fast/fullLinkJS from
66+
// Sbt project ID from within the sbt build to get fast/fullLinkJS from
6767
// default: the root project of the sbt build
6868
projectID: 'client',
6969

70+
// Custom sbt task to run. Must return the Scala.js output path,
71+
// either as java.io.File, or as a String of its absolute path.
72+
// default: either 'fastLinkJSOutput' or 'fullLinkJSOutput', depending on NODE_ENV.
73+
task: 'fastLinkJSOutput',
74+
7075
// URI prefix of imports that this plugin catches (without the trailing ':')
7176
// default: 'scalajs' (so the plugin recognizes URIs starting with 'scalajs:')
7277
uriPrefix: 'scalajs',

index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function printSbtTaskImpl(
4949
let errorMessage = `sbt invocation for Scala.js compilation failed with exit code ${code}.`;
5050
if (fullOutput.includes("Not a valid command: --")) {
5151
errorMessage += "\nCause: Your sbt launcher script version is too old (<1.3.3)."
52-
errorMessage += "\nFix: Re-install the latest version of sbt launcher script from https://www.scala-sbt.org/"
52+
errorMessage += "\nFix: Re-install the latest version of sbt launcher script from https://www.scala-sbt.org/"
5353
reject(new Error(errorMessage));
5454
} else if (fullOutput.includes("sbt thinks that server is already booting")) {
5555
if (remainingAttempts > 0) {
@@ -86,6 +86,7 @@ function printWarning(message: String): void {
8686
export interface ScalaJSPluginOptions {
8787
cwd?: string,
8888
projectID?: string,
89+
task?: string,
8990
uriPrefix?: string,
9091
maxAttempts?: number,
9192
retryDelayMs?: number,
@@ -117,11 +118,22 @@ export function scalaJsSbtPlugin(options: ScalaJSPluginOptions = {}): VitePlugin
117118
},
118119

119120
// standard Rollup
120-
async buildStart(options) {
121-
if (isDev === undefined)
121+
async buildStart(buildOptions) {
122+
if (isDev === undefined) {
122123
throw new Error("configResolved must be called before buildStart");
124+
}
123125

124-
const task = isDev ? "fastLinkJSOutput" : "fullLinkJSOutput";
126+
const task = options.task?.trim() || (isDev ? "fastLinkJSOutput" : "fullLinkJSOutput");
127+
if (["fastLinkJS", "fullLinkJS", "fastOptJS", "fullOptJS"].includes(task)) {
128+
// Warn about known-bad tasks to help users out
129+
let errorMessage = `scalaJsSbtPlugin: you provided a known unsupported task '${task}'.`;
130+
errorMessage += "\nFix: Provide either 'fastLinkJSOutput' or 'fullLinkJSOutput'.";
131+
errorMessage += "\n One of those is probably what you want.";
132+
errorMessage += "\n By default, the plugin chooses between them depending on NODE_ENV.";
133+
errorMessage += "\nOr: Provide a custom task that returns the Scala.js output directory,";
134+
errorMessage += "\n either as a java.io.File, or as a String with its absolute path.";
135+
throw new Error(errorMessage);
136+
}
125137
const projectTask = projectID ? `${projectID}/${task}` : task;
126138
scalaJsOutputDir = await printSbtTask(projectTask, cwd, maxAttempts, retryDelayMs);
127139
},
@@ -163,13 +175,15 @@ export function resolveModuleId(
163175
scalaJsOutputDir: string | undefined,
164176
errorMessageIfNoOutputDir: string
165177
): string | null {
166-
if (scalaJsOutputDir === undefined)
178+
if (scalaJsOutputDir === undefined) {
167179
throw new Error(errorMessageIfNoOutputDir);
180+
}
168181

169182
const fullUriPrefix = uriPrefix ? (uriPrefix + ':') : 'scalajs:';
170183

171-
if (!moduleId.startsWith(fullUriPrefix))
184+
if (!moduleId.startsWith(fullUriPrefix)) {
172185
return null;
186+
}
173187

174188
const path = moduleId.substring(fullUriPrefix.length);
175189

0 commit comments

Comments
 (0)