ng-packagr icon indicating copy to clipboard operation
ng-packagr copied to clipboard

Typescript enum + namespace declaration merging causes error

Open dallastjames opened this issue 7 years ago • 4 comments
trafficstars

Type of Issue

[x] Bug Report
[ ] Feature Request

Description

When trying to package a library that has an exported enum that is merged with a namespace, the build throws the error Identifier '{enumName}' has already been declared. I use this format in several different Angular projects and there are no issues when running something like this through the ngc compiler (both JIT and AOT work correctly). Seems to be something possibly related to how the packaging works with rollup. From what I can tell, rollup doesn't deal with typescript so I figured I'd start here and hopefully find a solution.

How To Reproduce

https://plnkr.co/edit/ofKm8uDao8IxQlFb0rj4

Using the files provided in the plunker (standard ng-packagr setup with a single enum file): npm i npm run build

Building Angular Package
Building entry point 'packagr-enum-bug'
Cleaning build directory
Rendering Stylesheets
Rendering Templates
Compiling TypeScript sources through ngc
Bundling to FESM15

BUILD ERROR
Identifier 'Color' has already been declared (15:11) in C:\...\packagr-bug\.ng_pkg_build\packagr-enum-bug\out\my-enum.js
SyntaxError: Identifier 'Color' has already been declared (15:11) in C:\...\packagr-bug\.ng_pkg_build\packagr-enum-bug\out\my-enum.js
    at error (C:\...\packagr-bug\node_modules\rollup\dist\rollup.js:170:15)
    at C:\...\packagr-bug\node_modules\rollup\dist\rollup.js:17548:17
    at <anonymous>

Expected Behaviour

Build should complete successfully.

Version Information

ng-packagr: 2.2.0
@angular/*: ^5.2.0
typescript: 2.6.2
node: 8.9.3
npm: 5.5.1

dallastjames avatar Mar 09 '18 01:03 dallastjames

I use the following workaround: Don't export the enum and namespace, but export Color at the end. I think the result is the same (please correct me if it isn't) and I haven't noticed any unwanted side effects.

enum Color {
    red = 1,
    green = 2,
    blue = 4
}

namespace Color {
    export function mixColor(colorName: string) {
        if (colorName == "yellow") {
            return Color.red + Color.green;
        }
        else if (colorName == "white") {
            return Color.red + Color.green + Color.blue;
        }
        else if (colorName == "magenta") {
            return Color.red + Color.blue;
        }
        else if (colorName == "cyan") {
            return Color.green + Color.blue;
        }
    }
}

export {Color};

willemsendennis avatar Apr 18 '18 10:04 willemsendennis

Workaround does not work for me with

"devDependencies": {
    "@angular/compiler-cli": "^6.0.3",
    "@angular-devkit/build-ng-packagr": "~0.6.8",
    "@angular-devkit/build-angular": "~0.6.8",
    "ng-packagr": "3.0.3",
    "tsickle": ">=0.25.5",
    "tslib": "^1.7.1",
    "typescript": "~2.7.2",
    "@angular/cli": "~6.0.8",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  }

jfrankowski avatar Jul 12 '18 18:07 jfrankowski

Any updates on this ? I am facing the same issue with following versions

  1. ng packagr: 4.4.5
  2. compiler-cli: ^6.1.1
  3. angular/cli:^6.1.2 4)angular-devkit/build-angular:~0.6.1 5)devkit-build/ng-pacakgr:~0.6.8

Will be really a help if anybody cofirms what the issue is.

jforjava1981 avatar Dec 12 '18 11:12 jforjava1981

ok it actually doesnt work with @willemsendennis solution

jforjava1981 avatar Dec 12 '18 11:12 jforjava1981

Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases. Please update to the most recent version.

alan-agius4 avatar Dec 21 '22 14:12 alan-agius4

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

This action has been performed automatically by a bot.

github-actions[bot] avatar Jan 21 '23 01:01 github-actions[bot]