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

Enable consuming of ES modules in extensions #130367

Open
igorskyflyer opened this issue Aug 8, 2021 · 67 comments · May be fixed by #212727
Open

Enable consuming of ES modules in extensions #130367

igorskyflyer opened this issue Aug 8, 2021 · 67 comments · May be fixed by #212727
Assignees
Labels
extension-host Extension host issues extensions Issues concerning extensions feature-request Request for new features or functionality
Milestone

Comments

@igorskyflyer
Copy link

When developing extensions and using either JavaScript or TypeScript we are unable to consume ES modules, only somewhat legacy CommonJS modules, setting the type to module and rewriting the extension to use import instead of require breaks the extension, generating an exception that states that all modules should use import instead of require in internal VS Code JavaScript files, I conclude it's caused by the type: module that forces Node to treat all .js files as ES modules. Tried using TypeScript which transpiles its own syntax to CommonJS module - so that's a no, I have also tried using just .mjs extension, again the same issue.

What is the status of this issue and are there plans to enable using of ES modules in extension development? That (could) bring somewhat big performance gains when bundling extensions with, for example, esbuild because it would enable tree-shaking - dead code removal, thus loading only necessary code. But I think this is not an extension API only issue, right? This needs to be done for VS Code itself?

@vscodebot
Copy link

vscodebot bot commented Aug 8, 2021

(Experimental duplicate detection)
Thanks for submitting this issue. Please also check if it is already covered by an existing one, like:

@igorskyflyer
Copy link
Author

Closing after 15 days of no reply.

@alexdima
Copy link
Member

cc @jrieken

@alexdima alexdima added extension-host Extension host issues feature-request Request for new features or functionality web Issues related to running VSCode in the web and removed web Issues related to running VSCode in the web labels Aug 25, 2021
@Lemmingh
Copy link
Contributor

Lemmingh commented Sep 3, 2021

Sounds like a duplicate of #116056. Is it possible to reopen that issue?


The VS Code extension host currently only accepts CJS module, as shown in the (trimmed) error message below:

Activating extension failed

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module
require() of ES modules is not supported.

Instead change the requiring code to use import()

at internal/modules/cjs/loader.js:823:14

at require (internal/modules/cjs/helpers.js:88:18)
at Function.t [as __$__nodeRequire] (c:\Program Files\Microsoft VS Code\resources\app\out\vs\loader.js:5:101)
at v._loadCommonJSModule
at v._doActivateExtension
at v._activateExtension (c:\Program Files\Microsoft VS Code\resources\app\out\vs\workbench\services\extensions\node\extensionHostProcess.js:99:11695)

However, I think ES module will be more convenient in future.

Besides, TypeScript can transpile dynamic import() in CJS module in a surprising way (microsoft/TypeScript#43329), while it's never a problem in ES module where import() is emitted as is. VS Code extension authors can benefit from building extension as ES module.

@andyleejordan
Copy link
Member

For what it's worth, I ran into this trying to update node-fetch from 2.x to 3.x, since the newer version is ESM only. Looks to be the direction things are going, so extension developers are only more likely in the future to run into issues due to lack of ESM support.

@andyleejordan
Copy link
Member

FYI @TylerLeonhardt one of the Code issues I'd love to see fixed 😃

LinqLover added a commit to LinqLover/downstream-repository-mining that referenced this issue Oct 2, 2021
@Lemmingh
Copy link
Contributor

TypeScript 4.5 will perhaps have a new module option called node12, which preserves import() in CJS module. Then, although your entry point still have to be a CJS module now, you can load ES modules internally in an asynchronous manner.

See

@andyleejordan
Copy link
Member

Excuse me, @TylerLeonhardt, do you know why my comment above was marked as spam? I have to assume that was a mistake.

@alexdima
Copy link
Member

alexdima commented Nov 1, 2021

@andschwa I'm sorry, it might have been me that marked the comment as spam, but I don't remember doing it. I personally tend to hide comments that do not bring any value to the underlying discussion or distract from it.

@Malix-off
Copy link
Contributor

Malix-off commented Nov 21, 2023

I personally am desperate to make typescript + vite work for VSCode extension development.

It seems that any tsconfig.json (https://github.com/Malix-off/VSC-Extension-StashFlow/blob/dev/tsconfig.json) and vite.config (https://github.com/Malix-off/VSC-Extension-StashFlow/blob/dev/vite.config.ts) I tried didn't worked

I think it might be tied to that issue.
Do you guys found a workaround?

Update:

  1. ESNext TS + Vite works now!
    Thanks a lot to @rxliuli for chore: Update vite config of build cjs output Malix-off/VSC-Extension-StashFlow#4 ❤️

@theoludwig
Copy link

#196941 The next Electron is 27 not 28...

The next one is now Electron v28.
Progress can be tracked here: #201935

We should be able to close this issue very soon! 😄

@JustinGrote
Copy link
Contributor

I personally am desperate to make typescript + vite work for VSCode extension development.

It seems that any tsconfig.json (https://github.com/Malix-off/VSC-Extension-StashFlow/blob/dev/tsconfig.json) and vite.config (https://github.com/Malix-off/VSC-Extension-StashFlow/blob/dev/vite.config.ts) I tried didn't worked

I think it might be tied to that issue. Do you guys found a workaround?

Update:

  1. ESNext TS + Vite works now!
    Thanks a lot to @rxliuli for chore: Update vite config of build cjs output Malix-off/VSC-Extension-StashFlow#4 ❤️

FYI pretty sure this won't work inside a vscode runner with vscode-test, because the vitest runner is ES only.

@Malix-off
Copy link
Contributor

I didn't use vitest, so can't confirm

@starball5
Copy link

Related on Stack Overflow: How do I import an ES6 JavaScript module in my VS Code extension written in TypeScript?

@alexandruavadanii
Copy link

alexandruavadanii commented Feb 26, 2024

Looks like #203956 was just merged in main, so the next one of the next Insiders builds will be based on Electron 28 (probably after the endgame release from the stable branch is verified).

lukecotter added a commit to lukecotter/debug-log-analyzer that referenced this issue Feb 26, 2024
@igorskyflyer
Copy link
Author

Looks like #203956 was just merged in main, so the next one of the next Insiders builds will be based on Electron 28 (probably after the endgame release from the stable branch is verified).

Igor Dimitrijević (igorskyflyer) shared an image.

@estruyf
Copy link

estruyf commented Feb 29, 2024

image

Great news, the latest Insiders build includes Electron 28!

A big step forward, but now the VSCode loader needs to support ESM as well before we can use it in extensions.

@alexandruavadanii
Copy link

I'm not familiar enough with js, so I don't know how much of this is still relevant, but an issue has been waiting in the vscode-loader repo at microsoft/vscode-loader#36 for a while. Maybe others do have expertise in that area and can chime in.

@lppedd
Copy link

lppedd commented Feb 29, 2024

So the answer is we're still mid-way. The base platform now supports ES modules, but the VS Code infrastructure doesn't.
Correct?

@lppedd
Copy link

lppedd commented Mar 2, 2024

Btw, there is also the question of Web Extensions to consider.

IIRC as of now we compile them using CJS. And I suppose the base framework needs to be updated to handle ESM imports.

@metawrap-dev
Copy link

I have a project that depends on being able to load ESM into VSCode extensions, which I assume also means that VSCode extensions themselves need to be ESM.

I'm considering downgrading the project to CJS.

Should I wait? Is a fix to this weeks away or months... or more?

@itsdouges
Copy link

itsdouges commented Apr 22, 2024

Hi folks — can you confirm dynamic imports should work in VSCode extensions? When running my code for production (i.e. it's packaged and installed) I'm getting an Invalid host defined options error:

[error] TypeError: Invalid host defined options
    at Object.module.exports.<computed> [as createServer] (/Users/douges/.vscode/extensions/triplex.triplex-vscode-0.0.1/node_modules/vite/index.cjs:23:36)

Which, when zooming to that line of code, is this highlighted line:

// vite/index.cjs
asyncFunctions.forEach((name) => {
  module.exports[name] = (...args) =>
+    import('./dist/node/index.js').then((i) => i[name](...args))
})

Leading me to believe it's not supported. Could it be because Vite has this in a cjs file? Something else?

Edit: I've seen microsoft/vscode-loader#36. Seems quite relevant and matches my experience.

@lppedd
Copy link

lppedd commented Apr 22, 2024

@metawrap-dev imo you should switch to CJS.

Don't make plans based on what you read on issues. Not the first time highly requested features take years to land.

@JustinGrote
Copy link
Contributor

JustinGrote commented Apr 22, 2024

@itsdouges @metawrap-dev as of today, the general recommendation is to use a bundler like esbuild (recommended) or rollup and use their features/plugins to convert ES modules to CJS automatically. They can even translate default imports and you can use import syntax in your code which will get translated to the require syntax by the bundler, so when ESM is available you don't have to go back and change all that.

Generally have had no issues with this, the only major roadblock I've seen is you can't run E2E in-vscode testing using testing frameworks that require ESM such as vitest, but mocha is the de-facto standard used by everything including the official vscode test cli and you can still run tests that don't need/reference the vscode api or mock it in whatever framework you want.

@itsdouges
Copy link

itsdouges commented Apr 22, 2024 via email

@metawrap-dev
Copy link

@itsdouges @metawrap-dev as of today, the general recommendation is to use a bundler like esbuild (recommended) ...

Thanks. That's exactly what I did as a workaround. I am glad that you have confirmed the choice,

rosshamish added a commit to rosshamish/kuskus that referenced this issue Apr 29, 2024
…#148)

Update language server and language client to 7.*

With the update, we get some benefits:
- Fix load symbols (fixes bug #119) 
- Add support for load symbols on public clusters that need MSA account logins (rather than AAD/Entra account login)
- Fix load symbols on remote connections which dont support clipboard writes
- Update default load symbols params to be the public sample cluster

Also:
- Add steps for manual testing the 5 basic scenarios in kusto-language-server/README.md (activate extension, format document, load symbols, autocomplete, hover info)
- Update `open` and `clipboardy` to the newest non-ESM versions (vs code does NOT support ESM-only modules, ref: microsoft/vscode#130367)
kachick added a commit to kachick/vscode-extension-yamlfmt that referenced this issue May 10, 2024
kachick added a commit to kachick/vscode-extension-yamlfmt that referenced this issue May 13, 2024
* Install typescript dependencies

* Enable typescript for extensions

But ESM is restricted in microsoft/vscode#130367

* Fix merge miss

* Follow out dir in package.json

* Commit out dir

* Revert "Commit out dir"

This reverts commit e72912f.

* Transpile just before package

* Fix step position after npm ci
@SimonSiefke SimonSiefke linked a pull request May 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-host Extension host issues extensions Issues concerning extensions feature-request Request for new features or functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.