Skip to content

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

Merged
patak-cat merged 2 commits intovitejs:mainfrom
gaomeng1900:patch-2
Apr 8, 2022
Merged

docs: warning for new URL(url, import.meta.url)#7279
patak-cat merged 2 commits intovitejs:mainfrom
gaomeng1900:patch-2

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-cat patak-cat 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-cat patak-cat 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