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

docs: warning for new URL(url, import.meta.url) #7279

Merged
merged 2 commits into from
Apr 8, 2022

Conversation

gaomeng1900
Copy link
Contributor

@gaomeng1900 gaomeng1900 commented Mar 11, 2022

Vite use es2019 as default esbuild target for building and pre-bundling, which does NOT support import.meta.url.

new URL(import.meta.url) will be transpiled to

var import_metaN = {};
new URL(url, import_metaN.url);

This will cause a fatal error.

Description

fix #5558 and #5270

new URL(url, import.meta.url) pattern does not work by default. Users will get a confusing error after the code is bundled.

Additional context

I would recommend vite to upgrade its esbuild config. Even esbuild use es2020 by default.


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.

Vite use `es2019` as default target for building and Pre-Bundling which does not support `import.meta.url`.

`import.meta.url` will be transpiled to 

```javascript
var import_metaN = {};
new URL(url, import_metaN.url);
```

This will cause a fatal error.
@sapphi-red sapphi-red mentioned this pull request Apr 8, 2022
4 tasks
@patak-dev patak-dev changed the title docs: Add warning for new URL(url, import.meta.url) because it does not work by default. docs: warning for new URL(url, import.meta.url) Apr 8, 2022
@patak-dev patak-dev merged commit 09c4fc0 into vitejs:main Apr 8, 2022
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.

The production environment uses new URL(src, import.meta.url) to report an error
2 participants