From 8ae44bc95d78b644bf8ba6a5caf9d10fea82cc25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20=C2=B7=20=E8=A5=BF=E8=90=8C?= Date: Sat, 12 Mar 2022 00:14:19 +0800 Subject: [PATCH] Update assets.md 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. --- docs/guide/assets.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/guide/assets.md b/docs/guide/assets.md index fd5aa46f49467f..44435165d37d35 100644 --- a/docs/guide/assets.md +++ b/docs/guide/assets.md @@ -105,6 +105,10 @@ function getImageUrl(name) { During the production build, Vite will perform necessary transforms so that the URLs still point to the correct location even after bundling and asset hashing. +::: warning Note: Esbuild target config is necessary +This pattern needs esbuild target to be set to `es2020` or higher. `vite@2.x` use `es2019` as default target. Set [build-target](https://vitejs.dev/config/#build-target) and [optimizedeps.esbuildoptions.target](https://vitejs.dev/config/#optimizedeps-esbuildoptions) to `es2020` or higher if you intend to use this partten. +::: + ::: warning Note: Does not work with SSR This pattern does not work if you are using Vite for Server-Side Rendering, because `import.meta.url` have different semantics in browsers vs. Node.js. The server bundle also cannot determine the client host URL ahead of time. :::