Repository: parcel-bundler/parcel
File: packages/packagers/js/src/DevPackager.js (JavaScript)

Code before:
          if (this.bundleGraph.isDependencySkipped(dep)) {
            deps[getSpecifier(dep)] = false;
          } else if (resolved) {
            deps[getSpecifier(dep)] =
              this.bundleGraph.getAssetPublicId(resolved);
          }
        }

        let {code, mapBuffer} = results[i];
        let output = code || '';
        wrapped +=
          JSON.stringify(this.bundleGraph.getAssetPublicId(asset)) +
          ':[function(require,module,exports) {\n' +
          output +
          '\n},';
        wrapped += JSON.stringify(deps);
        wrapped += ']';

        // For node environment @parcel/transformer-js replaces __dirname and __filename calls with
        // path.join containing placeholders. The packager now replaces those placeholders with
        // relative paths so that in the end the __dirname/__filename calls resolve to the correct files
        if (this.bundle.env.isNode()) {
          const relPath = normalizeSeparators(
            path.relative(
              this.bundle.target.distDir,
              path.dirname(asset.filePath),
            ),
          );
          wrapped = wrapped.replace('$parcel$dirnameReplace', relPath);
          wrapped = wrapped.replace('$parcel$filenameReplace', relPath);
        }

        if (this.bundle.env.sourceMap) {
          if (mapBuffer) {
            map.addBuffer(mapBuffer, lineOffset);
          } else {
            map.addEmptyMap(
              path
                .relative(this.options.projectRoot, asset.filePath)
                .replace(/\\+/g, '/'),
              output,
              lineOffset,
            );
          }

          lineOffset += countLines(output) + 1;
        }
        i++;
      }

      assets += wrapped;

Review comment (suggestion):
@mischnic Ok, after a bit of a treasure hunt (which was fun!) I managed to put the correct code paths into DevPackager and ScopeHoistingPackager.

How can I do the `asset.meta` optimization you talked about?

Oh, also, I changed it from `projectRoot` to `asset.filePath` as otherwise it wouldn't actually resolve to the correct file

Code after:
          if (this.bundleGraph.isDependencySkipped(dep)) {
            deps[getSpecifier(dep)] = false;
          } else if (resolved) {
            deps[getSpecifier(dep)] =
              this.bundleGraph.getAssetPublicId(resolved);
          }
        }

        let {code, mapBuffer} = results[i];
        let output = code || '';
        wrapped +=
          JSON.stringify(this.bundleGraph.getAssetPublicId(asset)) +
          ':[function(require,module,exports) {\n' +
          output +
          '\n},';
        wrapped += JSON.stringify(deps);
        wrapped += ']';

        if (
          this.bundle.env.isNode() &&
          asset.meta.has_node_replacements === true
        ) {
          const relPath = normalizeSeparators(
            path.relative(
              this.bundle.target.distDir,
              path.dirname(asset.filePath),
            ),
          );
          wrapped = wrapped.replace('$parcel$dirnameReplace', relPath);
          wrapped = wrapped.replace('$parcel$filenameReplace', relPath);
        }

        if (this.bundle.env.sourceMap) {
          if (mapBuffer) {
            map.addBuffer(mapBuffer, lineOffset);
          } else {
            map.addEmptyMap(
              path
                .relative(this.options.projectRoot, asset.filePath)
                .replace(/\\+/g, '/'),
              output,
              lineOffset,
            );
          }

          lineOffset += countLines(output) + 1;
        }
        i++;
      }

      assets += wrapped;