From cfd4924af2ac45fa3ce47135450448af0af4917c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20=C5=A0est=C3=A1k?= Date: Sat, 21 Oct 2023 18:50:27 +0200 Subject: [PATCH 1/3] Add support for multiple subprojects --- index.ts | 83 ++++++++++++++++++++++++++++++++++-------- test/plugin.test.ts | 88 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+), 14 deletions(-) diff --git a/index.ts b/index.ts index 5bfcf5d..0a2b9ad 100644 --- a/index.ts +++ b/index.ts @@ -2,8 +2,8 @@ import { spawn, SpawnOptions } from "child_process"; import type { Plugin as VitePlugin } from "vite"; // Utility to invoke a given sbt task and fetch its output -function printSbtTask(task: string, cwd?: string): Promise { - const args = ["--batch", "-no-colors", "-Dsbt.supershell=false", `print ${task}`]; +function printSbtTasks(tasks: Array, cwd?: string): Promise> { + const args = ["--batch", "-no-colors", "-Dsbt.supershell=false", ...tasks.map(task => `print ${task}`)]; const options: SpawnOptions = { cwd: cwd, stdio: ['ignore', 'pipe', 'inherit'], @@ -28,24 +28,68 @@ function printSbtTask(task: string, cwd?: string): Promise { if (code !== 0) reject(new Error(`sbt invocation for Scala.js compilation failed with exit code ${code}.`)); else - resolve(fullOutput.trimEnd().split('\n').at(-1)!); + resolve(fullOutput.trimEnd().split('\n').slice(-tasks.length)); }); }); } +export interface Subproject { + projectID: string | null, + uriPrefix: string, +} + export interface ScalaJSPluginOptions { cwd?: string, projectID?: string, uriPrefix?: string, + subprojects?: Array, } -export default function scalaJSPlugin(options: ScalaJSPluginOptions = {}): VitePlugin { - const { cwd, projectID, uriPrefix } = options; +function extractSubprojects(options: ScalaJSPluginOptions): Array { + if (options.subprojects) { + if (options.projectID || options.uriPrefix) { + throw new Error("If you specify subprojects, you cannot specify projectID / uriPrefix") + } + return options.subprojects; + } else { + return [ + { + projectID: options.projectID || null, + uriPrefix: options.uriPrefix || 'scalajs', + } + ]; + } +} + +function mapBy(a: Array, f: ((item: T) => K), itemName: string): Map { + const out = new Map(); + a.forEach((item) => { + const key: K = f(item); + if (out.has(key)) { + throw Error("Duplicate " + itemName + " " + key + "."); + } else { + out.set(key, item); + } + }); + return out; +} + +function zip(a: Array, b: Array): Array<[T, U]> { + if (a.length != b.length) { + throw new Error("length mismatch: " + a.length + " ~= " + b.length) + } + return a.map((item, i) => [item, b[i]]); +} - const fullURIPrefix = uriPrefix ? (uriPrefix + ':') : 'scalajs:'; +export default function scalaJSPlugin(options: ScalaJSPluginOptions = {}): VitePlugin { + const { cwd } = options; + const subprojects = extractSubprojects(options); + // This also checks for duplicates + const spByProjectID = mapBy(subprojects, (p) => p.projectID, "projectID") + const spByUriPrefix = mapBy(subprojects, (p) => p.uriPrefix, "uriPrefix") let isDev: boolean | undefined = undefined; - let scalaJSOutputDir: string | undefined = undefined; + let scalaJSOutputDirs: Map | undefined = undefined; return { name: "scalajs:sbt-scalajs-plugin", @@ -61,20 +105,31 @@ export default function scalaJSPlugin(options: ScalaJSPluginOptions = {}): ViteP throw new Error("configResolved must be called before buildStart"); const task = isDev ? "fastLinkJSOutput" : "fullLinkJSOutput"; - const projectTask = projectID ? `${projectID}/${task}` : task; - scalaJSOutputDir = await printSbtTask(projectTask, cwd); + const projectTasks = subprojects.map( p => + p.projectID ? `${p.projectID}/${task}` : task + ); + const scalaJSOutputDirsArray = await printSbtTasks(projectTasks, cwd); + scalaJSOutputDirs = new Map(zip( + subprojects.map(p => p.uriPrefix), + scalaJSOutputDirsArray + )) }, // standard Rollup resolveId(source, importer, options) { - if (scalaJSOutputDir === undefined) + if (scalaJSOutputDirs === undefined) throw new Error("buildStart must be called before resolveId"); - - if (!source.startsWith(fullURIPrefix)) + const colonPos = source.indexOf(':'); + if (colonPos == -1) { + return null; + } + const subprojectUriPrefix = source.substr(0, colonPos); + const outDir = scalaJSOutputDirs.get(subprojectUriPrefix) + if (outDir == null) return null; - const path = source.substring(fullURIPrefix.length); + const path = source.substring(subprojectUriPrefix.length + 1); - return `${scalaJSOutputDir}/${path}`; + return `${outDir}/${path}`; }, }; } diff --git a/test/plugin.test.ts b/test/plugin.test.ts index f48f503..6cea7d7 100644 --- a/test/plugin.test.ts +++ b/test/plugin.test.ts @@ -18,6 +18,23 @@ function normalizeSlashes(path: string | null): string | null { return path === null ? null : path.replace(/\\/g, '/'); } +function testBothModes( + testFunction: (d: string, func: () => Promise, options: TestOptions) => void, + description: string, + f: (mode: string, suffix: string) => Promise, + testOptions: TestOptions, +) { + testFunction ||= it + const MODES = [["production", MODE_PRODUCTION, "opt"], ["development", MODE_DEVELOPMENT, "fastopt"]] + MODES.forEach( ([modeName, mode, suffix]) => { + testFunction( + description + "(" + modeName + ")", + async () => await f(mode, suffix), + testOptions, + ) + }) +} + const MODE_DEVELOPMENT = 'development'; const MODE_PRODUCTION = 'production'; @@ -102,6 +119,77 @@ describe("scalaJSPlugin", () => { .toBeNull(); }, testOptions); + testBothModes(it, "works with a project with subprojects", async (mode, suffix) => { + const [plugin, fakePluginContext] = setup({ + subprojects: [ + { + projectID: "otherProject", + uriPrefix: "foo", + }, + { + projectID: null, + uriPrefix: "bar", + }, + ] + }); + + await plugin.configResolved.call(undefined, { mode: mode }); + await plugin.buildStart.call(fakePluginContext, {}); + + expect(normalizeSlashes(await plugin.resolveId.call(fakePluginContext, 'foo:main.js'))) + .toContain('/testproject/other-project/target/scala-3.2.2/otherproject-' + suffix + '/main.js'); + expect(normalizeSlashes(await plugin.resolveId.call(fakePluginContext, 'bar:main.js'))) + .toContain('/testproject/target/scala-3.2.2/testproject-' + suffix + '/main.js'); + + expect(await plugin.resolveId.call(fakePluginContext, 'scalajs/main.js')) + .toBeNull(); + }, testOptions); + + it.fails("with duplicate projectID", async () => { + setup({ + subprojects: [ + { + projectID: "otherProject", + uriPrefix: "foo", + }, + { + projectID: "otherProject", + uriPrefix: "bar", + }, + ] + }); + }); + + it.fails("with duplicate uriPrefix", async () => { + setup({ + subprojects: [ + { + projectID: "otherProject", + uriPrefix: "foo", + }, + { + projectID: null, + uriPrefix: "foo", + }, + ] + }); + }); + + it.fails("when both projectID and subproojects are specified", async () => { + setup({ + projectID: "xxx", + subprojects: [] + }); + }); + + it.fails("when both uriPrefix and subproojects are specified", async () => { + setup({ + uriPrefix: "xxx", + subprojects: [] + }); + }); + + it("does not work with a project that does not link", async () => { const [plugin, fakePluginContext] = setup({ projectID: "invalidProject", From 0d986cb9416bbacd19a8d0aae74ed1695c022dc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20=C5=A0est=C3=A1k?= Date: Wed, 25 Oct 2023 19:37:57 +0200 Subject: [PATCH 2/3] Code adjustment according to some PR comments --- index.ts | 13 ++++++++----- test/plugin.test.ts | 38 ++++++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/index.ts b/index.ts index 0a2b9ad..1fd0053 100644 --- a/index.ts +++ b/index.ts @@ -48,7 +48,10 @@ export interface ScalaJSPluginOptions { function extractSubprojects(options: ScalaJSPluginOptions): Array { if (options.subprojects) { if (options.projectID || options.uriPrefix) { - throw new Error("If you specify subprojects, you cannot specify projectID / uriPrefix") + throw new Error("If you specify subprojects, you cannot specify projectID / uriPrefix"); + } + if (options.subprojects.length == 0) { + throw new Error("If you specify subprojects, they shall not be empty."); } return options.subprojects; } else { @@ -76,7 +79,7 @@ function mapBy(a: Array, f: ((item: T) => K), itemName: string): Map(a: Array, b: Array): Array<[T, U]> { if (a.length != b.length) { - throw new Error("length mismatch: " + a.length + " ~= " + b.length) + throw new Error("length mismatch: " + a.length + " ~= " + b.length); } return a.map((item, i) => [item, b[i]]); } @@ -85,8 +88,8 @@ export default function scalaJSPlugin(options: ScalaJSPluginOptions = {}): ViteP const { cwd } = options; const subprojects = extractSubprojects(options); // This also checks for duplicates - const spByProjectID = mapBy(subprojects, (p) => p.projectID, "projectID") - const spByUriPrefix = mapBy(subprojects, (p) => p.uriPrefix, "uriPrefix") + const spByProjectID = mapBy(subprojects, (p) => p.projectID, "projectID"); + const spByUriPrefix = mapBy(subprojects, (p) => p.uriPrefix, "uriPrefix"); let isDev: boolean | undefined = undefined; let scalaJSOutputDirs: Map | undefined = undefined; @@ -112,7 +115,7 @@ export default function scalaJSPlugin(options: ScalaJSPluginOptions = {}): ViteP scalaJSOutputDirs = new Map(zip( subprojects.map(p => p.uriPrefix), scalaJSOutputDirsArray - )) + )); }, // standard Rollup diff --git a/test/plugin.test.ts b/test/plugin.test.ts index 6cea7d7..42728e1 100644 --- a/test/plugin.test.ts +++ b/test/plugin.test.ts @@ -24,15 +24,14 @@ function testBothModes( f: (mode: string, suffix: string) => Promise, testOptions: TestOptions, ) { - testFunction ||= it - const MODES = [["production", MODE_PRODUCTION, "opt"], ["development", MODE_DEVELOPMENT, "fastopt"]] + const MODES = [["production", MODE_PRODUCTION, "opt"], ["development", MODE_DEVELOPMENT, "fastopt"]]; MODES.forEach( ([modeName, mode, suffix]) => { testFunction( description + "(" + modeName + ")", async () => await f(mode, suffix), testOptions, - ) - }) + ); + }); } const MODE_DEVELOPMENT = 'development'; @@ -130,7 +129,7 @@ describe("scalaJSPlugin", () => { projectID: null, uriPrefix: "bar", }, - ] + ], }); await plugin.configResolved.call(undefined, { mode: mode }); @@ -141,7 +140,7 @@ describe("scalaJSPlugin", () => { expect(normalizeSlashes(await plugin.resolveId.call(fakePluginContext, 'bar:main.js'))) .toContain('/testproject/target/scala-3.2.2/testproject-' + suffix + '/main.js'); - expect(await plugin.resolveId.call(fakePluginContext, 'scalajs/main.js')) + expect(await plugin.resolveId.call(fakePluginContext, 'scalajs:main.js')) .toBeNull(); }, testOptions); @@ -156,7 +155,7 @@ describe("scalaJSPlugin", () => { projectID: "otherProject", uriPrefix: "bar", }, - ] + ], }); }); @@ -171,24 +170,39 @@ describe("scalaJSPlugin", () => { projectID: null, uriPrefix: "foo", }, - ] + ], }); }); - it.fails("when both projectID and subproojects are specified", async () => { + it.fails("when both projectID and subprojects are specified", async () => { setup({ projectID: "xxx", - subprojects: [] + subprojects: [ + { + projectID: null, + uriPrefix: "foo", + }, + ], }); }); - it.fails("when both uriPrefix and subproojects are specified", async () => { + it.fails("when both uriPrefix and subprojects are specified", async () => { setup({ uriPrefix: "xxx", - subprojects: [] + subprojects: [ + { + projectID: null, + uriPrefix: "foo", + }, + ], }); }); + it.fails("when empty subprojects are specified", async () => { + setup({ + subprojects: [], + }); + }); it("does not work with a project that does not link", async () => { const [plugin, fakePluginContext] = setup({ From 73ee7cbf2737c24269dd05ad46ae5a4a3c94ac01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20=C5=A0est=C3=A1k?= Date: Wed, 25 Oct 2023 19:41:46 +0200 Subject: [PATCH 3/3] Added missing space --- test/plugin.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/plugin.test.ts b/test/plugin.test.ts index 42728e1..7f1a14a 100644 --- a/test/plugin.test.ts +++ b/test/plugin.test.ts @@ -27,7 +27,7 @@ function testBothModes( const MODES = [["production", MODE_PRODUCTION, "opt"], ["development", MODE_DEVELOPMENT, "fastopt"]]; MODES.forEach( ([modeName, mode, suffix]) => { testFunction( - description + "(" + modeName + ")", + description + " (" + modeName + ")", async () => await f(mode, suffix), testOptions, );