Repository: parcel-bundler/parcel
File: packages/runtimes/hmr/src/loaders/hmr-runtime.js (JavaScript)

Code before:

      if (handled) {
        console.clear();

        // Dispatch custom event so other runtimes (e.g React Refresh) are aware.
        if (
          typeof window !== 'undefined' &&
          typeof CustomEvent !== 'undefined'
        ) {
          window.dispatchEvent(new CustomEvent('parcelhmraccept'));
        }

        await hmrApplyUpdates(assets);

        // Dispose all old assets.
        for (let i = 0; i < assetsToDispose.length; i++) {
          let id = assetsToDispose[i][1];

          if (!acceptedAssets[id]) {
            hmrDispose(assetsToDispose[i][0], id);
            acceptedAssets[id] = true;
          }
        }

        // Run accept callbacks. This will also re-execute other disposed assets in topological order.
        acceptedAssets = {};
        for (let i = 0; i < assetsToAccept.length; i++) {
          let id = assetsToAccept[i][1];

          if (!acceptedAssets[id]) {
            hmrAccept(assetsToAccept[i][0], id);
            acceptedAssets[id] = true;
          }
        }
      } else fullReload();
    }

    if (data.type === 'error') {
      // Log parcel errors to console
      for (let ansiDiagnostic of data.diagnostics.ansi) {
        let stack = ansiDiagnostic.codeframe
          ? ansiDiagnostic.codeframe
          : ansiDiagnostic.stack;

        console.error(
          '🚨 [parcel]: ' +
            ansiDiagnostic.message +
            '\n' +
            stack +
            '\n\n' +
            ansiDiagnostic.hints.join('\n'),

Review comment (suggestion):
I think `acceptedAssets` can be a local variable now? Kind of confusing that it's really both `disposedAssets` and `acceptedAssets` at the same time (and as a global variable for no apparent reason)

Code after:
        console.clear();

        // Dispatch custom event so other runtimes (e.g React Refresh) are aware.
        if (
          typeof window !== 'undefined' &&
          typeof CustomEvent !== 'undefined'
        ) {
          window.dispatchEvent(new CustomEvent('parcelhmraccept'));
        }

        await hmrApplyUpdates(assets);

        // Dispose all old assets.
        let processedAssets = ({} /*: {|[string]: boolean|} */);
        for (let i = 0; i < assetsToDispose.length; i++) {
          let id = assetsToDispose[i][1];

          if (!processedAssets[id]) {
            hmrDispose(assetsToDispose[i][0], id);
            processedAssets[id] = true;
          }
        }

        // Run accept callbacks. This will also re-execute other disposed assets in topological order.
        processedAssets = {};
        for (let i = 0; i < assetsToAccept.length; i++) {
          let id = assetsToAccept[i][1];

          if (!processedAssets[id]) {
            hmrAccept(assetsToAccept[i][0], id);
            processedAssets[id] = true;
          }
        }
      } else fullReload();
    }

    if (data.type === 'error') {
      // Log parcel errors to console
      for (let ansiDiagnostic of data.diagnostics.ansi) {
        let stack = ansiDiagnostic.codeframe
          ? ansiDiagnostic.codeframe
          : ansiDiagnostic.stack;

        console.error(
          '🚨 [parcel]: ' +
            ansiDiagnostic.message +
            '\n' +
            stack +
            '\n\n' +
            ansiDiagnostic.hints.join('\n'),
        );