-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all Prettier logic to the worker
This way the only data that needs to be cloned using the Structured Clone Algorithm is the string contents. This means the Prettier configuration may now use plugins. Because `tsx` was unable to resolve `worker.ts` with a `.js` extension, the code had to be changed to JavaScript. Because of a bug in TypeScript, type definitions had to be written manually. Closes #1
- Loading branch information
1 parent
c113447
commit 2f22eb8
Showing
10 changed files
with
899 additions
and
1,272 deletions.
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ root: true | |
extends: | ||
- remcohaszing | ||
rules: | ||
new-cap: off | ||
import/no-extraneous-dependencies: off |
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
coverage/ | ||
node_modules/ | ||
*.d.ts | ||
*.js | ||
*.log | ||
*.tgz |
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,13 @@ | ||
import { type Options } from 'prettier' | ||
import { type FrozenProcessor } from 'unified' | ||
|
||
/** | ||
* A unified plugin to format output using Prettier. | ||
* | ||
* @param options | ||
* Options to pass to Prettier. | ||
*/ | ||
export default function unifiedPrettier<Processor extends FrozenProcessor<void, void, void, void>>( | ||
this: Processor, | ||
options?: Options | undefined | ||
): undefined |
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,64 @@ | ||
import { resolve } from 'node:path' | ||
import { MessageChannel, receiveMessageOnPort, Worker } from 'node:worker_threads' | ||
|
||
/** @type {Worker} */ | ||
let worker | ||
|
||
/** | ||
* A unified plugin to format output using Prettier. | ||
* | ||
* @param {import('prettier').Options | undefined} [options] | ||
* Options to pass to Prettier. | ||
* @this {import('unified').FrozenProcessor} | ||
*/ | ||
export default function unifiedPrettier(options) { | ||
const { Compiler } = this | ||
|
||
if (!Compiler) { | ||
throw new Error('unified-prettier needs another compiler to be registered first') | ||
} | ||
|
||
this.Compiler = (tree, file) => { | ||
/** @type {unknown} */ | ||
let content | ||
if (Compiler.prototype?.compile) { | ||
const compiler = new /** @type {any} */ (Compiler)(tree, file) | ||
content = compiler.compile() | ||
} else { | ||
content = /** @type {import('unified').CompilerFunction} */ (Compiler)(tree, file) | ||
} | ||
|
||
const filepath = resolve(file.cwd, file.path) | ||
|
||
const signal = new Int32Array(new SharedArrayBuffer(4)) | ||
const { port1: localPort, port2: workerPort } = new MessageChannel() | ||
|
||
if (!worker) { | ||
worker = new Worker(new URL('worker.js', import.meta.url)) | ||
worker.unref() | ||
} | ||
|
||
worker.postMessage( | ||
/** @type {import('./worker.js').Payload} */ ({ | ||
content: String(content), | ||
filepath, | ||
options, | ||
port: workerPort, | ||
signal | ||
}), | ||
[workerPort] | ||
) | ||
|
||
Atomics.wait(signal, 0, 0) | ||
|
||
const { message } = /** @type {{ message: import('./worker.js').Response }} */ ( | ||
receiveMessageOnPort(localPort) | ||
) | ||
|
||
if ('error' in message) { | ||
throw message.error | ||
} | ||
|
||
return message.result | ||
} | ||
} |
Oops, something went wrong.