Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 14, 2025

Fixes the issue where __dirname and __filename checks were broken when running Vite through --eval based scripts, as reported by @AriPerkkio in #919.

Problem

The plugin was using conditional checks to support both ESM and CJS environments:

const _dirname =
  typeof __dirname !== 'undefined'
    ? __dirname
    : dirname(fileURLToPath(import.meta.url))
const resolve = createRequire(
  typeof __filename !== 'undefined' ? __filename : import.meta.url,
).resolve

These checks fail in certain execution contexts, particularly when running Vite through --eval based scripts, because the typeof __dirname !== 'undefined' check behaves unexpectedly in those environments.

Solution

Since @vitejs/plugin-react-swc is now an ESM-only package (has "type": "module" in package.json), we can safely remove these compatibility checks and use import.meta.url and import.meta.dirname directly:

const resolve = createRequire(import.meta.url).resolve
// ...
join(import.meta.dirname, 'refresh-runtime.js')

This simplification:

  • Removes unnecessary conditional logic that was only needed for ESM/CJS interop
  • Fixes the issue with --eval based scripts
  • Uses the native import.meta.dirname instead of manually computing the directory path
  • Makes the code cleaner and more straightforward

Testing

  • ✅ All 21 Playwright tests pass
  • ✅ Unit tests pass
  • ✅ Build completes successfully
  • ✅ Linting passes
  • ✅ Module loads and instantiates correctly

Fixes #919

Original prompt

This section details on the original issue you should resolve

<issue_title>Remove __filename and __dirname check from packages/plugin-react-swc/src/index.ts</issue_title>
<issue_description>

/* eslint-disable no-restricted-globals */
const _dirname =
typeof __dirname !== 'undefined'
? __dirname
: dirname(fileURLToPath(import.meta.url))
const resolve = createRequire(
typeof __filename !== 'undefined' ? __filename : import.meta.url,
).resolve
/* eslint-enable no-restricted-globals */

@AriPerkkio reported this check is broken when running vite through --eval based script. Probably this was only meant for esm/cjs support, but the package is now esm only, so we can probably remove this.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #919


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] Remove __filename and __dirname check from index.ts Remove __filename and __dirname checks from plugin-react-swc Oct 14, 2025
@Copilot Copilot AI requested a review from hi-ogawa October 14, 2025 11:31
Copilot finished work on behalf of hi-ogawa October 14, 2025 11:31
Copy link

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

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

Thanks!

@sapphi-red
Copy link
Member

@copilot let's use import.meta.dirname instead of _dirname as that is supported

Copy link
Contributor Author

Copilot AI commented Oct 14, 2025

@copilot let's use import.meta.dirname instead of _dirname as that is supported

