Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit c6e399c

Browse files
Add type module (#421)
Description of changes Add `type: module` to package.json and update various build/test scripts.
1 parent c7c9dcb commit c6e399c

File tree

7 files changed

+36
-43
lines changed

7 files changed

+36
-43
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"homepage": "https://github.com/microsoft/vscode-webview-ui-toolkit#readme",
66
"license": "MIT",
77
"author": "Microsoft Corporation",
8+
"type": "module",
89
"bugs": {
910
"url": "https://github.com/microsoft/vscode-webview-ui-toolkit/issues"
1011
},

scripts/helpers.js

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
const fs = require('fs');
5-
const path = require('path');
4+
import {existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, rmdirSync, unlinkSync, writeFileSync} from 'fs';
5+
import path from 'path';
66

7-
function createDir(dir) {
8-
if (!fs.existsSync(dir)) {
9-
fs.mkdirSync(dir);
7+
export function createDir(dir) {
8+
if (!existsSync(dir)) {
9+
mkdirSync(dir);
1010
}
1111
}
1212

13-
function copyDir(source, target) {
13+
export function copyDir(source, target) {
1414
let files = [];
1515
const targetFolder = path.join(target, path.basename(source));
16-
if (!fs.existsSync(targetFolder)) {
17-
fs.mkdirSync(targetFolder);
16+
if (!existsSync(targetFolder)) {
17+
mkdirSync(targetFolder);
1818
}
19-
if (fs.lstatSync(source).isDirectory()) {
20-
files = fs.readdirSync(source);
19+
if (lstatSync(source).isDirectory()) {
20+
files = readdirSync(source);
2121
files.forEach(function (file) {
2222
const curSource = path.join(source, file);
23-
if (fs.lstatSync(curSource).isDirectory()) {
23+
if (lstatSync(curSource).isDirectory()) {
2424
copyDir(curSource, targetFolder);
2525
} else {
2626
copyFile(curSource, targetFolder);
@@ -29,14 +29,14 @@ function copyDir(source, target) {
2929
}
3030
}
3131

32-
function copyFile(source, target) {
32+
export function copyFile(source, target) {
3333
let targetFile = target;
34-
if (fs.existsSync(target)) {
35-
if (fs.lstatSync(target).isDirectory()) {
34+
if (existsSync(target)) {
35+
if (lstatSync(target).isDirectory()) {
3636
targetFile = path.join(target, path.basename(source));
3737
}
3838
}
39-
fs.writeFileSync(targetFile, fs.readFileSync(source));
39+
writeFileSync(targetFile, readFileSync(source));
4040
}
4141

4242
const colors = {
@@ -48,32 +48,24 @@ const colors = {
4848
cyan: '\x1b[36m',
4949
};
5050

51-
function color(opts, text) {
51+
export function color(opts, text) {
5252
let colorString = '';
5353
for (const opt of opts) {
5454
colorString += colors[opt];
5555
}
5656
return `${colorString}${text}${colors.reset}`;
5757
}
5858

59-
function delDir(path) {
60-
if (fs.existsSync(path) && fs.lstatSync(path).isDirectory()) {
61-
fs.readdirSync(path).forEach(function (file, index) {
59+
export function delDir(path) {
60+
if (existsSync(path) && lstatSync(path).isDirectory()) {
61+
readdirSync(path).forEach(function (file, index) {
6262
const currPath = path + '/' + file;
63-
if (fs.lstatSync(currPath).isDirectory()) {
63+
if (lstatSync(currPath).isDirectory()) {
6464
delDir(currPath);
6565
} else {
66-
fs.unlinkSync(currPath);
66+
unlinkSync(currPath);
6767
}
6868
});
69-
fs.rmdirSync(path);
69+
rmdirSync(path);
7070
}
7171
}
72-
73-
module.exports = {
74-
createDir,
75-
copyDir,
76-
copyFile,
77-
color,
78-
delDir,
79-
};

scripts/move-react-build-dir.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
const {copyDir, color, delDir} = require('./helpers');
5-
const fsPromises = require('fs').promises;
6-
const process = require('process');
4+
import {copyDir, color, delDir} from './helpers.js';
5+
import process from 'process';
6+
import {readFile, writeFile} from 'fs/promises';
77

88
/**
99
* Developer note:
@@ -67,7 +67,7 @@ async function updateReactBuildImportPaths(path) {
6767

6868
// Read React build file and update import paths if appropriate
6969
try {
70-
const fileContents = await fsPromises.readFile(path, {encoding: 'utf8'});
70+
const fileContents = await readFile(path, {encoding: 'utf8'});
7171
// These regex strings rely on an assumption that they will not change
7272
// If importing React components from the toolkit starts to break check here first
7373
result = fileContents.replace(/\.\.\/index/g, '../dist/index');
@@ -80,7 +80,7 @@ async function updateReactBuildImportPaths(path) {
8080

8181
// Overwrite React build file with any updated import paths
8282
try {
83-
await fsPromises.writeFile(path, result, {encoding: 'utf8'});
83+
await writeFile(path, result, {encoding: 'utf8'});
8484
} catch (err) {
8585
console.log(`${color(['red'], 'Error: Writing new React build file import paths failed.')}\n ${err}`);
8686
process.exit();

scripts/setup-webview-test-env.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
const {createDir, copyDir, color, delDir} = require('./helpers');
5-
const {exec} = require('child_process');
6-
const fs = require('fs');
7-
const process = require('process');
8-
const util = require('util');
4+
import {createDir, copyDir, color, delDir} from './helpers.js';
5+
import {existsSync} from 'fs';
6+
import {exec} from 'child_process';
7+
import {promisify} from 'util';
8+
import process from 'process';
99

10-
const execShellCommand = util.promisify(exec);
10+
const execShellCommand = promisify(exec);
1111

1212
async function main() {
1313
// Empty print line to pretty-ify command line output
1414
console.log();
1515

1616
// Copy webview test environment locally if it does not already exist
17-
if (!fs.existsSync('./test-webview')) {
17+
if (!existsSync('./test-webview')) {
1818
try {
1919
console.log(color(['dim'], 'Copying webview test environment locally...'));
2020
await execShellCommand('npx degit microsoft/vscode-webview-ui-toolkit-samples/default/component-gallery test-webview');
@@ -25,7 +25,7 @@ async function main() {
2525
}
2626

2727
// Install the webview test environment dependencies if they do not exist
28-
if (!fs.existsSync('./test-webview/node_modules')) {
28+
if (!existsSync('./test-webview/node_modules')) {
2929
try {
3030
console.log(color(['dim'], 'Installing webview test environment dependencies...'));
3131
await execShellCommand('cd ./test-webview && npm install');

0 commit comments

Comments
 (0)