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

fix(vite-node): Prevent crash when passing single module as options #1406

Merged
merged 2 commits into from Jun 4, 2022

Conversation

antoinerey
Copy link
Contributor

Right now, when passing a single module with the new --options CLI flag, cac returns a string instead of an array. This causes a crash, trying to call map on it.

❯ ./packages/vite-node/vite-node.mjs --options.deps.inline="module" hello.js
file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:697
      inline: (_b = (_a = serverOptions.deps) == null ? void 0 : _a.inline) == null ? void 0 : _b.map((dep) => {
                                                                                                  ^

TypeError: _b.map is not a function
    at parseServerOptions (file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:697:99)
    at CAC.run (file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:660:49)
    at CAC.runMatchedCommand (file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:622:34)
    at CAC.parse (file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:549:12)
    at file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:652:5
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)

This PR updates the typings to reflect that, and makes sure that both string and string[] are properly handled.

Right now, when passing a single module with the new `--options` CLI flag, `cac` returns a string instead of an array. This causes a crash, trying to call `map` on it.

```
❯ ./packages/vite-node/vite-node.mjs --options.deps.inline="module" hello.js
file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:697
      inline: (_b = (_a = serverOptions.deps) == null ? void 0 : _a.inline) == null ? void 0 : _b.map((dep) => {
                                                                                                  ^

TypeError: _b.map is not a function
    at parseServerOptions (file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:697:99)
    at CAC.run (file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:660:49)
    at CAC.runMatchedCommand (file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:622:34)
    at CAC.parse (file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:549:12)
    at file:///Users/antoine/Code/vitest/packages/vite-node/dist/cli.js:652:5
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
```

This PR updates the typings to reflect that, and makes sure that both `string` and `string[]` are properly handled.
@netlify
Copy link

netlify bot commented May 31, 2022

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit d218ee2
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/629718ebd6bb46000874010f
😎 Deploy Preview https://deploy-preview-1406--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@@ -108,18 +124,18 @@ function parseServerOptions(serverOptions: ViteNodeServerOptionsCLI): ViteNodeSe
transformMode: {
...serverOptions.transformMode,

ssr: serverOptions.transformMode?.ssr?.map(dep => new RegExp(dep)),
web: serverOptions.transformMode?.ssr?.map(dep => new RegExp(dep)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a mistake here. It should have been web instead. It's now fixed.

return {
...serverOptions,
deps: {
...serverOptions.deps,
inline: serverOptions.deps?.inline?.map((dep) => {
inline: serverOptionsDepsInline?.map((dep) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may just use toArray (you can move it from vitest to vite-node)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in d218ee2. Much simpler indeed!

@antoinerey
Copy link
Contributor Author

CI failed, but I believe it has nothing to do with the changes. Should I do something?

@antfu antfu merged commit ab8f004 into vitest-dev:main Jun 4, 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.

None yet

3 participants