Skip to content

Library mode umd build hangs #14065

Closed

Description

@fnlctrl

Describe the bug

We have a rather large library (~9MB mininized) being converted from webpack to vite. The esm format builds just fine, but umd builds hangs for ~7 minutes. After searching a bit I found the same issue #10900 and doing profiling myself revealed the same offending regex

const INJECT_HELPERS_UMD_RE =

^(.*?)(\(function\([^)]*\)\s*\{.+?amd.+?function\([^)]*\)\s*\{\s*"use strict";)

image

I can't share the code, but the closest I got is getting a repo in library mode with as much bundled code as possible - though it didn't hang for that long.

Looking at the #7948 that fixed the collision issue, maybe we should find another fix that configures esbuild properly so that no regex replacement is required?

Reproduction

https://stackblitz.com/edit/vitejs-vite-bpuk79?file=index.ts

Steps to reproduce

  1. Open the stackblitz link
  2. Run npm run build
  3. Go to dist and copy the file content to https://regex101.com/
  4. Fill in the regex as ^(.*?)(\(function\([^)]*\)\s*\{.+?amd.+?function\([^)]*\)\s*\{\s*"use strict";)
  5. Wait for it to show "catastrophic backtracking", click it
    image
  6. See that it halted after 160k steps. My project's output was halted at 400k steps.
    image

System Info

System:
    OS: macOS 13.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 170.47 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.13.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.19.3 - /usr/local/bin/npm
    pnpm: 8.6.2 - /usr/local/bin/pnpm
  Browsers:
    Chrome: 115.0.5790.170
    Safari: 16.5.2
  npmPackages:
    vite: ^4.4.9 => 4.4.9

Used Package Manager

pnpm

Logs

No response

Validations

Activity

  1. bolt-new-by-stackblitz commented on Aug 10, 2023

    @bolt-new-by-stackblitz
  2. fnlctrl commented on Aug 10, 2023

    @fnlctrl
    ContributorAuthor

    I think I've found a minimal reproduction similar to the #7188 issue:
    https://stackblitz.com/edit/vitejs-vite-vlp9mq?file=package.json,vite.config.ts&terminal=build

    When the target requires helpers (e.g. target: ['chrome66', 'safari11']), and set minify: false
    Have the library entry export from another file that uses object spread, and have another expression that uses optional chaining

    // foo.ts
    export const foo = { ...{} };
    // index.ts
    export * from './foo';
    foo()?.bar;

    will cause the output to contain helpers injected to the global scope

    var __defProp = Object.defineProperty;
    var __getOwnPropSymbols = Object.getOwnPropertySymbols;
    var __hasOwnProp = Object.prototype.hasOwnProperty;
    var __propIsEnum = Object.prototype.propertyIsEnumerable;
    var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
    var __spreadValues = (a, b) => {
      for (var prop in b || (b = {}))
        if (__hasOwnProp.call(b, prop))
          __defNormalProp(a, prop, b[prop]);
      if (__getOwnPropSymbols)
        for (var prop of __getOwnPropSymbols(b)) {
          if (__propIsEnum.call(b, prop))
            __defNormalProp(a, prop, b[prop]);
        }
      return a;
    };
    (function(global, factory) {
      typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.Test = {}));
    })(this, function(exports2) {
      var _a;
      "use strict";
      const foo$1 = __spreadValues({}, {});
      (_a = foo()) == null ? void 0 : _a.bar;
      exports2.foo = foo$1;
      Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
    });

    However, commenting out either of the lines will prevent the helpers to become global.

    Maybe this could help to discovering a better fix to #7188

  3. locked and limited conversation to collaborators on Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions