Repository: parcel-bundler/parcel
File: packages/shared/scope-hoisting/src/link.js (JavaScript)

Code before:
                }

                for (let path of binding.constantViolations) {
                  path.insertAfter(ESMODULE_TEMPLATE({EXPORTS: name}));
                }

                binding.path.setData('hasESModuleFlag', true);
              }
            }

            // We need to wrap the module in a function when a require
            // call happens inside a non top-level scope, e.g. in a
            // function, if statement, or conditional expression.
            if (mod.meta.shouldWrap) {
              let call = t.callExpression(getIdentifier(mod, 'init'), []);
              node = node ? t.sequenceExpression([call, node]) : call;
            }
          } else if (mod.type === 'js') {
            node = addBundleImport(mod, path);
          }

          if (node) {
            path.replaceWith(node);
          } else {
            path.remove();
          }
        }
      } else if (callee.name === '$parcel$require$resolve') {
        let [id, source] = args;
        if (
          args.length !== 2 ||
          !t.isStringLiteral(id) ||
          !t.isStringLiteral(source)
        ) {
          throw new Error(
            'invariant: invalid signature, expected : $parcel$require$resolve(number, string)',
          );
        }

        let mapped = nullthrows(assets.get(id.value));
        let dep = nullthrows(
          bundleGraph
            .getDependencies(mapped)
            .find(dep => dep.moduleSpecifier === source.value),
        );
        let mod = nullthrows(bundleGraph.getDependencyResolution(dep));
        path.replaceWith(t.valueToNode(mod.id));
      }
    },
    VariableDeclarator: {
      exit(path) {

Review comment (suggestion):
Was there a reason why `isCommonJS` was checked here? From my point of view, it does not matter as long as it is an es6 module with a default export.

Code after:
        if (!mod) {
          if (dep.isOptional) {
            path.replaceWith(
              THROW_TEMPLATE({MODULE: t.stringLiteral(source.value)}),
            );
          } else if (dep.isWeak && dep.isDeferred) {
            path.remove();
          } else {
            let name = addExternalModule(path, dep);
            if (isUnusedValue(path) || !name) {
              path.remove();
            } else {
              path.replaceWith(t.identifier(name));
            }
          }
        } else {
          if (mod.meta.id && assets.get(assertString(mod.meta.id))) {
            // Replace with nothing if the require call's result is not used.
            let isReferenced = bundleGraph.isAssetReferencedByAnotherBundleOfType(
              mod,
              'js',
            );
            if (!isUnusedValue(path) || isReferenced) {
              let name = assertString(mod.meta.exportsIdentifier);
              node = t.identifier(replacements.get(name) || name);

              // Insert __esModule interop flag if the required module is an ES6 module with a default export.
              // This ensures that code generated by Babel and other tools works properly.
              const hasESModuleDefaultExport =
                mod.meta.isES6Module && mod.symbols.has('default');
              const isUsed = asset.meta.isCommonJS && hasESModuleDefaultExport;
              // TODO If referenced it might be used in another bundle, even though we don't know for sure. If not included but required, the code would throw so we insert it always for now. We should find out when it is required and exclude it if now.
              const mightBeUsed = isReferenced && hasESModuleDefaultExport;
              if (isUsed || mightBeUsed) {
                let binding = path.scope.getBinding(name);
                if (binding && !binding.path.getData('hasESModuleFlag')) {
                  if (binding.path.node.init) {
                    binding.path
                      .getStatementParent()
                      .insertAfter(
                        ESMODULE_TEMPLATE({EXPORTS: t.identifier(name)}),
                      );
                  }

                  for (let path of binding.constantViolations) {
                    path.insertAfter(
                      ESMODULE_TEMPLATE({EXPORTS: t.identifier(name)}),
                    );
                  }

                  binding.path.setData('hasESModuleFlag', true);