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

Add support for multiple subprojects #16

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
83 changes: 69 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
const args = ["--batch", "-no-colors", "-Dsbt.supershell=false", `print ${task}`];
function printSbtTasks(tasks: Array<string>, cwd?: string): Promise<Array<string>> {
const args = ["--batch", "-no-colors", "-Dsbt.supershell=false", ...tasks.map(task => `print ${task}`)];
const options: SpawnOptions = {
cwd: cwd,
stdio: ['ignore', 'pipe', 'inherit'],
Expand All @@ -28,24 +28,68 @@ function printSbtTask(task: string, cwd?: string): Promise<string> {
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you quite sure this always works? Can't we get info lines between the two print results?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can. I am not sure what to do there, though. We could filter lines starting with [info] (and warning etc.) and assume that this is not the actual path. (Technically, many weird characters might occur there, IIRC Linux allows everything but '\0' and '/' in a file name. I am sure newlines would probably break it, too, so it depends how much we want to be perfectionists…)

Other alternative would be calling SBT twice, but this would increate startup time, as starting two SBT instances in parallel sometimes fails. This still wouldn't resolve the issue with newlines, so maybe filtering [info] and similar prefixes would be a better way.

});
});
}

export interface Subproject {
projectID: string | null,
uriPrefix: string,
}

export interface ScalaJSPluginOptions {
cwd?: string,
projectID?: string,
uriPrefix?: string,
subprojects?: Array<Subproject>,
}

export default function scalaJSPlugin(options: ScalaJSPluginOptions = {}): VitePlugin {
const { cwd, projectID, uriPrefix } = options;
function extractSubprojects(options: ScalaJSPluginOptions): Array<Subproject> {
if (options.subprojects) {
if (options.projectID || options.uriPrefix) {
throw new Error("If you specify subprojects, you cannot specify projectID / uriPrefix")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coding style: we use semicolons in this codebase:

Suggested change
throw new Error("If you specify subprojects, you cannot specify projectID / uriPrefix")
throw new Error("If you specify subprojects, you cannot specify projectID / uriPrefix");

This comment applies everywhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

}
return options.subprojects;
} else {
return [
{
projectID: options.projectID || null,
uriPrefix: options.uriPrefix || 'scalajs',
}
];
}
}

function mapBy<T, K>(a: Array<T>, f: ((item: T) => K), itemName: string): Map<K, T> {
const out = new Map<K, T>();
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<T, U>(a: Array<T>, b: Array<U>): 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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variables are unused. So instead of a mapBy, consider simplifying to only check duplicates in an array:

function checkNoDuplicates<T>(a: Array<T>, itemName: string): void

then call it as

Suggested change
const spByProjectID = mapBy(subprojects, (p) => p.projectID, "projectID")
const spByUriPrefix = mapBy(subprojects, (p) => p.uriPrefix, "uriPrefix")
checkNoDuplicates(subprojects.map((p) => p.projectID), "projectID");
checkNoDuplicates(subprojects.map((p) => p.uriPrefix), "uriPrefix");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit undecided there. Sure, this can be the way. OTOH, I suspect those variables might be needed soon.


let isDev: boolean | undefined = undefined;
let scalaJSOutputDir: string | undefined = undefined;
let scalaJSOutputDirs: Map<string, string> | undefined = undefined;

return {
name: "scalajs:sbt-scalajs-plugin",
Expand All @@ -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}`;
},
};
}
88 changes: 88 additions & 0 deletions test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>, options: TestOptions) => void,
description: string,
f: (mode: string, suffix: string) => Promise<void>,
testOptions: TestOptions,
) {
testFunction ||= it
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This like does not seem necessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

const MODES = [["production", MODE_PRODUCTION, "opt"], ["development", MODE_DEVELOPMENT, "fastopt"]]
MODES.forEach( ([modeName, mode, suffix]) => {
testFunction(
description + "(" + modeName + ")",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
description + "(" + modeName + ")",
description + " (" + modeName + ")",

will probably display better.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

async () => await f(mode, suffix),
testOptions,
)
})
}

const MODE_DEVELOPMENT = 'development';
const MODE_PRODUCTION = 'production';

Expand Down Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(await plugin.resolveId.call(fakePluginContext, 'scalajs/main.js'))
.toBeNull();
expect(await plugin.resolveId.call(fakePluginContext, 'scalajs:main.js'))
.toBeNull();

would make more sense, wouldn't it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. BTW, the scalajs/main.js is copied from other tests. Maybe we should fix it in the other places, although it is out of scope of this PR.

}, 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 () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it.fails("when both projectID and subproojects are specified", async () => {
it.fails("when both projectID and subprojects are specified", async () => {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

setup({
projectID: "xxx",
subprojects: []
});
});

it.fails("when both uriPrefix and subproojects are specified", async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it.fails("when both uriPrefix and subproojects are specified", async () => {
it.fails("when both uriPrefix and subprojects are specified", async () => {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

setup({
uriPrefix: "xxx",
subprojects: []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is subprojects: [] something that should be supported at all? Probably not.

Copy link
Author

@v6ak v6ak Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it is a kind of an edge case.

  • Theoretically, one might use it dynamically and no subprojects would equal to disabled plugin.
  • Practically, such use case is unlikely to be common.
  • It also might require the plugin to handle those edge cases. (Well, I am not sure if printSbtTasks handles empty list well…)

So, I'll disable this edge case.

EDIT: disabled

});
});


it("does not work with a project that does not link", async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double blank line:

Suggested change
});
it("does not work with a project that does not link", async () => {
});
it("does not work with a project that does not link", async () => {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

const [plugin, fakePluginContext] = setup({
projectID: "invalidProject",
Expand Down
Loading