Skip to content

Commit

Permalink
Warn on multiple simultaneous invocations, vs erroring out.
Browse files Browse the repository at this point in the history
  • Loading branch information
DDR0 committed Jul 1, 2022
1 parent fcf3e3a commit eb0ac9d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const which = require('which')
const { homedir } = require('os')

const error = (msg) => console.error(chalk.bold.red(msg))
const warn = (msg) => console.warn(chalk.bold.yellow(msg))
let info = (msg) => console.log(chalk.bold.blue(msg))

function findWasmPack() {
Expand Down Expand Up @@ -150,7 +151,7 @@ class WasmPackPlugin {
return runProcess('yarn', ['global', 'add', 'wasm-pack'], {})
} else {
error(
'⚠️ could not install wasm-pack, you must have yarn or npm installed'
' could not install wasm-pack, you must have yarn or npm installed'
)
}
return false
Expand Down Expand Up @@ -225,8 +226,20 @@ function spawnWasmPack({ outDir, outName, isDebug, cwd, args, extraArgs }) {
return runProcess(bin, allArgs, options)
}

let previousRun = Promise.resolve();
const isRunning = Symbol('running')
function runProcess(bin, args, options) {
return new Promise((resolve, reject) => {
return previousRun = new Promise(async (resolve, reject) => {
Promise.race([previousRun, isRunning]).then(status => {
if (status === isRunning) {
warn(`⚠️ Rust compilation invoked more than once at the same time.`)
info(`ℹ️ This will work with some speed penalty, but it represents an error in your webpack configuration and is not guaranteed to continue working.\n`)
}
})

// Don't run two Rust compilers at once - the compilers will interfere
// with each other, one will fail, and the build process will fail.
await previousRun.finally()
const p = spawn(bin, args, options)

p.on('close', (code) => {
Expand Down

0 comments on commit eb0ac9d

Please sign in to comment.