sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.7k stars 1.94k forks source link

Vite console error `Unexpected token 'export'` when trying to load some external library #7363

Closed Tal500 closed 2 years ago

Tal500 commented 2 years ago

Describe the bug

Although this is a Vite related issue, I have decided to post it here since it will be clear and happens after @benmccann changes on Vite for fixing an issue for svelte, and I'm not sure how to construct an example using only Vite. I have used git bisect on Vite to found that it happen because of commit 325900601c4955d5a97459d4b80def1a724672f6 of Vite.

When I try to load a specific library, svelte-i18, I get an error on Vite console: Unexpected token 'export'.

Reproduction

Repo: https://github.com/Tal500/demo-error-vite-unexpected-export

For the full reproduction story

  1. create-svelte, I used version 2.0.0-next.187, and with the following choices:
    ? Which Svelte app template? » SvelteKit demo app
    ? Add type checking with TypeScript? » Yes, using JavaScript with JSDoc comments
    ? Add ESLint for code linting? ... No
    ? Add Prettier for code formatting? ... No
    ? Add Playwright for browser testing? ... No
  2. In package.json, link vite via pnpm to your local version of Vite (on commit 325900601c4955d5a97459d4b80def1a724672f6):
    "pnpm": {
        "overrides": {
            "vite": "link:C:/Src/vite/packages/vite"
        }
    }
  3. pnpm install && pnpm install --save-dev svelte-i18n
  4. Add import 'svelte-i18n'; to the script in +layout.svelte
  5. pnpm dev

You'll see the error on the (SSR) console, when opening http://localhost:5173/:

Unexpected token 'export'
C:\Src\my-demo-error\node_modules\.pnpm\tslib@2.4.0\node_modules\tslib\tslib.es6.js:24
export function __extends(d, b) {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:827:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Src\my-demo-error\node_modules\.pnpm\intl-messageformat@9.13.0\node_modules\intl-messageformat\index.js:8:15)     
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
Unexpected token 'export'
C:\Src\my-demo-error\node_modules\.pnpm\tslib@2.4.0\node_modules\tslib\tslib.es6.js:24
export function __extends(d, b) {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:827:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Src\my-demo-error\node_modules\.pnpm\intl-messageformat@9.13.0\node_modules\intl-messageformat\index.js:8:15)     
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
Not found: /favicon.ico
Error: Not found: /favicon.ico
    at resolve (file:///C:/Src/my-demo-error/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.522_svelte@3.52.0/node_modules/@sveltejs/kit/src/runtime/server/index.js:259:13)
    at resolve (file:///C:/Src/my-demo-error/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.522_svelte@3.52.0/node_modules/@sveltejs/kit/src/runtime/server/index.js:290:5)
    at Object.handle (file:///C:/Src/my-demo-error/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.522_svelte@3.52.0/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:340:66)
    at respond (file:///C:/Src/my-demo-error/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.522_svelte@3.52.0/node_modules/@sveltejs/kit/src/runtime/server/index.js:287:40)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at file:///C:/Src/my-demo-error/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.522_svelte@3.52.0/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:410:22
Unexpected token 'export'
C:\Src\my-demo-error\node_modules\.pnpm\tslib@2.4.0\node_modules\tslib\tslib.es6.js:24
export function __extends(d, b) {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:827:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Src\my-demo-error\node_modules\.pnpm\intl-messageformat@9.13.0\node_modules\intl-messageformat\index.js:8:15)     
    at Module._compile (node:internal/modules/cjs/loader:1105:14)

Logs

C:\Src\my-demo-error>pnpm dev

> my-demo-error@0.0.1 dev C:\Src\my-demo-error
> vite dev

  VITE v3.2.0-beta.1  ready in 2008 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
Unexpected token 'export'
C:\Src\my-demo-error\node_modules\.pnpm\tslib@2.4.0\node_modules\tslib\tslib.es6.js:24
export function __extends(d, b) {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:827:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Src\my-demo-error\node_modules\.pnpm\intl-messageformat@9.13.0\node_modules\intl-messageformat\index.js:8:15)     
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
Unexpected token 'export'
C:\Src\my-demo-error\node_modules\.pnpm\tslib@2.4.0\node_modules\tslib\tslib.es6.js:24
export function __extends(d, b) {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:827:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Src\my-demo-error\node_modules\.pnpm\intl-messageformat@9.13.0\node_modules\intl-messageformat\index.js:8:15)     
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
Not found: /favicon.ico
Error: Not found: /favicon.ico
    at resolve (file:///C:/Src/my-demo-error/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.522_svelte@3.52.0/node_modules/@sveltejs/kit/src/runtime/server/index.js:259:13)
    at resolve (file:///C:/Src/my-demo-error/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.522_svelte@3.52.0/node_modules/@sveltejs/kit/src/runtime/server/index.js:290:5)
    at Object.handle (file:///C:/Src/my-demo-error/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.522_svelte@3.52.0/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:340:66)
    at respond (file:///C:/Src/my-demo-error/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.522_svelte@3.52.0/node_modules/@sveltejs/kit/src/runtime/server/index.js:287:40)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at file:///C:/Src/my-demo-error/node_modules/.pnpm/@sveltejs+kit@1.0.0-next.522_svelte@3.52.0/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:410:22
Unexpected token 'export'
C:\Src\my-demo-error\node_modules\.pnpm\tslib@2.4.0\node_modules\tslib\tslib.es6.js:24
export function __extends(d, b) {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:827:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Src\my-demo-error\node_modules\.pnpm\intl-messageformat@9.13.0\node_modules\intl-messageformat\index.js:8:15)     
    at Module._compile (node:internal/modules/cjs/loader:1105:14)

### System Info

```Shell
C:\Src\my-demo-error>npx envinfo --system --binaries --browsers --npmPackages "{svelte,@sveltejs/*,vite}"

  System:
    OS: Windows 10 10.0.19044
    CPU: (4) x64 Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
    Memory: 4.12 GB / 15.49 GB
  Binaries:
    Node: 18.3.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.18 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.14.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (106.0.1370.52)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.84
    @sveltejs/kit: next => 1.0.0-next.522
    svelte: ^3.46.0 => 3.52.0
    vite: ^3.1.0 => 3.2.0-beta.1

Severity

blocking an upgrade

Additional Information

I'm sorry I've just said "it doesn't work with svelte-i18n library", I just didn't understand the logic enough to fix this, so I'm asking for your help, though I've found the exact version that causes the issue.

Obviously, this issue is wider than a specific library.

dominikg commented 2 years ago

You are using a beta version of vite, this is going to be fixed in the next beta. See https://github.com/vitejs/vite/pull/10569 (already merged to vite main but not released)

benmccann commented 2 years ago

Thanks for the report. I'll go ahead and close this since it's already been fixed as Dominik mentioned

Tal500 commented 2 years ago

You are using a beta version of vite, this is going to be fixed in the next beta. See vitejs/vite#10569 (already merged to vite main but not released)

Thanks, you're right! Tested and it works.