-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sjrd/initial-implementation
Initial implementation.
- Loading branch information
Showing
13 changed files
with
2,344 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: Node.js test and Build | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, windows-latest] | ||
node-version: ['16'] | ||
java: ['8'] | ||
|
||
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- name: Set up JDK ${{ matrix.java }} | ||
uses: actions/setup-java@v3 | ||
with: | ||
distribution: 'adopt' | ||
java-version: ${{ matrix.java }} | ||
cache: 'sbt' | ||
|
||
- name: Cache dependencies | ||
uses: actions/cache@v3 | ||
with: | ||
path: | | ||
**/node_modules | ||
key: ${{ runner.os }}-${{ hashFiles('**/package-lock.json') }} | ||
|
||
- name: Install dependencies | ||
run: npm install | ||
- name: Run sbt once in the test project to make sure sbt is downloaded | ||
run: sbt projects | ||
working-directory: ./test/testproject | ||
- name: Perform unit test | ||
run: npm test | ||
- name: Build | ||
run: npm run build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/node_modules/ | ||
/dist/ | ||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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}`]; | ||
const options: SpawnOptions = { | ||
cwd: cwd, | ||
stdio: ['ignore', 'pipe', 'inherit'], | ||
}; | ||
const child = process.platform === 'win32' | ||
? spawn("sbt.bat", args.map(x => `"${x}"`), {shell: true, ...options}) | ||
: spawn("sbt", args, options); | ||
|
||
let fullOutput: string = ''; | ||
|
||
child.stdout!.setEncoding('utf-8'); | ||
child.stdout!.on('data', data => { | ||
fullOutput += data; | ||
process.stdout.write(data); // tee on my own stdout | ||
}); | ||
|
||
return new Promise((resolve, reject) => { | ||
child.on('error', err => { | ||
reject(new Error(`sbt invocation for Scala.js compilation could not start. Is it installed?\n${err}`)); | ||
}); | ||
child.on('close', code => { | ||
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)!); | ||
}); | ||
}); | ||
} | ||
|
||
export interface ScalaJSPluginOptions { | ||
cwd?: string, | ||
projectID?: string, | ||
uriPrefix?: string, | ||
} | ||
|
||
export default function scalaJSPlugin(options: ScalaJSPluginOptions = {}): VitePlugin { | ||
const { cwd, projectID, uriPrefix } = options; | ||
|
||
const fullURIPrefix = uriPrefix ? (uriPrefix + ':') : 'scalajs:'; | ||
|
||
let isDev: boolean | undefined = undefined; | ||
let scalaJSOutputDir: string | undefined = undefined; | ||
|
||
return { | ||
name: "scalajs:sbt-scalajs-plugin", | ||
|
||
// Vite-specific | ||
configResolved(resolvedConfig) { | ||
isDev = resolvedConfig.mode === 'development'; | ||
}, | ||
|
||
// standard Rollup | ||
async buildStart(options) { | ||
if (isDev === undefined) | ||
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); | ||
}, | ||
|
||
// standard Rollup | ||
resolveId(source, importer, options) { | ||
if (scalaJSOutputDir === undefined) | ||
throw new Error("buildStart must be called before resolveId"); | ||
|
||
if (!source.startsWith(fullURIPrefix)) | ||
return null; | ||
const path = source.substring(fullURIPrefix.length); | ||
|
||
return `${scalaJSOutputDir}/${path}`; | ||
}, | ||
}; | ||
} |
Oops, something went wrong.