Skip to content

Unused classes in export remain in bundle even after tree-shaking #2899

@sonicoder86

Description

@sonicoder86

I'm submitting a bug report

Webpack version:
2.1.0-beta.21

Please tell us about your environment:
Linux

Current behavior:

I have a class named Car in the file car.ts and two other classes for the engines in another file engine.ts.
car.ts

import { V8Engine, Engine } from './engine';

class SportsCar {
  constructor(private engine: Engine) {}

  toString() {
    return this.engine.toString() + ' Sports Car';
  }
}

engine.ts

export interface Engine {
  toString(): string;
}

export class V6Engine implements Engine {
  toString() {
    return 'V6';
  }
}

export class V8Engine implements Engine {
  toString() {
    return 'V8';
  }
}

export function getVersion() {
  return '1.0';
}

console.log(new SportsCar(new V8Engine()).toString());

The car.ts file only imports the V8Engine class, but the V6Engine class also appears even in the minified file. The unused function i export from engine.ts is stripped.

When running Webpack without UglifyJS plugin, the V6Engine class is marked with /* unused harmony export V6Engine */ as expected.
But after adding the plugin i get this warning message from UglifyJS:

    WARNING in car.prod.bundle.js from UglifyJs
    Dropping unused function getVersion [car.prod.bundle.js:89,9]
    Side effects in initialization of unused variable V6Engine [car.prod.bundle.js:73,132]

I have created a repository where the problem can be reproduced.
Cloning, installing and running npm run webpack and npm run webpack-prod reproduces the issue.

Don't know if this is a bug with the Typescript transpilation, UglifyJS or the orchestration of these tools.
I'm using Typescript with es2015 modules and Webpack 2.

Expected/desired behavior:

The unused class should be removed in the minified file.

  • What is the motivation / use case for changing the behavior?
  • Browser: all
  • Language: TypeScript 2.0-dev

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions