using my boilerplate yuh
This commit is contained in:
84
webpack/plugins/custom/TypeErrorNotifierPlugin.ts
Normal file
84
webpack/plugins/custom/TypeErrorNotifierPlugin.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { Compiler } from 'webpack';
|
||||
import path from 'path';
|
||||
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
|
||||
import WebpackBuildNotifierPlugin from 'webpack-build-notifier';
|
||||
import { Issue, IssueLocation } from 'fork-ts-checker-webpack-plugin/lib/issue';
|
||||
|
||||
interface Resource {
|
||||
path: string;
|
||||
location: IssueLocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* This plugin hooks into the fork-ts-checker-webpack-plugin and
|
||||
* notifies the developer of type errors using the webpack-build-notifier plugin.
|
||||
*/
|
||||
export default class TypeErrorNotifierPlugin {
|
||||
apply(compiler: Compiler) {
|
||||
// hook into the fork-ts-checker-webpack-plugin
|
||||
const hooks = ForkTsCheckerWebpackPlugin.getCompilerHooks(compiler);
|
||||
hooks.issues.tap('MyPlugin', issues => {
|
||||
const errors = issues.filter(issue => issue.severity === 'error');
|
||||
if (!errors?.[0]?.message) {
|
||||
return errors;
|
||||
}
|
||||
|
||||
let error = errors[0];
|
||||
let resource = getErrorResource(error);
|
||||
|
||||
try {
|
||||
notifyTypeError(resource, error.message, errors);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return errors;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function notifyTypeError(resource: Resource, message: string, errors: Issue[]) {
|
||||
const { line, column } = resource.location.start;
|
||||
|
||||
const buildNotifier = new WebpackBuildNotifierPlugin({
|
||||
logo: path.resolve('public', 'icons', 'icon_production_128.png'),
|
||||
compilationSound: 'Pop',
|
||||
failureSound: 'Sosumi',
|
||||
title: `TS: ${errors.length} errors`,
|
||||
notifyOptions: {
|
||||
open: `vscode://file/${resource.path}:${line}:${column}`,
|
||||
},
|
||||
});
|
||||
|
||||
const fakeInput = {
|
||||
hasErrors: () => true,
|
||||
compilation: {
|
||||
children: null,
|
||||
errors: [
|
||||
{
|
||||
message,
|
||||
module: {
|
||||
resource: resource.path,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
// @ts-ignore - private method
|
||||
buildNotifier.onCompilationDone(fakeInput);
|
||||
}
|
||||
|
||||
function getErrorResource(error: Issue): Resource {
|
||||
return {
|
||||
path: error.file ?? '',
|
||||
location: error.location ?? {
|
||||
end: {
|
||||
column: 0,
|
||||
line: 0,
|
||||
},
|
||||
start: {
|
||||
column: 0,
|
||||
line: 0,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
36
webpack/plugins/custom/hotReloadServer.ts
Normal file
36
webpack/plugins/custom/hotReloadServer.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Server } from 'socket.io';
|
||||
import { Compiler } from 'webpack';
|
||||
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
|
||||
|
||||
// Name of the plugin
|
||||
const PLUGIN_NAME = 'HotReloadServer';
|
||||
|
||||
// How long to wait before reloading the browser after a successful build
|
||||
const RELOAD_LOCKOUT = 2000;
|
||||
|
||||
// we want to cache all the "reload requests" here so we don't have to keep re-compiling the app while typing
|
||||
const reloads: NodeJS.Timeout[] = [];
|
||||
|
||||
let io: Server;
|
||||
|
||||
export function initializeHotReloading(port: number, compiler: Compiler) {
|
||||
io = new Server(port);
|
||||
|
||||
const hooks = ForkTsCheckerWebpackPlugin.getCompilerHooks(compiler);
|
||||
hooks.issues.tap(PLUGIN_NAME, (issues, compilation) => {
|
||||
const typeErrors = issues.filter(issue => issue.severity === 'error');
|
||||
const buildErrors = compilation?.errors ?? [];
|
||||
// if no errors (thus successful build), lets queue up a reload for the browser
|
||||
if (typeErrors.length === 0 && buildErrors.length === 0) {
|
||||
reloads.push(setTimeout(() => io.emit('reload'), RELOAD_LOCKOUT));
|
||||
}
|
||||
return typeErrors;
|
||||
});
|
||||
|
||||
// if a recompile is triggered, we want to clear out all the queue'd reloads
|
||||
// (so we don't spam-reload the extension while we are still typing
|
||||
compiler.hooks.compile.tap(PLUGIN_NAME, () => {
|
||||
reloads.forEach(reload => clearTimeout(reload));
|
||||
reloads.length = 0;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user