-
Notifications
You must be signed in to change notification settings - Fork 7
feat(babel): add parallel option to transform in worker threads
#139
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
Open
NullVoxPopuli-ai-agent
wants to merge
2
commits into
rolldown:main
Choose a base branch
from
NullVoxPopuli-ai-agent:babel-parallel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 type { PluginOptions } from './options.ts' | ||
| import type { RolldownBabelPresetItem } from './rolldownPreset.ts' | ||
|
|
||
| function isCloneable(value: unknown): boolean { | ||
| if (value === null) return true | ||
| switch (typeof value) { | ||
| case 'undefined': | ||
| case 'string': | ||
| case 'number': | ||
| case 'boolean': | ||
| return true | ||
| case 'object': | ||
| break | ||
| default: | ||
| return false | ||
| } | ||
| if (value instanceof RegExp) return true | ||
| if (Array.isArray(value)) { | ||
| for (const item of value) { | ||
| if (!isCloneable(item)) return false | ||
| } | ||
| return true | ||
| } | ||
| const proto = Object.getPrototypeOf(value) | ||
| if (proto !== Object.prototype && proto !== null) return false | ||
| for (const item of Object.values(value)) { | ||
| if (!isCloneable(item)) return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| function findUncloneablePreset( | ||
| presets: RolldownBabelPresetItem[] | undefined, | ||
| path: string, | ||
| ): string | undefined { | ||
| if (!presets) return | ||
| for (let i = 0; i < presets.length; i++) { | ||
| const preset = presets[i] | ||
| // The `rolldown` part of a preset (filters and hooks) is only used in the main thread. | ||
| const babelPreset = typeof preset === 'object' && 'rolldown' in preset ? preset.preset : preset | ||
| if (!isCloneable(babelPreset)) return `${path}[${i}]` | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the path of the first option that cannot be sent to a worker. | ||
| */ | ||
| export function findUncloneableOption(options: PluginOptions): string | undefined { | ||
| const { presets, overrides, ...rest } = options | ||
| for (const [key, value] of Object.entries(rest)) { | ||
| if (!isCloneable(value)) return key | ||
| } | ||
| const presetPath = findUncloneablePreset(presets, 'presets') | ||
| if (presetPath) return presetPath | ||
| if (!overrides) return | ||
| for (let i = 0; i < overrides.length; i++) { | ||
| const { presets: overridePresets, ...overrideRest } = overrides[i] | ||
| for (const [key, value] of Object.entries(overrideRest)) { | ||
| if (!isCloneable(value)) return `overrides[${i}].${key}` | ||
| } | ||
| const overridePresetPath = findUncloneablePreset(overridePresets, `overrides[${i}].presets`) | ||
| if (overridePresetPath) return overridePresetPath | ||
| } | ||
| } |
This file contains hidden or 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,11 @@ | ||
| export default function identifierReplacePlugin({ types: t }, { name, value }) { | ||
| return { | ||
| visitor: { | ||
| Identifier(p) { | ||
| if (p.node.name === name) { | ||
| p.replaceWith(t.booleanLiteral(value)) | ||
| } | ||
| }, | ||
| }, | ||
| } | ||
| } |
This file contains hidden or 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,5 @@ | ||
| import identifierReplacePlugin from './identifier-replace-plugin.mjs' | ||
|
|
||
| export default function identifierReplacePreset(_api, options) { | ||
| return { plugins: [[identifierReplacePlugin, options]] } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| import { availableParallelism } from 'node:os' | ||
| import { fileURLToPath } from 'node:url' | ||
| import workerpool, { type Pool } from 'workerpool' | ||
| import type { PluginOptions } from './options.ts' | ||
| import { findUncloneableOption } from './cloneable.ts' | ||
|
|
||
| // The source worker is used when this file runs unbundled, for example in tests. | ||
| const WORKER_PATH = fileURLToPath( | ||
| new URL(import.meta.url.endsWith('.ts') ? './worker.ts' : './worker.mjs', import.meta.url), | ||
| ) | ||
|
|
||
| // More workers rarely help, because each worker loads babel and the plugins again. | ||
| const DEFAULT_MAX_WORKERS = 4 | ||
|
|
||
| /** | ||
| * Returns the worker count, or `undefined` when parallel mode is off. | ||
| */ | ||
| export function resolveParallelOption(options: PluginOptions): number | undefined { | ||
| const { parallel } = options | ||
| if (typeof parallel === 'number' && (!Number.isInteger(parallel) || parallel < 1)) { | ||
| throw new Error( | ||
| 'The "parallel" option must be true or a positive integer that sets the worker count.', | ||
| ) | ||
| } | ||
| if (!parallel) return | ||
|
|
||
| const uncloneable = findUncloneableOption(options) | ||
| if (uncloneable) { | ||
| throw new Error( | ||
| `Cannot use the "parallel" option, because "${uncloneable}" cannot be sent to a worker thread. ` + | ||
| 'Refer to plugins and presets by module name instead of by function or object.', | ||
| ) | ||
| } | ||
|
|
||
| return typeof parallel === 'number' | ||
| ? parallel | ||
| : Math.min(availableParallelism(), DEFAULT_MAX_WORKERS) | ||
| } | ||
|
|
||
| export function createWorkerPool(maxWorkers: number): Pool { | ||
| return workerpool.pool(WORKER_PATH, { maxWorkers, workerType: 'thread' }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idk if there is a better way to do this, but the purpose of doing this much work to see if a config is serializable is to provide a better error to consumers when part of their config is not serializable