Skip to content
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

feat: better config __dirname support #8442

Merged
merged 1 commit into from Jun 8, 2022

Conversation

sapphi-red
Copy link
Member

Description

This PR changes the way to implement __dirname/__filename/import.meta.url support in config files.

Before this PR, those values were replaced. But now it is injected at each files.
This will avoid some strange errors.

I tested this with the files below.

// vite.config.js
import './dep'

console.log('__dirname', __dirname)
console.log('__filename', __filename)
console.log('import.meta.url', import.meta.url)

;(() => {
  const __dirname = 'd'
  const __filename = 'f'
  console.log(__dirname, __filename)
})()

export default {}
// dep.js
console.log('[dep] __dirname', __dirname)
console.log('[dep] __filename', __filename)
console.log('[dep] import.meta.url', import.meta.url)

refs: #7846 (comment)

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added the p2-nice-to-have 🍰 Not breaking anything but nice to have (priority) label Jun 2, 2022
@@ -24,14 +24,14 @@ vite --config my-config.js
```

::: tip NOTE
Vite will replace `__filename`, `__dirname`, and `import.meta.url` in config files and its deps. Using these as variable names or passing as a parameter to a function with string double quote (example `console.log`) will result in an error:
Vite will inject `__filename`, `__dirname` in config files and its deps. Declaring these variables at top level will result in an error:
Copy link
Member Author

@sapphi-red sapphi-red Jun 2, 2022

Choose a reason for hiding this comment

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

In CJS, the following code fails, so it is not a special thing.

const __dirname = ''

But here it also happens with ESM. This is the reason I left this note.

Also I didn't mention import.meta.url because __vite_injected_original_import_meta_url will rarely conflict.

@patak-dev patak-dev merged commit 51e9195 into vitejs:main Jun 8, 2022
@sapphi-red sapphi-red deleted the feat/better-config-dirname branch June 8, 2022 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have 🍰 Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants