vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
67.45k stars 6.07k forks source link

Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1") #6985

Closed asasugar closed 2 years ago

asasugar commented 2 years ago

Describe the bug

Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1") image

"scripts": {
    "dev": "vite",
    "build": "vue-tsc --noEmit && vite build",
    "build:debug": "vue-tsc --noEmit && vite build --debug",
  },

yarn dev is ok, yarn build is not work~~

Reproduction

https://github.com/asasugar/vite-element-plus-admin/blob/master/src/main.ts

System Info

System:
    OS: macOS 12.0.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    Memory: 212.00 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.1.0 - ~/.nvm/versions/node/v15.1.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v15.1.0/bin/yarn
    npm: 7.0.8 - ~/.nvm/versions/node/v15.1.0/bin/npm
  Browsers:
    Chrome: 97.0.4692.99
    Safari: 15.1

Used Package Manager

yarn

Logs

vite:config bundled config file loaded in 288.76ms +0ms
  vite:config using resolved config: {
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     port: 9999,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   css: { preprocessorOptions: { less: [Object] } },
  vite:config   resolve: {
  vite:config     dedupe: undefined,
  vite:config     alias: [ [Object], [Object], [Object], [Object] ]
  vite:config   },
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-script-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:vue',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:watch-package-data',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'unplugin-auto-import',
  vite:config     'unplugin-vue-components',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: '/Users/xj/github/vite-element-plus-admin/dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: { input: '/Users/xj/github/vite-element-plus-admin/index.html' },
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   define: { __VUE_OPTIONS_API__: true, __VUE_PROD_DEVTOOLS__: false },
  vite:config   ssr: { external: [ 'vue', '@vue/server-renderer' ] },
  vite:config   configFile: '/Users/xj/github/vite-element-plus-admin/vite.config.ts',
  vite:config   configFileDependencies: [ 'vite.config.ts' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/Users/xj/github/vite-element-plus-admin',
  vite:config   base: '/',
  vite:config   publicDir: '/Users/xj/github/vite-element-plus-admin/public',
  vite:config   cacheDir: '/Users/xj/github/vite-element-plus-admin/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: true,
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   }
  vite:config } +70ms
vite v2.7.13 building for production...
transforming (142) node_modules/element-plus/es/components/skeleton/src/skeleton.mjs  vite:resolve 1.04ms default.less -> /Users/xj/github/vite-element-plus-admin/src/style/default.less +0ms
transforming (1526) node_modules/lodash/_isPrototype.jsUse of eval is strongly discouraged, as it poses security risks and may cause issues with minification
✓ 1606 modules transformed.
rendering chunks (2)...  vite:esbuild esbuild error with options used:  {
  sourcemap: true,
  sourcefile: 'assets/index.fe7d1bf3.js',
  target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  minify: true,
  treeShaking: true,
  format: 'esm',
  loader: 'js',
  tsconfigRaw: { compilerOptions: {} }
} +0ms
[vite:esbuild-transpile] Transform failed with 2 errors:
assets/index.fe7d1bf3.js:1036:0: error: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")
assets/index.fe7d1bf3.js:1039:17: error: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")

Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")
1033|  };
1034|  
1035|  mockXHR();
   |             ^
1036|  await registerDynamicRoutes();
   |  ^
1037|  const app = createApp(App);

Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")
1037|  const app = createApp(App);
1038|  setupGlobDirectives(app);
1039|  const { user } = await SDK.init();
   |                   ^
1040|  if (user) {
1041|    store.dispatch("user/asyncSetUserinfo", user);

error during build:
Error: Transform failed with 2 errors:
assets/index.fe7d1bf3.js:1036:0: error: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")
assets/index.fe7d1bf3.js:1039:17: error: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")
    at failureErrorWithLog (/Users/xj/github/vite-element-plus-admin/node_modules/esbuild/lib/main.js:1493:15)
    at /Users/xj/github/vite-element-plus-admin/node_modules/esbuild/lib/main.js:1282:29
    at /Users/xj/github/vite-element-plus-admin/node_modules/esbuild/lib/main.js:629:9
    at handleIncomingPacket (/Users/xj/github/vite-element-plus-admin/node_modules/esbuild/lib/main.js:726:9)
    at Socket.readFromStdout (/Users/xj/github/vite-element-plus-admin/node_modules/esbuild/lib/main.js:596:7)
    at Socket.emit (node:events:327:20)
    at addChunk (node:internal/streams/readable:304:12)
    at readableAddChunk (node:internal/streams/readable:279:9)
    at Socket.Readable.push (node:internal/streams/readable:218:10)
    at Pipe.onStreamRead (node:internal/stream_base_commons:192:23)
[!] Error: unfinished hook action(s) on exit:
(vite:esbuild-transpile) renderChunk

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Validations

ydcjeff commented 2 years ago

This is not a Vite issue. Top level await is not supported in the default configured target. See on https://caniuse.com/?search=top%20level%20await. You should either change the target or use .then to resolve the build.

asasugar commented 2 years ago

Already Fix. set vite.config.ts build.target: 'esnext'

build: {
    target: 'esnext'
  }
Menci commented 2 years ago

For those who comes here from searching the error message, if you want to support normal browsers (without setting esnext target), try my plugin vite-plugin-top-level-await.