Repository: parcel-bundler/parcel
File: packages/utils/node-resolver-core/src/NodeResolver.js (JavaScript)

Code before:

    // Add the invalidation *before* we try to parse the JSON in case of errors
    // so that changes are picked up if the file is edited to fix the error.
    ctx.invalidateOnFileChange.add(file);
    let pkg = JSON.parse(json);

    await this.processPackage(pkg, file, dir);

    this.packageCache.set(file, pkg);
    return pkg;
  }

  async processPackage(pkg: InternalPackageJSON, file: string, dir: string) {
    pkg.pkgfile = file;
    pkg.pkgdir = dir;

    // If the package has a `source` field, make sure
    // - the package is behind symlinks
    // - and the realpath to the packages does not includes `node_modules`.
    // Since such package is likely a pre-compiled module
    // installed with package managers, rather than including a source code.
    if (pkg.source) {
      let realpath = await this.fs.realpath(file);
      if (
        realpath === file ||
        normalizeSeparators(realpath).includes('/node_modules/')
      ) {
        delete pkg.source;
      }
    }
  }

  getPackageEntries(
    pkg: InternalPackageJSON,
    env: Environment,
  ): Array<{|
    filename: string,
    field: string,
  |}> {
    return this.mainFields
      .map(field => {
        if (field === 'browser' && pkg.browser != null) {
          if (!env.isBrowser()) {
            return null;
          } else if (typeof pkg.browser === 'string') {
            return {field, filename: pkg.browser};
          } else if (typeof pkg.browser === 'object' && pkg.browser[pkg.name]) {
            return {
              field: `browser/${pkg.name}`,
              filename: pkg.browser[pkg.name],
            };

Review comment (suggestion):
`realpath` will already return a normalized string for the platform it is running on, so might be faster to do this instead:

```suggestion
        realpath.includes(`${path.sep}node_modules${path.sep}`)
```

Code after:

    // Add the invalidation *before* we try to parse the JSON in case of errors
    // so that changes are picked up if the file is edited to fix the error.
    ctx.invalidateOnFileChange.add(file);
    let pkg = JSON.parse(json);

    await this.processPackage(pkg, file, dir);

    this.packageCache.set(file, pkg);
    return pkg;
  }

  async processPackage(pkg: InternalPackageJSON, file: string, dir: string) {
    pkg.pkgfile = file;
    pkg.pkgdir = dir;

    // If the package has a `source` field, make sure
    // - the package is behind symlinks
    // - and the realpath to the packages does not includes `node_modules`.
    // Since such package is likely a pre-compiled module
    // installed with package managers, rather than including a source code.
    if (pkg.source) {
      let realpath = await this.fs.realpath(file);
      if (
        realpath === file ||
        realpath.includes(`${path.sep}node_modules${path.sep}`)
      ) {
        delete pkg.source;
      }
    }
  }

  getPackageEntries(
    pkg: InternalPackageJSON,
    env: Environment,
  ): Array<{|
    filename: string,
    field: string,
  |}> {
    return this.mainFields
      .map(field => {
        if (field === 'browser' && pkg.browser != null) {
          if (!env.isBrowser()) {
            return null;
          } else if (typeof pkg.browser === 'string') {
            return {field, filename: pkg.browser};
          } else if (typeof pkg.browser === 'object' && pkg.browser[pkg.name]) {
            return {
              field: `browser/${pkg.name}`,
              filename: pkg.browser[pkg.name],
            };