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!: use type module (revert #1411) #1465

Merged
merged 8 commits into from Oct 7, 2022
Merged

fix!: use type module (revert #1411) #1465

merged 8 commits into from Oct 7, 2022

Conversation

bluwy
Copy link
Contributor

@bluwy bluwy commented Jun 11, 2022

Reverts #1411 (and also #1439)

Ref: #1411 (comment)

It's not needed to remove type module to fix the issue stated in the PR

@netlify
Copy link

netlify bot commented Jun 11, 2022

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 0f2f0ba
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/62a988d064e33a00090f3001
😎 Deploy Preview https://deploy-preview-1465--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.

@sheremet-va
Copy link
Member

Will it revert errors for users? If so, what should we recommend people?

@bluwy
Copy link
Contributor Author

bluwy commented Jun 11, 2022

Yes it will revert the errors. Following #1411 (comment), if they're using the new nodenext or node16 options, they must name the test files with .mts if "type": "module" is not specified.

@sheremet-va
Copy link
Member

sheremet-va commented Jun 12, 2022

Yes it will revert the errors. Following #1411 (comment), if they're using the new nodenext or node16 options, they must name the test files with .mts if "type": "module" is not specified.

In that case, should we also extend our defaults?

'test{,-*}.{js,cjs,mjs,ts,tsx,jsx}',
'**/*{.,-}test.{js,cjs,mjs,ts,tsx,jsx}',

extension: ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.vue', '.svelte'],

@bluwy
Copy link
Contributor Author

bluwy commented Jun 12, 2022

I think that makes sense 👍

@antfu
Copy link
Member

antfu commented Jun 12, 2022

/cc @aleclarson

@antfu
Copy link
Member

antfu commented Jun 13, 2022

I would hold this for a while longer. Indeed the usage in #1411 might better use .mts for the tests. But as .mts is not wildly supported yet, plus there is not too much harm for us to use .mjs, I would more lean to make it easy for users until we really encounter some blockers for the current workaround.

@aleclarson
Copy link
Contributor

Following #1411 (comment), if they're using the new nodenext or node16 options, they must name the test files with .mts if "type": "module" is not specified.

The "better solution" I mentioned in that thread goes like this:

".": {
  "require": {
    "types": "./dist/index.d.cts",
    "default": "./dist/index.cjs"
  },
  "types": "./dist/index.d.ts",
  "import": "./dist/index.js"
},

That would be how Vitest's exports should look. Then users aren't required to change their setup at all.

Both dist/index.d.cts and dist/index.cjs can just re-export from the other dist files.

@sheremet-va
Copy link
Member

sheremet-va commented Jun 13, 2022

The "better solution" I mentioned in that thread goes like this:

First of all, we already saw in that PR that it doesn't work, does it? TypeScript has a bug preventing that.

Second of all, I don't see how this is a "better solution"? We will essentially double package size just to please few people with TypeScript issues that can be resolved on their end? Those files won't even be run in any situation.

@aleclarson
Copy link
Contributor

First of all, we already saw in that PR that it doesn't work, does it? TypeScript has a big preventing that.

The two differences from your idea are that (1) require.types is defined and (2) it uses a .d.cts extension. Both are required for TypeScript to believe the package is not ESM only.

We will essentially double package size

No, the new .cts and .cjs files are basic re-exports of the ESM dist files.

// dist/index.d.cts
export * from './index.mjs'

// dist/index.cjs
export * from './index.mjs'

@sheremet-va
Copy link
Member

sheremet-va commented Jun 13, 2022

No, the new .cts and .cjs files are basic re-exports of the ESM dist files.

But this wouldn't work for the same reason it doesn't work right now, no? You cannot use static import/export inside cjs.

This is also just a hack to please typescript. They added this extensions for a the reason we are trying to fix here, so I would rather go with it.

@aleclarson
Copy link
Contributor

But this wouldn't work for the same reason it doesn't work right now, no? You cannot use static import/export inside cjs.

Those modules would never be loaded by Node.js runtime, so no errors would occur.

Correction: The dist/index.cjs module should actually be empty. The dist/index.d.cts allows import/export syntax, and I've tested it myself, so I know it works. :)

It's not a "hack", because the TypeScript team themselves said it's a valid solution.

@sheremet-va
Copy link
Member

sheremet-va commented Jun 13, 2022

Those modules would never be loaded by Node.js runtime, so no errors would occur.

No, I mean by TypeScript. Why would importing ESM from commonjs would work inside a node_module, but not in your own project?

Current problem:

  • importing Vitest from commonjs gives error from TypeScript typechecker, because it will compile import to require and requiring Vitest is not allowed
  • but importing inside Vitest's cts magically works? I don't buy it tbh

@sheremet-va
Copy link
Member

Ok, I've looked through the threads, and now I see that @aleclarson's idea is valid. We can include it in this PR actually, and safely merge it. @antfu, what do you think?

@antfu
Copy link
Member

antfu commented Jun 14, 2022

I can't really follow the topic, but sure, if it helps and doesn't break anything else :)

packages/vitest/rollup.config.js Outdated Show resolved Hide resolved
@jroru
Copy link

jroru commented Sep 14, 2022

Is there anything blocking this from being merged?

@sheremet-va
Copy link
Member

Is there anything blocking this from being merged?

This needs updating, we changed quite a few APIs since then, and if merged, Vitest won't work.

@jroru
Copy link

jroru commented Sep 16, 2022

For a package with:

  • package.json type set to module
  • tsconfig.json compilerOptions.moduleResolution set to Node16
  • vitest@0.22.1
  • vite@3.0.9
  • typescript@4.7.4

Running tsc outputs:

../../node_modules/vitest/dist/config.d.ts:2:27 - error TS1471: Module 'vite' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.

2 export { ConfigEnv } from 'vite'

Is there a known workaround?

@bluwy
Copy link
Contributor Author

bluwy commented Sep 17, 2022

I've updated the PR. Tried to update some of the newer places, and the tests seems to be passing, except ubuntu node14 (not sure if it's a fluke).

