Skip to content

🎥 Make videos programmatically with React

error overlay

Jonny Burger3a505b9b6597
diff --git a/packages/bundler/src/error-overlay/entry-basic.ts b/packages/bundler/src/error-overlay/entry-basic.ts
new file mode 100644
index 0000000000..25cb1a5f4d
--- /dev/null
+++ b/packages/bundler/src/error-overlay/entry-basic.ts
@@ -0,0 +1,38 @@
+const {
+ setEditorHandler,
+ startReportingRuntimeErrors,
+ dismissRuntimeErrors,
+} = require('react-error-overlay');
+const launchEditorEndpoint = require('react-dev-utils/launchEditorEndpoint');
+
+type ErrorLocation = {
+ fileName: string;
+ lineNumber: number;
+ colNumber?: number;
+};
+
+setEditorHandler((errorLocation: ErrorLocation) => {
+ // Keep this sync with errorOverlayMiddleware.js
+ fetch(
+ launchEditorEndpoint +
+ '?fileName=' +
+ window.encodeURIComponent(errorLocation.fileName) +
+ '&lineNumber=' +
+ window.encodeURIComponent(errorLocation.lineNumber || 1) +
+ '&colNumber=' +
+ window.encodeURIComponent(errorLocation.colNumber || 1)
+ );
+});
+
+startReportingRuntimeErrors({
+ onError() {
+ if (module.hot) {
+ module.hot.addStatusHandler((status) => {
+ if (status === 'apply') {
+ // window.location.reload();
+ dismissRuntimeErrors();
+ }
+ });
+ }
+ },
+});
diff --git a/packages/bundler/src/error-overlay/entry-devserver.ts b/packages/bundler/src/error-overlay/entry-devserver.ts
new file mode 100644
index 0000000000..73bbad0e59
--- /dev/null
+++ b/packages/bundler/src/error-overlay/entry-devserver.ts
@@ -0,0 +1,43 @@
+// @ts-check
+const querystring = require('querystring');
+const url = require('url');
+const SockJS = require('sockjs-client');
+const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
+const {reportBuildError, dismissBuildError} = require('react-error-overlay');
+
+let sockOptions: {
+ sockHost?: string;
+ sockPort?: string;
+ sockPath?: string;
+} = {};
+if (typeof __resourceQuery === 'string' && __resourceQuery.length > 1) {
+ sockOptions = querystring.parse(__resourceQuery.substr(1));
+}
+
+const connection = new SockJS(
+ url.format({
+ protocol: window.location.protocol,
+ hostname: sockOptions.sockHost || window.location.hostname,
+ port: sockOptions.sockPort || window.location.port,
+ pathname: sockOptions.sockPath || '/sockjs-node',
+ })
+);
+
+connection.onmessage = function (e: {data: string}) {
+ const {type, data} = JSON.parse(e.data);
+ let formatted;
+ switch (type) {
+ case 'ok':
+ dismissBuildError();
+ break;
+ case 'errors':
+ formatted = formatWebpackMessages({
+ errors: data,
+ warnings: [],
+ });
+ reportBuildError(formatted.errors[0]);
+ break;
+ default:
+ // Do nothing.
+ }
+};
diff --git a/packages/bundler/src/error-overlay/index.ts b/packages/bundler/src/error-overlay/index.ts
new file mode 100644
index 0000000000..45844505b5
--- /dev/null
+++ b/packages/bundler/src/error-overlay/index.ts
@@ -0,0 +1,111 @@
+import {Router} from 'express';
+import webpack from 'webpack';
+
+const errorOverlayMiddleware = require('react-dev-utils/errorOverlayMiddleware');
+
+const chunkPathBasic = require.resolve('./entry-basic.js');
+const chunkPathDevServer = require.resolve('./entry-devserver.js');
+let deps: string[] = [];
+
+type SockOptions = {
+ sockHost?: string;
+ sockPath?: string;
+ sockPort?: string;
+};
+
+class ErrorOverlayPlugin {
+ apply(compiler: webpack.Compiler) {
+ const className = this.constructor.name;
+
+ if (compiler.options.mode !== 'development') {
+ return;
+ }
+
+ if (!compiler.options.devServer) {
+ return;
+ }
+
+ const devServerEnabled = Boolean(compiler.options.devServer);
+ const usingSocket =
+ devServerEnabled && typeof compiler.options.devServer.socket === 'string';
+
+ const sockOptions: SockOptions = {};
+ if (devServerEnabled && usingSocket) {
+ sockOptions.sockHost = compiler.options.devServer.sockHost;
+ sockOptions.sockPath = compiler.options.devServer.sockPath;
+ sockOptions.sockPort = compiler.options.devServer.sockPort;
+ }
+
+ compiler.hooks.entryOption.tap(
+ className,
+ (context: unknown, entry: webpack.EntryNormalized) => {
+ if (typeof entry !== 'function') {
+ Object.keys(entry).forEach((entryName) => {
+ if (deps.includes(entryName)) {
+ // Skip dependencies, only inject real entry points
+ return;
+ }
+
+ const _entry = entry[entryName];
+
+ function adjustEntry(enableDevServer: boolean) {
+ if (_entry.dependOn) {
+ deps = [...deps, ..._entry.dependOn];
+ }
+
+ if (_entry.library) {
+ // skip libraries
+ return _entry;
+ }
+
+ if (typeof _entry.import === 'string') {
+ _entry.import = [_entry.import];
+ }
+
+ if (enableDevServer && usingSocket) {
+ const sockHost = sockOptions.sockHost
+ ? `&sockHost=${sockOptions.sockHost}`
+ : '';
+ const sockPath = sockOptions.sockPath
+ ? `&sockPath=${sockOptions.sockPath}`
+ : '';
+ const sockPort = sockOptions.sockPort
+ ? `&sockPort=${sockOptions.sockPort}`
+ : '';
+ const chunkPathDevServerWithParams = `${chunkPathDevServer}?${sockHost}${sockPath}${sockPort}`;
+ if (!_entry.import?.includes(chunkPathDevServerWithParams)) {
+ _entry.import?.unshift(chunkPathDevServerWithParams);
+ }
+ }
+
+ if (!_entry.import?.includes(chunkPathBasic)) {
+ _entry.import?.unshift(chunkPathBasic);
+ }
+
+ return _entry;
+ }
+
+ entry[entryName] = adjustEntry(devServerEnabled);
+ });
+ }
+
+ return false;
+ }
+ );
+
+ compiler.hooks.afterResolvers.tap(className, ({options}) => {
+ if (devServerEnabled && options.devServer) {
+ const originalBefore = options.devServer.before;
+ options.devServer.before = (app: Router, server: unknown) => {
+ if (originalBefore) {
+ originalBefore(app, server);
+ }
+
+ app.use(errorOverlayMiddleware());
+ };
+ }
+ });
+ }
+}
+
+module.exports = ErrorOverlayPlugin;