Skip to content

Commit

Permalink
Makes the extension work on VSCode web (#72)
Browse files Browse the repository at this point in the history
* Readme additions

* Change alt

* Bump package versions

* Prettify the whole project

* Make build work on web too

* resolve "path" to path-browserify

* Use prettier in standalone mode for the browser
  • Loading branch information
yoavbls committed Aug 12, 2023
1 parent 995f288 commit e31e3da
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 27 deletions.
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@
"displayName": "Pretty TypeScript Errors",
"publisher": "yoavbls",
"description": "Make TypeScript errors prettier and more human-readable in VSCode",
"version": "0.5.0",
"version": "0.5.1",
"icon": "assets/icon.png",
"repository": {
"type": "git",
Expand Down Expand Up @@ -34,6 +34,7 @@
"onLanguage:mdx"
],
"main": "./dist/extension.js",
"browser": "./dist/extension.js",
"contributes": {
"languages": [
{
Expand Down Expand Up @@ -77,6 +78,7 @@
"eslint": "^8.46.0",
"glob": "^10.3.3",
"mocha": "^10.2.0",
"path-browserify": "^1.0.1",
"typescript": "^5.1.6"
},
"dependencies": {
Expand Down
58 changes: 36 additions & 22 deletions scripts/build.js
@@ -1,24 +1,38 @@
(async () => {
const ctx = await require('esbuild').context({
entryPoints: {
extension: './src/extension.ts'
},
bundle: true,
outdir: './dist',
external: ['vscode'],
format: 'cjs',
platform: 'node',
tsconfig: './tsconfig.json',
define: process.argv.includes('--production') ? { 'process.env.NODE_ENV': '"production"' } : undefined,
minify: process.argv.includes('--production'),
sourcemap: !process.argv.includes('--production'),
});
if (process.argv.includes('--watch')) {
await ctx.watch();
console.log('watching...');
}
else {
await ctx.rebuild();
await ctx.dispose();
}
const ctx = await require("esbuild").context({
entryPoints: {
extension: "./src/extension.ts",
},
bundle: true,
outdir: "./dist",
external: ["vscode"],
format: "cjs",
inject: ["./scripts/process-shim.js"],
tsconfig: "./tsconfig.json",
define: process.argv.includes("--production")
? { "process.env.NODE_ENV": '"production"' }
: undefined,
minify: process.argv.includes("--production"),
sourcemap: !process.argv.includes("--production"),
plugins: [
{
name: "node-deps",
setup(build) {
build.onResolve({ filter: /^path$/ }, (args) => {
const path = require.resolve("../node_modules/path-browserify", {
paths: [__dirname],
});
return { path };
});
},
},
],
});
if (process.argv.includes("--watch")) {
await ctx.watch();
console.log("watching...");
} else {
await ctx.rebuild();
await ctx.dispose();
}
})();
9 changes: 9 additions & 0 deletions scripts/process-shim.js
@@ -0,0 +1,9 @@
// https://esbuild.github.io/api/#inject

let _cwd = "/";

export let process = {
cwd: () => _cwd,
chdir: (newCwd) => (_cwd = newCwd),
env: {},
};
4 changes: 3 additions & 1 deletion src/format/prettify.ts
@@ -1,7 +1,9 @@
import { format } from "prettier";
import parserTypescript from "prettier/parser-typescript";
import { format } from "prettier/standalone";

export function prettify(text: string) {
return format(text, {
plugins: [parserTypescript],
parser: "typescript",
printWidth: 60,
singleAttributePerLine: false,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -9,7 +9,8 @@
"strict": true /* enable all strict type-checking options */,
/* Additional Checks */
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
"esModuleInterop": true
},
"include": ["src/**/*.ts"]
}

0 comments on commit e31e3da

Please sign in to comment.