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

Code before:
          !t.isStringLiteral(id) ||
          !t.isStringLiteral(source)
        ) {
          throw new Error(
            'invariant: invalid signature, expected : $parcel$require(number, string)'
          );
        }

        let asset = nullthrows(assets.get(id.value));
        let dep = nullthrows(
          bundleGraph
            .getDependencies(asset)
            .find(dep => dep.moduleSpecifier === source.value)
        );

        let mod = bundleGraph.getDependencyResolution(dep);
        let node;

        if (!mod) {
          if (dep.isOptional) {
            path.replaceWith(
              THROW_TEMPLATE({MODULE: t.stringLiteral(source.value)})
            );
          } else if (
            dep.isWeak &&
            !bundle.env.isLibrary /* TODO dep.wasDeferred*/
          ) {
            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.
            if (!isUnusedValue(path)) {
              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.
              if (
                asset.meta.isCommonJS &&
                mod.meta.isES6Module &&
                mod.symbols.has('default')
              ) {
                let binding = path.scope.getBinding(name);

Review comment (suggestion):
Not happy with using `isLibrary` here, adding something like `excludeNodeModules` could break this again in the future.
Should really be `if(dep.wasDeferred)`

Code after:
        if (
          args.length !== 2 ||
          !t.isStringLiteral(id) ||
          !t.isStringLiteral(source)
        ) {
          throw new Error(
            'invariant: invalid signature, expected : $parcel$require(number, string)',
          );
        }

        let asset = nullthrows(assets.get(id.value));
        let dep = nullthrows(
          bundleGraph
            .getDependencies(asset)
            .find(dep => dep.moduleSpecifier === source.value),
        );

        let mod = bundleGraph.getDependencyResolution(dep);
        let node;

        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.
            if (!isUnusedValue(path)) {
              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.
              if (
                asset.meta.isCommonJS &&
                mod.meta.isES6Module &&
                mod.symbols.has('default')
              ) {
                let binding = path.scope.getBinding(name);
                if (binding && !binding.path.getData('hasESModuleFlag')) {