Done in commit de4e140. Replaced _dirname computation with import.meta.dirname and removed the now-unused imports of dirname and fileURLToPath.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • emotion.sh
    • Triggering command: /home/REDACTED/.cache/ms-playwright/chromium_headless_shell-1194/chrome-linux/headless_shell --disable-field-trial-config --disable-REDACTED-networking --disable-REDACTED-timer-throttling --disable-REDACTEDing-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-REDACTED-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=AcceptCHFrame,AvoidUnnecessaryBeforeUnloadCheckSync,DestroyProfileOnBrowserClose,DialMediaRouteProvider,GlobalMediaControls,HttpsUpgrades,LensOverlay,MediaRouter,PaintHolding,ThirdPartyStoragePartitioning,Translate,AutoDeElevate,RenderDocument --enable-features=CDPScreenshotNewSurface --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-REDACTEDing --force-color-profile=srgb --metrics-recording-only --no-first-run --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --disable-search-engine-choice-screen --unsafely-disable-devtools-self-xss-warnings --edge-skip-compat-layer-relaunch --enable-automation --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4 --no-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-cM8KwS --remote-debugging-pipe --no-startup-window (dns block)
    • Triggering command: /home/REDACTED/.cache/ms-playwright/chromium_headless_shell-1194/chrome-linux/headless_shell --disable-field-trial-config --disable-REDACTED-networking --disable-REDACTED-timer-throttling --disable-REDACTEDing-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-REDACTED-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=AcceptCHFrame,AvoidUnnecessaryBeforeUnloadCheckSync,DestroyProfileOnBrowserClose,DialMediaRouteProvider,GlobalMediaControls,HttpsUpgrades,LensOverlay,MediaRouter,PaintHolding,ThirdPartyStoragePartitioning,Translate,AutoDeElevate,RenderDocument --enable-features=CDPScreenshotNewSurface --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-REDACTEDing --force-color-profile=srgb --metrics-recording-only --no-first-run --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --disable-search-engine-choice-screen --unsafely-disable-devtools-self-xss-warnings --edge-skip-compat-layer-relaunch --enable-automation --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4 --no-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-FGbWBB --remote-debugging-pipe --no-startup-window (dns block)
  • styled-components.com
    • Triggering command: /home/REDACTED/.cache/ms-playwright/chromium_headless_shell-1194/chrome-linux/headless_shell --disable-field-trial-config --disable-REDACTED-networking --disable-REDACTED-timer-throttling --disable-REDACTEDing-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-REDACTED-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=AcceptCHFrame,AvoidUnnecessaryBeforeUnloadCheckSync,DestroyProfileOnBrowserClose,DialMediaRouteProvider,GlobalMediaControls,HttpsUpgrades,LensOverlay,MediaRouter,PaintHolding,ThirdPartyStoragePartitioning,Translate,AutoDeElevate,RenderDocument --enable-features=CDPScreenshotNewSurface --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-REDACTEDing --force-color-profile=srgb --metrics-recording-only --no-first-run --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --disable-search-engine-choice-screen --unsafely-disable-devtools-self-xss-warnings --edge-skip-compat-layer-relaunch --enable-automation --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4 --no-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-cM8KwS --remote-debugging-pipe --no-startup-window (dns block)
    • Triggering command: /home/REDACTED/.cache/ms-playwright/chromium_headless_shell-1194/chrome-linux/headless_shell --disable-field-trial-config --disable-REDACTED-networking --disable-REDACTED-timer-throttling --disable-REDACTEDing-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-REDACTED-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=AcceptCHFrame,AvoidUnnecessaryBeforeUnloadCheckSync,DestroyProfileOnBrowserClose,DialMediaRouteProvider,GlobalMediaControls,HttpsUpgrades,LensOverlay,MediaRouter,PaintHolding,ThirdPartyStoragePartitioning,Translate,AutoDeElevate,RenderDocument --enable-features=CDPScreenshotNewSurface --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-REDACTEDing --force-color-profile=srgb --metrics-recording-only --no-first-run --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --disable-search-engine-choice-screen --unsafely-disable-devtools-self-xss-warnings --edge-skip-compat-layer-relaunch --enable-automation --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4 --no-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-FGbWBB --remote-debugging-pipe --no-startup-window (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@Copilot Copilot AI requested a review from sapphi-red October 14, 2025 13:43
Copilot finished work on behalf of sapphi-red October 14, 2025 13:43
@sapphi-red sapphi-red changed the title Remove __filename and __dirname checks from plugin-react-swc fix(react-swc): remove __filename and __dirname checks from plugin-react-swc Oct 14, 2025
@sapphi-red sapphi-red changed the title fix(react-swc): remove __filename and __dirname checks from plugin-react-swc fix(react-swc): remove __filename and __dirname checks Oct 14, 2025
@hi-ogawa hi-ogawa marked this pull request as ready for review October 14, 2025 23:34
@hi-ogawa hi-ogawa merged commit 910cd41 into main Oct 14, 2025
12 of 14 checks passed
@hi-ogawa hi-ogawa deleted the copilot/remove-filename-dirname-check branch October 14, 2025 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove __filename and __dirname check from packages/plugin-react-swc/src/index.ts

5 participants