@StevenGBrown
Copy link

For a package with:

  • package.json type set to module
  • tsconfig.json compilerOptions.moduleResolution set to Node16
  • vitest@0.22.1
  • vite@3.0.9
  • typescript@4.7.4

Running tsc outputs:

../../node_modules/vitest/dist/config.d.ts:2:27 - error TS1471: Module 'vite' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.

2 export { ConfigEnv } from 'vite'

Is there a known workaround?

@jroru My workaround until this PR is merged has been to use patch-package to add "type": "module" to the vitest package.json.

@sheremet-va sheremet-va added this to the Next milestone Oct 5, 2022
@sheremet-va sheremet-va merged commit 652f160 into vitest-dev:main Oct 7, 2022
@bluwy bluwy deleted the use-type-module branch October 7, 2022 11:56
AlexKMarshall added a commit to AlexKMarshall/necromunda-turbo-old that referenced this pull request Oct 7, 2022
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [@vitest/coverage-c8](https://togithub.com/vitest-dev/vitest) |
[`0.23.4` ->
`0.24.0`](https://renovatebot.com/diffs/npm/@vitest%2fcoverage-c8/0.23.4/0.24.0)
|
[![age](https://badges.renovateapi.com/packages/npm/@vitest%2fcoverage-c8/0.24.0/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/npm/@vitest%2fcoverage-c8/0.24.0/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/npm/@vitest%2fcoverage-c8/0.24.0/compatibility-slim/0.23.4)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/npm/@vitest%2fcoverage-c8/0.24.0/confidence-slim/0.23.4)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>vitest-dev/vitest</summary>

###
[`v0.24.0`](https://togithub.com/vitest-dev/vitest/releases/tag/v0.24.0)

[Compare
Source](https://togithub.com/vitest-dev/vitest/compare/v0.23.4...v0.24.0)

#####    🚨 Breaking Changes

- Use type module (revert
[#&#8203;1411](https://togithub.com/vitest-dev/vitest/issues/1411))  - 
by [@&#8203;bluwy](https://togithub.com/bluwy) and
[@&#8203;sheremet-va](https://togithub.com/sheremet-va) in
[vitest-dev/vitest#1465
- Drop support for Vite 2  -  by
[@&#8203;antfu](https://togithub.com/antfu) and
[@&#8203;sheremet-va](https://togithub.com/sheremet-va) in
[vitest-dev/vitest#1928

#####    🚀 Features

- **benchmark**: Todo mode  -  by
[@&#8203;Aslemammad](https://togithub.com/Aslemammad) in
[vitest-dev/vitest#2057
- **inline-snapshot**: Support comment  -  by
[@&#8203;azaleta](https://togithub.com/azaleta) in
[vitest-dev/vitest#2077

#####    🐞 Bug Fixes

- Run related test, even if test doesn't have dependencies  -  by
[@&#8203;sheremet-va](https://togithub.com/sheremet-va) in
[vitest-dev/vitest#2043
- Check for asymmetricMatch before accessing  -  by
[@&#8203;sheremet-va](https://togithub.com/sheremet-va)
[<samp>(75719)</samp>](https://togithub.com/vitest-dev/vitest/commit/757199a6)
- Check hook teardown return type, closes
[#&#8203;2092](https://togithub.com/vitest-dev/vitest/issues/2092)  - 
by [@&#8203;sheremet-va](https://togithub.com/sheremet-va)
[<samp>(cba3f)</samp>](https://togithub.com/vitest-dev/vitest/commit/cba3ff09)
- Don't stop watch mode, if non-object error is thrown, close
[#&#8203;2106](https://togithub.com/vitest-dev/vitest/issues/2106)  - 
by [@&#8203;sheremet-va](https://togithub.com/sheremet-va)
[<samp>(bd677)</samp>](https://togithub.com/vitest-dev/vitest/commit/bd677017)
- Use correct source maps in stacktrace  -  by
[@&#8203;haikyuu](https://togithub.com/haikyuu) in
[vitest-dev/vitest#2027
- Import CustomEventMap from vite for vite-node  -  by
[@&#8203;sheremet-va](https://togithub.com/sheremet-va) in
[vitest-dev/vitest#2124
- **jsdom**: Use jsdom Blob instead of Node, if jsdom is enabled  -  by
[@&#8203;ChpShy](https://togithub.com/ChpShy) in
[vitest-dev/vitest#2086

#####     [View changes on
GitHub](https://togithub.com/vitest-dev/vitest/compare/v0.23.4...v0.24.0)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click
this checkbox.

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/AlexKMarshall/necromunda-turbo).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzMi4yMjIuMyIsInVwZGF0ZWRJblZlciI6IjMyLjIyMi4zIn0=-->
AlexKMarshall added a commit to AlexKMarshall/necromunda-turbo-old that referenced this pull request Oct 7, 2022
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [vitest](https://togithub.com/vitest-dev/vitest) | [`0.23.4` ->
`0.24.0`](https://renovatebot.com/diffs/npm/vitest/0.23.4/0.24.0) |
[![age](https://badges.renovateapi.com/packages/npm/vitest/0.24.0/age-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://badges.renovateapi.com/packages/npm/vitest/0.24.0/adoption-slim)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://badges.renovateapi.com/packages/npm/vitest/0.24.0/compatibility-slim/0.23.4)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://badges.renovateapi.com/packages/npm/vitest/0.24.0/confidence-slim/0.23.4)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>vitest-dev/vitest</summary>

###
[`v0.24.0`](https://togithub.com/vitest-dev/vitest/releases/tag/v0.24.0)

[Compare
Source](https://togithub.com/vitest-dev/vitest/compare/v0.23.4...v0.24.0)

#####    🚨 Breaking Changes

- Use type module (revert
[#&#8203;1411](https://togithub.com/vitest-dev/vitest/issues/1411))  - 
by [@&#8203;bluwy](https://togithub.com/bluwy) and
[@&#8203;sheremet-va](https://togithub.com/sheremet-va) in
[vitest-dev/vitest#1465
- Drop support for Vite 2  -  by
[@&#8203;antfu](https://togithub.com/antfu) and
[@&#8203;sheremet-va](https://togithub.com/sheremet-va) in
[vitest-dev/vitest#1928

#####    🚀 Features

- **benchmark**: Todo mode  -  by
[@&#8203;Aslemammad](https://togithub.com/Aslemammad) in
[vitest-dev/vitest#2057
- **inline-snapshot**: Support comment  -  by
[@&#8203;azaleta](https://togithub.com/azaleta) in
[vitest-dev/vitest#2077

#####    🐞 Bug Fixes

- Run related test, even if test doesn't have dependencies  -  by
[@&#8203;sheremet-va](https://togithub.com/sheremet-va) in
[vitest-dev/vitest#2043
- Check for asymmetricMatch before accessing  -  by
[@&#8203;sheremet-va](https://togithub.com/sheremet-va)
[<samp>(75719)</samp>](https://togithub.com/vitest-dev/vitest/commit/757199a6)
- Check hook teardown return type, closes
[#&#8203;2092](https://togithub.com/vitest-dev/vitest/issues/2092)  - 
by [@&#8203;sheremet-va](https://togithub.com/sheremet-va)
[<samp>(cba3f)</samp>](https://togithub.com/vitest-dev/vitest/commit/cba3ff09)
- Don't stop watch mode, if non-object error is thrown, close
[#&#8203;2106](https://togithub.com/vitest-dev/vitest/issues/2106)  - 
by [@&#8203;sheremet-va](https://togithub.com/sheremet-va)
[<samp>(bd677)</samp>](https://togithub.com/vitest-dev/vitest/commit/bd677017)
- Use correct source maps in stacktrace  -  by
[@&#8203;haikyuu](https://togithub.com/haikyuu) in
[vitest-dev/vitest#2027
- Import CustomEventMap from vite for vite-node  -  by
[@&#8203;sheremet-va](https://togithub.com/sheremet-va) in
[vitest-dev/vitest#2124
- **jsdom**: Use jsdom Blob instead of Node, if jsdom is enabled  -  by
[@&#8203;ChpShy](https://togithub.com/ChpShy) in
[vitest-dev/vitest#2086

#####     [View changes on
GitHub](https://togithub.com/vitest-dev/vitest/compare/v0.23.4...v0.24.0)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click
this checkbox.

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/AlexKMarshall/necromunda-turbo).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzMi4yMjIuMyIsInVwZGF0ZWRJblZlciI6IjMyLjIyMi4zIn0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants