Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minifier removes too much code, leaving variable undefined #8271

Closed
mischnic opened this issue Nov 11, 2023 · 1 comment · Fixed by #8283
Closed

minifier removes too much code, leaving variable undefined #8271

mischnic opened this issue Nov 11, 2023 · 1 comment · Fixed by #8283
Assignees
Labels
Milestone

Comments

@mischnic
Copy link
Contributor

mischnic commented Nov 11, 2023

Describe the bug

The minified removes too much code

Input code

var $eb2fd35624c84372$exports = {};
var $eb2fd35624c84372$var$__esDecorate = $eb2fd35624c84372$exports && $eb2fd35624c84372$exports.__esDecorate || function(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
    function accept(f) {
        if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected");
        return f;
    }
    var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
    var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
    var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
    var _, done = false;
    for(var i = decorators.length - 1; i >= 0; i--){
        var context = {};
        for(var p in contextIn)context[p] = p === "access" ? {} : contextIn[p];
        for(var p in contextIn.access)context.access[p] = contextIn.access[p];
        context.addInitializer = function(f) {
            if (done) throw new TypeError("Cannot add initializers after decoration has completed");
            extraInitializers.push(accept(f || null));
        };
        var result = (0, decorators[i])(kind === "accessor" ? {
            get: descriptor.get,
            set: descriptor.set
        } : descriptor[key], context);
        if (kind === "accessor") {
            if (result === void 0) continue;
            if (result === null || typeof result !== "object") throw new TypeError("Object expected");
            if (_ = accept(result.get)) descriptor.get = _;
            if (_ = accept(result.set)) descriptor.set = _;
            if (_ = accept(result.init)) initializers.unshift(_);
        } else if (_ = accept(result)) {
            if (kind === "field") initializers.unshift(_);
            else descriptor[key] = _;
        }
    }
    if (target) Object.defineProperty(target, contextIn.name, descriptor);
    done = true;
};
var $eb2fd35624c84372$var$__runInitializers = $eb2fd35624c84372$exports && $eb2fd35624c84372$exports.__runInitializers || function(thisArg, initializers, value) {
    var useValue = arguments.length > 2;
    for(var i = 0; i < initializers.length; i++)value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
    return useValue ? value : void 0;
};
Symbol.metadata ??= Symbol("Symbol.metadata");
const $eb2fd35624c84372$var$CustomElement = (tag)=>{
    return (ctor, context)=>{
        context.addInitializer(function() {
            customElements.define(tag, this);
            console.log("metadata:", context.metadata);
        });
        return ctor;
    };
};
let $eb2fd35624c84372$var$A = (()=>{
    let _classDecorators = [
        $eb2fd35624c84372$var$CustomElement("component-a")
    ];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _classSuper = HTMLElement;
    var A = class extends _classSuper {
        static{
            _classThis = this;
        }
        static{
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            $eb2fd35624c84372$var$__esDecorate(null, _classDescriptor = {
                value: _classThis
            }, _classDecorators, {
                kind: "class",
                name: _classThis.name,
                metadata: _metadata
            }, null, _classExtraInitializers);
            A = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, {
                enumerable: true,
                configurable: true,
                writable: true,
                value: _metadata
            });
            $eb2fd35624c84372$var$__runInitializers(_classThis, _classExtraInitializers);
        }
        constructor(){
            super();
            this.innerHTML = "Component A is working";
        }
    };
    return A = _classThis;
})();
console.log(new $eb2fd35624c84372$var$A().tagName);

Config

const config = {
  jsc: {
    target: "es2022",
    minify: {
      mangle: true,
      compress: true,
      toplevel: false,
      module: false,
    },
  },
  minify: true,
  sourceMaps: false,
  configFile: false,
  swcrc: false,
};

await swc.transform(fs.readFileSync(input, "utf8"), config);

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.94-nightly-20231016.1&code=H4sIAAAAAAAAA6VXbW%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Xs8YiI2vWSvhDDdqMMOluN8tgzcFuJb8anfBkDd68g9AKhAtUbYJNL%2BThpd52rL2u5YhexlZHhQu8629MkMnRTUheKWyY2WgcNC6WcDVFsbKtlo6C94oHwUHBryXWVFc03vg9qwF5aEH9AqQ%2FlqvTdUMzGMLN%2Fi4EDv4j7yS9QFbNJLl8zalIZ0w7gRIwGd0OwrSCsZtXORK3PptXrMDGQ1wqkqlCbweAVkARhOBtdmQ9RCPpot4%2BjwTi8L9cW%2BTzd3nvn85uSxzPrlugI4%2Fj8fmbWFNQlcno%2BXIj32f9X49Ka3Xec2sCfR3NZxyuf8Hw%2FodQi8GAAA%3D

SWC Info output

1.3.94-nightly-20231016.1

Expected behavior

Still works afterwards

Actual behavior

The class is gone and _classThis undefined

let _classThis;
var $eb2fd35624c84372$exports = {};
$eb2fd35624c84372$exports && $eb2fd35624c84372$exports.__esDecorate,
  $eb2fd35624c84372$exports && $eb2fd35624c84372$exports.__runInitializers,
  (Symbol.metadata ??= Symbol("Symbol.metadata")),
  console.log(new (HTMLElement, _classThis)().tagName);

Version

1.3.94-nightly-20231016.1

Additional context

swc fails to minify what tsc outputs for

// @ts-ignore assign to readonly property as polyfill
Symbol.metadata ??= Symbol("Symbol.metadata");

const CustomElement = (tag: string) => {
  return <T extends new () => HTMLElement>(ctor: T, context: ClassDecoratorContext<T>) => {
    context.addInitializer(function () {
      customElements.define(tag, this);
      console.log("metadata:", context.metadata);
    });
    return ctor;
  };
};

@CustomElement("component-a")
class A extends HTMLElement {
  constructor() {
    super();
    this.innerHTML = "Component A is working";
  }
}

console.log(new A().tagName);

parcel-bundler/parcel#9219

@mischnic mischnic added the C-bug label Nov 11, 2023
@mischnic mischnic changed the title minified removes too much code minifier removes too much code, leaving variable undefined Nov 11, 2023
@kdy1 kdy1 self-assigned this Nov 11, 2023
@kdy1 kdy1 added this to the Planned milestone Nov 11, 2023
kdy1 pushed a commit that referenced this issue Nov 14, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.98 Nov 21, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Dec 21, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Dec 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3 participants