Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions turbopack/crates/turbopack-node/js/src/transforms/postcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare const __turbopack_external_require__: (
) => any

import type { Processor } from 'postcss'
import { workerData } from 'worker_threads'

// @ts-ignore
import postcss from '@vercel/turbopack/postcss'
Expand Down Expand Up @@ -133,8 +134,7 @@ export default async function transform(
}
}

// @ts-ignore
if (typeof self !== 'undefined' && self.workerData && self.workerData.binding) {
if (workerData && workerData.binding) {
// @ts-ignore
const { run } = require('../web_worker/evaluate')
run(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from './transforms'
import fs from 'fs'
import path from 'path'
import { workerData } from 'worker_threads'

export type IpcInfoMessage =
| {
Expand Down Expand Up @@ -509,8 +510,7 @@ const transform = (

export { transform as default }

// @ts-ignore
if (typeof self !== 'undefined' && self.workerData && self.workerData.binding) {
if (workerData && workerData.binding) {
// @ts-ignore
const { run } = require('../web_worker/evaluate')
run(async () => {
Expand Down
13 changes: 3 additions & 10 deletions turbopack/crates/turbopack-node/js/src/web_worker/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@ import {
} from '../worker_thread/taskChannel'
import { structuredError } from '../error'
import type { Channel } from '../types'
import { workerData, threadId as workerId } from 'worker_threads'

export type Self = DedicatedWorkerGlobalScope & {
workerData: {
workerId: number
cwd: string
binding: Binding
}
}
export type Self = DedicatedWorkerGlobalScope

export declare const self: Self
// @ts-ignore
const { workerId } = self.workerData

let binding: Binding = self.workerData.binding
let binding: Binding = workerData.binding
Comment on lines +9 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While moving to the standard worker_threads API is a great improvement, this change loses the type definition for workerData that was previously part of the Self type. To maintain type safety and code clarity, it's a good practice to define an interface for workerData and apply it. This also makes it clear what properties are expected, such as cwd which was present in the old type.

import { workerData as untypedWorkerData, threadId as workerId } from 'worker_threads'

interface TurbopackWorkerData {
  binding: Binding
  cwd: string
}

const workerData = untypedWorkerData as TurbopackWorkerData

export type Self = DedicatedWorkerGlobalScope

export declare const self: Self

let binding: Binding = workerData.binding


binding.workerCreated(workerId)

Expand Down
Loading