ToddThomson / tsproject

Typescript minifier and modular typescript bundle optimizer for gulp (Ts Vinyl Adapter).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Duplicate identifier if using visual studio internal modules?

mykohsu opened this issue · comments

[TsProject] Scripts/Framework/Class/Main.ts(5,18): error TS2300: Duplicate identifier 'Main'.

@mykohsu I would love to help you out, but you haven't really given me much information to go on. Can you please submit a concise gist or some source that I can use to reproduce the issue?

TsProject works by combining multiple ES6 source modules into a single ES6 source module/bundle/package. The idea is to package a complete application ( which would have a single main entry point ) or a complete packaged library.

The diagnostic error indicates that you have 2 ( or more ) identifiers ( functions, classes, variables, etc.) that have the same name in the bundle.

@ToddThomson Sorry for the late response. Unfortunately for me I'm limited to what I can post, but will try to put together an example that shows what is happening.

Here is the tsproject.json, which is under Scripts\Framework:

{
    "compilerOptions": {
        "diagnostics": true,
        "inlineSourceMap": true,
        "noResolve": false,
        "outFile": "../../.tsproject/output.js",
        "target": "es5"
    },
    "bundles": {
        "main": {
            "files": [
                "Class/main.ts"
            ],
            "config": {
                "outDir": "../"
            }
        }
    }
}

And the error message:
[TsProject] Compiling bundle...
[TsProject] Scripts/Framework/Class/Main.ts(6,18): error TS2300: Duplicate identifier 'Main'.

(6,18) basically refers to export class Main {

There are some other errors due to relative dependencies which are related to #30:
[TsProject] Scripts/main.ts(1,1): error TS6053: File Dep/Dep1.ts
[TsProject] Scripts/main.ts(2,1): error TS6053: File Dep/Dep2.ts

where the reference paths are:
/// <reference path="../Dep/Dep1.ts" />
/// <reference path="../Dep/Dep2.ts" />

Here's the folder hierarchy in one place:
<Project Root>\Scripts\Framework\tsproject.json
<Project Root>\Scripts\Framework\Class\Main.ts
<Project Root>\Scripts\Framework\Dep\*

What I find interesting is that even when considering the base path (Scripts/Framework), TsProject is referring to the incorrect path for Main.ts.

Maybe I am not using TsProject for the correct reason. My goal is two parts - 1) to be able to resolve dependencies and output JS in the correct order and 2) split the TS files into separate bundles.

@mykohsu I haven't looked into your additional information yet, but I have fixed a bug in the 1.2.0-rc.x releases that could cause a duplicate identifier in the bundle compilation. I will be releasing the next rc update as soon as the minification feature has been tested to my satisfaction ( hopefully this week ).
I will look at your additional information tomorrow. Thanks!

@mykohsu Would you please provide simple implementations (source) for main.ts, dep1.ts, dep2.ts. I would like to see your import statements and what the dependencies are between the 3 source files. Just keep it really simple!

I can tell you that TsProject builds a dependency tree for all the project source files. Since your tsconfig.json does not have a "files" property, all files in your source tree are included. When you specify the bundle "files": ["./class/main.ts"], what you are doing, is telling TsProject to bundle all the dependencies that main.ts has within the set of the project files and construct a single bundled output ts file ( in your case main.ts, main.js and main.d.ts ).

Notes:

  1. TsProject streams it's output as vinyl files to be consumed by the gulp build pipeline
  2. You shouldn't need to use /// references if you use a tsconfig.json project file.

@ToddThomson I actually get the same duplicate identifier without any /// references. Because we are using internal modules, /// references is needed for the TsCompiler otherwise in single file output the order of generated code is based on file hierarchy.

[TsProject] Scripts/Framework/Class/Main.ts(3,18): error TS2300: Duplicate identifier 'Main'.
[TsProject] Scripts/Framework/main2.ts(3,18): error TS2300: Duplicate identifier 'Main'.

It looks like TsProject is creating a new file main2.ts (using bundle name) with the same contents as Class/Main.ts and compiling main2.ts with Class/Main.ts as a dependency. I've been trying to grab the main2.ts file during compilation but it errors out and the file is deleted too quickly.

I can provide a skeleton of the Main class, but you will see that there is really nothing in it:

/// reference paths here
module someNamespace {
    export class Main {
        static Controls = {
            KEY1 = "some string1"
            KEY2 = "some string2";
        };
        static Initialize(): void {
            // Code that uses some dependency here
        }
    }
}

document.onreadystatechange = () => {
    if (document.readyState === "complete") {
        someNamespace.Main.Initialize();
    }
}

dependencies that main.ts has within the set of the project files

Does this mean that main.ts should not be part of the source tree? Because in the examples here they are.

This issue will be addressed in release 1.2.1.