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): coerce to string in import(dep) #3430

Merged
merged 3 commits into from May 25, 2023
Merged

fix(vite-node): coerce to string in import(dep) #3430

merged 3 commits into from May 25, 2023

Conversation

jcbhmr
Copy link
Contributor

@jcbhmr jcbhmr commented May 22, 2023

This PR would...

  • Coerce dep to string using the string coercion algorithm in import(dep)

Need to use `${s}` instead of "" + s in order to get the right hint in Symbol.toPrimitive

const obj2 = {
  [Symbol.toPrimitive](hint) {
    if (hint === "number") {
      return 10;
    }
    if (hint === "string") {
      return "hello";
    }
    return true;
  },
};
console.log(+obj2); // 10        — hint is "number"
console.log(`${obj2}`); // "hello"   — hint is "string"
console.log(obj2 + ""); // "true"    — hint is "default"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive#description

closes #3429


Review PR in StackBlitz Codeflow Submitted with StackBlitz Codeflow.

@stackblitz
Copy link

stackblitz bot commented May 22, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

packages/vite-node/src/client.ts Outdated Show resolved Hide resolved
packages/vite-node/src/client.ts Outdated Show resolved Hide resolved
Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

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

Can you add a test case for this, please?

@jcbhmr
Copy link
Contributor Author

jcbhmr commented May 24, 2023

Added a test case:

test('dynamic import coerces to string', async () => {
  const dataUri = 'data:text/javascript;charset=utf-8,export default "hi"'
  const { default: hi } = await import({ toString: () => dataUri } as string)
  expect(hi).toBe('hi')
})

@sheremet-va sheremet-va merged commit b72ebdb into vitest-dev:main May 25, 2023
16 of 17 checks passed
@jcbhmr jcbhmr deleted the jcbhmr/issue3429 branch May 25, 2023 16:34
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.

__vite_ssr_dynamic_import__ doesn't coerce import(specifier) to a string
2 participants