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

Error in dev mode only (works in prod) with json import #4897

Closed
7 tasks done
vladitasev opened this issue Sep 10, 2021 · 5 comments · Fixed by #5095
Closed
7 tasks done

Error in dev mode only (works in prod) with json import #4897

vladitasev opened this issue Sep 10, 2021 · 5 comments · Fixed by #5095
Labels
bug: upstream Bug in a dependency of Vite help wanted Extra attention is needed inconsistency Inconsistency between dev & build p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@vladitasev
Copy link

vladitasev commented Sep 10, 2021

Describe the bug

Hello, I am part of the UI5 Web Components team (https://sap.github.io/ui5-webcomponents/) and we're recommending vitejs as the to-go bundler for people looking to start using our web components. However, one of our demo apps fails in dev mode when we try to demonstrate the usage of our .json-based assets (in this case CLDR data).

After importing a .json file I get the following error:

[plugin:vite:import-analysis] Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.

This works perfectly fine in prod mode (npm run build), and only occurs for npm run dev.

Reproduction

  1. Create an empty project with the vanilla template:
    npm init vite@latest my-test-app -- --template vanilla
    npm i

  2. Add @ui5/webcomponents as a dependency
    npm i @ui5/webcomponents

  3. Import the following 2 modules in the main.js file:

import "@ui5/webcomponents/dist/Assets.js";
import "@ui5/webcomponents/dist/DatePicker.js";
  1. Use the web component in the index.html file, right after the body tag:
<ui5-date-picker></ui5-date-picker>
  1. Finally run the project in dev mode:
    npm run dev

You will get the "Invalid JS syntax" error.

Now run the project in prod mode
npm run build
npm run serve
This works well and you can see the date picker on your page.

System Info

System:
    OS: macOS 11.5.2
    CPU: (8) x64 Apple M1
    Memory: 36.10 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.16.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.7.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 93.0.4577.63
    Safari: 14.1.2
  npmPackages:
    vite: ^2.5.4 => 2.5.6

Used Package Manager

npm

Logs

Plugin: vite:import-analysis
  File: /Users/i313163/SAPDevelop/demo/vite-bug/node_modules/.vite/en_GB-KE4YER4S.js?v=2513426e
  5031|    __license,
  5032|    __version,
  5033|    ca_buddhist as "ca-buddhist",
     |                      ^
  5034|    ca_gregorian as "ca-gregorian",
  5035|    ca_islamic as "ca-islamic",
      at formatError (/Users/i313163/SAPDevelop/demo/vite-bug/node_modules/vite/dist/node/chunks/dep-9f74b403.js:50143:46)
      at TransformContext.error (/Users/i313163/SAPDevelop/demo/vite-bug/node_modules/vite/dist/node/chunks/dep-9f74b403.js:50139:19)
      at TransformContext.transform (/Users/i313163/SAPDevelop/demo/vite-bug/node_modules/vite/dist/node/chunks/dep-9f74b403.js:75829:22)
      at async Object.transform (/Users/i313163/SAPDevelop/demo/vite-bug/node_modules/vite/dist/node/chunks/dep-9f74b403.js:50353:30)
      at async transformRequest (/Users/i313163/SAPDevelop/demo/vite-bug/node_modules/vite/dist/node/chunks/dep-9f74b403.js:65248:29)
      at async viteTransformMiddleware (/Users/i313163/SAPDevelop/demo/vite-bug/node_modules/vite/dist/node/chunks/dep-9f74b403.js:65376:32) (x2)

Validations

@sodatea sodatea added bug: upstream Bug in a dependency of Vite inconsistency Inconsistency between dev & build and removed pending triage labels Sep 10, 2021
@sodatea
Copy link
Member

sodatea commented Sep 10, 2021

Bug in esbuild json loader. It shouldn't export these top-level properties that aren't valid js identifiers.

@sodatea
Copy link
Member

sodatea commented Sep 13, 2021

Reported at evanw/esbuild#1598

@evanw
Copy link

evanw commented Sep 13, 2021

This is not a problem with esbuild because the output is valid JavaScript. It's just using the new arbitrary module namespace identifier names syntax feature (supported in recent versions of Chrome, Firefox, Safari, and Node). You need to set esbuild's target setting correctly if you need to support older JavaScript VMs that can't parse newer syntax. Then esbuild won't generate these exports. Either that or Vite's import analysis pass needs to be updated to support this new import/export syntax.

@sodatea
Copy link
Member

sodatea commented Sep 14, 2021

Got it. Thanks for the information.

I think we are now pending es-module-lexer's support for this syntax: guybedford/es-module-lexer#67

If anyone's interested in implementing this feature, you can take the corresponding commits to support string exports in cjs-module-lexer as a reference: nodejs/cjs-module-lexer@1.1.1...1.2.1

@sodatea sodatea added help wanted Extra attention is needed p3-minor-bug An edge case that only affects very specific usage (priority) labels Sep 22, 2021
@github-actions
Copy link

Hello @vladitasev. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite help wanted Extra attention is needed inconsistency Inconsistency between dev & build p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants