-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.js
36 lines (35 loc) · 1.43 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const vscode = require('vscode');
const babel = require("@babel/core");
const esmCjsPlugin = require('./plugins/esm-cjs-plugin')
const cjsEsmPlugin = require('./plugins/cjs-esm-plugin')
function activate(context) {
const esmoduleToCommonjsDisposable = vscode.commands.registerCommand('extension.esmoduleToCommonjs', function () {
vscode.window.activeTextEditor.edit(editBuilder => {
const text = vscode.window.activeTextEditor.document.getText();
let { code } = babel.transformSync(text, {
sourceType: 'module',
plugins: [esmCjsPlugin]
});
const end = new vscode.Position(vscode.window.activeTextEditor.document.lineCount + 1, 0);
editBuilder.replace(new vscode.Range(new vscode.Position(0, 0), end), code);
});
});
context.subscriptions.push(esmoduleToCommonjsDisposable);
const commonjsToEsmoduleDisposable = vscode.commands.registerCommand('extension.commonjsToEsmodule', function () {
vscode.window.activeTextEditor.edit(editBuilder => {
const text = vscode.window.activeTextEditor.document.getText();
let { code } = babel.transformSync(text, {
sourceType: 'module',
plugins: [cjsEsmPlugin]
});
const end = new vscode.Position(vscode.window.activeTextEditor.document.lineCount + 1, 0);
editBuilder.replace(new vscode.Range(new vscode.Position(0, 0), end), code);
});
});
context.subscriptions.push(commonjsToEsmoduleDisposable);
}
function deactivate() { }
module.exports = {
activate,
deactivate
};