Skip to content

Minified global var names in conflict with other libraries using iife output format #7188

@thanoskrg

Description

@thanoskrg

Describe the bug

Recently I came across an extreme edge case when vite would build and minify my library in iife format, but keep some global variables outside of the main closure. This resulted to one of these minified global vars to be named after ga which resulted to conflict with the actual google analytics library, causing my library to break during runtime.

This is an example of the output javascript I got:

code

My vite configuration is similar to the following one:

import path from "path";
import minifyHTML from "rollup-plugin-minify-html-literals";
import { defineConfig } from "vite";
import eslintPlugin from "vite-plugin-eslint";

export default defineConfig(() => {
  return {
    build: {
      outDir: "dist",
      emptyOutDir: false,
      sourcemap: "hidden",
      lib: {
        name: "MyLibrary",
        entry: path.resolve(__dirname, "MyLibrary/index.ts"),
        formats: ["iife"],
        fileName: () => "my-library.js",
      },
    },
    plugins: [eslintPlugin(), minifyHTML()],
  };
});

Expected behaviour

I would expect that globally defined variables that are part of my distributable code should be enclosed in the main closure when in iife format, to avoid such issues.

Temporary solution

What I've done to "hack" this for now and avoid any potential issues is that after my build finishes I run a custom node script which wraps the output javascript file in a closure likewise:

import fs from "fs";
import glob from "glob";
import path from "path";

const ENCODING = "utf8";
const outDir = path.resolve(__dirname, "../dist");

const searchFor = `${outDir}/my-library.js`;

function closurify(content: string) {
  return `(function(){${content}})();`; // do the hack
}

glob(searchFor, {}, (err, files) => {
  files.forEach((file) => {
    fs.readFile(file, { encoding: ENCODING }, (err, content) => {
      fs.writeFile(
        file,
        closurify(content),
        { encoding: ENCODING, flag: "w" }
      );
    });
  });
});

Reproduction [UPDATED]

https://github.com/thanoskrg/lit-playground

The issue appears when I use spread operator inside index.ts and it can be noticed in the output js on the first line.

System Info

System:
    OS: macOS 11.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
    Memory: 35.39 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.13.0/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
  Browsers:
    Chrome: 98.0.4758.109
    Firefox: 97.0.1
    Safari: 14.1.2
  npmPackages:
    vite: ^2.8.0 => 2.8.0

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: library modep3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions