Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 98994

how to import mjs in vscode?

$
0
0

Is it possible to make a vscode extension made of mjs files?

because I tried to make an extension with mjs files only, in order to have full es6 features without TypeScript. But it does not run:

If I make the extension with $ vsce package it does not give any error but it makes an extension that does not work when installed: the contributions that I've put in the package.json are present but vscode shows an error popup that says

Activating extension 'my.ext' failed: Must use import to load ES Module: c:\vsext\extension.mjs.

and every command I try to run gives an error

command 'my.cmd' not found

If I run the extension on the debugger, and the breakpoint on uncaught exception option flagged, it breaks on /src/vs/workbench/api/node/extHostExtensionService.ts:88.

After further search, I noticed that this exception in generatend when the script tries to load the first mjs module.

there is something I can do in order to include my mjs library files?

I think that this behaviour could also impact the use of npm modules with mjs files.


EDIT

Found (kind of) a way using esm:

The idea is to use esm to handle es6 imports and share the vscode object between imported modules

this could seem quite tricky but when I tried to just import * as vscode from "vscode" im my mjs files, vscode complained that can't find module vscode.

so I've done the following

  • add 'esm' as dependency
  • in all the files where vscode is used, remove the import of vscode and add something like this function
var vscode; // will store vscode obj
export function IMPORTVSCODE(vscodeInstance){
    vscode = vscodeInstance
}
  • create a init file where you require vscode (with node native require) and your main module (with esm require)
  • in the init file call main.IMPORTVSCODE(vscode)
  • on all the files A that imports a file B that need vscode, before use the exported stuff from file B, call B.IMPORTVSCODE(vscode)

for example

// init.js
const vscode = require("vscode")
const esm = require("esm")(module/*, options*/)
const main = esm("./main.mjs")
main.IMPORTVSCODE(vscode)
module.exports = main

// main.js
import * as other from "./other.mjs"
var vscode; // will store vscode obj
export function IMPORTVSCODE(vscodeInstance){
    vscode = vscodeInstance
}
function activate(){
  other.IMPORTVSCODE(vscode)
  other.methodThatNeedsVscode()
}

// other.mjs
var vscode; // will store vscode obj
export function IMPORTVSCODE(vscodeInstance){
    vscode = vscodeInstance
}
export function methodThatNeedsVscode(){
    vscode // ...use vscode
}

My extension runs fine now!

But I think that better ideas could be found, so if somebody has some finest solutions, please share them


Viewing all articles
Browse latest Browse all 98994

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>