-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19ee60c
commit a509b7b
Showing
12 changed files
with
149 additions
and
12 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
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,12 @@ | ||
import type { AstNode, ParseAst } from '../../src/rollup/types'; | ||
import { parseAst } from '../../src/utils/parseAst'; | ||
|
||
// We could also do things in a web worker here, but that would make bundling | ||
// the | ||
// browser build much more complicated | ||
const parseAstAsync = async ( | ||
code: Parameters<ParseAst>[0], | ||
options?: Parameters<ParseAst>[1] | ||
): Promise<AstNode> => new Promise(resolve => resolve(parseAst(code, options))); | ||
|
||
export const getParseAstAsync = () => parseAstAsync; |
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,33 @@ | ||
import type { Plugin } from 'rollup'; | ||
|
||
const EMIT_PREFIX = 'emit:'; | ||
|
||
export default function emitFile(): Plugin { | ||
return { | ||
load(id) { | ||
if (id.startsWith(EMIT_PREFIX)) { | ||
const entryId = id.slice(EMIT_PREFIX.length); | ||
const referenceId = this.emitFile({ | ||
id: entryId, | ||
type: 'chunk' | ||
}); | ||
return `export default import.meta.ROLLUP_FILE_URL_${referenceId};`; | ||
} | ||
}, | ||
name: 'emit-file', | ||
resolveFileUrl({ moduleId, relativePath, format }) { | ||
if (moduleId.startsWith(EMIT_PREFIX)) { | ||
return format === 'cjs' | ||
? `__dirname + '/${relativePath}'` | ||
: `new URL('${relativePath}', import.meta.url)`; | ||
} | ||
}, | ||
async resolveId(source, importer) { | ||
if (source.startsWith(EMIT_PREFIX)) { | ||
return `${EMIT_PREFIX}${ | ||
(await this.resolve(source.slice(EMIT_PREFIX.length), importer))!.id | ||
}`; | ||
} | ||
} | ||
}; | ||
} |
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
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
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
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,58 @@ | ||
import { Worker } from 'node:worker_threads'; | ||
import parseWorkerUrl from 'emit:./parseAstWorkerEntry.ts'; | ||
import type { AstNode, ParseAst } from '../rollup/types'; | ||
import { convertProgram } from './convert-ast'; | ||
import getReadStringFunction from './getReadStringFunction'; | ||
|
||
type MessageResolvers = Map<number, [(buffer: Buffer) => void, (error: unknown) => void]>; | ||
|
||
export const getParseAstAsync = () => { | ||
let workerWithResolvers: { worker: Worker; resolvers: MessageResolvers } | null = null; | ||
let nextId = 0; | ||
|
||
const parseToBuffer = (code: string, allowReturnOutsideFunction: boolean): Promise<Buffer> => { | ||
if (!workerWithResolvers) { | ||
const worker = new Worker(parseWorkerUrl); | ||
const resolvers: MessageResolvers = new Map(); | ||
|
||
worker.on('message', ([id, buffer]: [id: number, buffer: Buffer]) => { | ||
resolvers.get(id)![0](buffer); | ||
resolvers.delete(id); | ||
if (resolvers.size === 0) { | ||
// We wait one macro task tick to no close the worker if there are | ||
// additional tasks directly queued up | ||
setTimeout(async () => { | ||
if (resolvers.size === 0) { | ||
workerWithResolvers = null; | ||
await worker.terminate(); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
worker.on('error', error => { | ||
if (workerWithResolvers?.worker === worker) { | ||
workerWithResolvers = null; | ||
for (const [, reject] of resolvers.values()) { | ||
reject(error); | ||
} | ||
resolvers.clear(); | ||
} | ||
}); | ||
workerWithResolvers = { resolvers, worker }; | ||
} | ||
const id = nextId++; | ||
return new Promise<Buffer>((resolve, reject) => { | ||
workerWithResolvers!.resolvers.set(id, [resolve, reject]); | ||
workerWithResolvers!.worker.postMessage([id, code, allowReturnOutsideFunction]); | ||
}); | ||
}; | ||
|
||
return async ( | ||
code: Parameters<ParseAst>[0], | ||
{ allowReturnOutsideFunction = false }: Parameters<ParseAst>[1] = {} | ||
): Promise<AstNode> => { | ||
const astBuffer = await parseToBuffer(code, allowReturnOutsideFunction); | ||
return convertProgram(astBuffer.buffer, getReadStringFunction(astBuffer)); | ||
}; | ||
}; |
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,14 @@ | ||
import { parentPort } from 'node:worker_threads'; | ||
import { parse } from '../../native'; | ||
|
||
parentPort!.on( | ||
'message', | ||
([id, code, allowReturnOutsideFunction]: [ | ||
id: number, | ||
code: string, | ||
allowReturnOutsideFunction: boolean | ||
]) => { | ||
const buffer = parse(code, allowReturnOutsideFunction); | ||
parentPort!.postMessage([id, buffer], [buffer.buffer]); | ||
} | ||
); |
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