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

Code before:

    this.isAsyncBundle =
      this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') &&
      !this.bundle.env.isIsolated() &&
      this.bundle.bundleBehavior !== 'isolated';

    this.globalNames = GLOBALS_BY_CONTEXT[bundle.env.context];
  }

  async package(): Promise<{|contents: string, map: ?SourceMap|}> {
    let wrappedAssets = await this.loadAssets();
    this.buildExportedSymbols();

    // If building a library, the target is actually another bundler rather
    // than the final output that could be loaded in a browser. So, loader
    // runtimes are excluded, and instead we add imports into the entry bundle
    // of each bundle group pointing at the sibling bundles. These can be
    // picked up by another bundler later at which point runtimes will be added.
    if (
      this.bundle.env.isLibrary ||
      this.bundle.env.outputFormat === 'commonjs'
    ) {
      for (let b of this.bundleGraph.getReferencedBundles(this.bundle)) {
        let entry = b.getMainEntry();
        let symbols = new Map();
        if (entry && !this.isAsyncBundle && entry.type === 'js') {
          this.externalAssets.add(entry);

          let usedSymbols = this.bundleGraph.getUsedSymbols(entry) || new Set();
          for (let s of usedSymbols) {
            symbols.set(s, this.getSymbolResolution(entry, entry, s));
          }
        }

        this.externals.set(relativeBundlePath(this.bundle, b), symbols);
      }
    }

    let res = '';
    let lineCount = 0;
    let sourceMap = null;
    let processAsset = asset => {
      let [content, map, lines] = this.visitAsset(asset);
      if (sourceMap && map) {
        sourceMap.addSourceMap(map, lineCount);
      } else if (this.bundle.env.sourceMap) {
        sourceMap = map;
      }

      res += content + '\n';
      lineCount += lines + 1;

Review comment (suggestion):
for now async bundles in libraries are still referenced with `parcelRequire`. Changing that would require additional changes to the runtimes, but we could fix it eventually.

Code after:

    this.isAsyncBundle =
      this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') &&
      !this.bundle.env.isIsolated() &&
      this.bundle.bundleBehavior !== 'isolated';

    this.globalNames = GLOBALS_BY_CONTEXT[bundle.env.context];
  }

  async package(): Promise<{|contents: string, map: ?SourceMap|}> {
    let wrappedAssets = await this.loadAssets();
    this.buildExportedSymbols();

    // If building a library, the target is actually another bundler rather
    // than the final output that could be loaded in a browser. So, loader
    // runtimes are excluded, and instead we add imports into the entry bundle
    // of each bundle group pointing at the sibling bundles. These can be
    // picked up by another bundler later at which point runtimes will be added.
    if (
      this.bundle.env.isLibrary ||
      this.bundle.env.outputFormat === 'commonjs'
    ) {
      for (let b of this.bundleGraph.getReferencedBundles(this.bundle)) {
        let entry = b.getMainEntry();
        let symbols = new Map();
        if (entry && !this.isAsyncBundle && entry.type === 'js') {
          this.externalAssets.add(entry);

          let usedSymbols = this.bundleGraph.getUsedSymbols(entry) || new Set();
          for (let s of usedSymbols) {
            // If the referenced bundle is ESM, and we are importing '*', use 'default' instead.
            // This matches the logic below in buildExportedSymbols.
            let imported = s;
            if (imported === '*' && b.env.outputFormat === 'esmodule') {
              imported = 'default';
            }
            symbols.set(imported, this.getSymbolResolution(entry, entry, s));
          }
        }

        this.externals.set(relativeBundlePath(this.bundle, b), symbols);
      }
    }

    let res = '';
    let lineCount = 0;
    let sourceMap = null;
    let processAsset = asset => {
      let [content, map, lines] = this.visitAsset(asset);
      if (sourceMap && map) {
        sourceMap.addSourceMap(map, lineCount);