TypeStrong / dts-bundle

Export TypeScript .d.ts files as an external module definition
MIT License
308 stars 57 forks source link

it is not possible to set out relative to gruntfile.js/package.json root #26

Closed pksorensen closed 8 years ago

pksorensen commented 9 years ago

My build outputs typesscript files and declarations to /artifacts/dev/ relative to project root (location of gruntfile.js or package.json). (I am using vs2015 and tsconfig.json).

Here is my grunt task:

dts_bundle: {
            build: {
                options: {
                    name: 'si-portal-framework',
                    main: 'artifacts/dev/index.d.ts',
                   // baseDir: 'artifacts/dev',
                    out: 'dist/si-portal-framework.d.ts',
                    verbose:true
                }
            }
        }

Running "dts_bundle:build" (dts_bundle) task
### settings ###
main:         artifacts/dev/index.d.ts
name:         si-portal-framework
out:          dist/si-portal-framework.d.ts
baseDir:      C:\dev\S-Innovations\S-Innovations.PortalFramework\src\artifacts\dev
mainFile:     C:\dev\S-Innovations\S-Innovations.PortalFramework\src\artifacts\dev\index.d.ts
outFile:      C:\dev\S-Innovations\S-Innovations.PortalFramework\src\artifacts\dev\dist\si-portal-framework.d.ts
externals:    no
exclude:      null
removeSource: no
comments:     no

alternative

dts_bundle: {
            build: {
                options: {
                    name: 'si-portal-framework',
                    main: 'artifacts/dev/index.d.ts',
                   // baseDir: 'artifacts/dev',
                    out: '/dist/si-portal-framework.d.ts',
                    verbose:true
                }
            }
        }

Innovations.PortalFramework\src\Gruntfile.js" dts_bundle:build
Running "dts_bundle:build" (dts_bundle) task
### settings ###
main:         artifacts/dev/index.d.ts
name:         si-portal-framework
out:          /dist/si-portal-framework.d.ts
baseDir:      C:\dev\S-Innovations\S-Innovations.PortalFramework\src\artifacts\dev
mainFile:     C:\dev\S-Innovations\S-Innovations.PortalFramework\src\artifacts\dev\index.d.ts
outFile:      C:\dist\si-portal-framework.d.ts
externals:    no
exclude:      null
removeSource: no
comments:     no

Is this how it was intended to work? I would assume that the out option would be relative to the project root and not the baseDir?

and the second case of using "/" makees it relative to disk entry point.

tolemac commented 8 years ago

I like this aproach.

This would be a breaking change ... to prevent problems with existing uses of dts-bundle I would keep the behaivor when out parameter don't starts with slash ("/"). But I would change the behaivor when out parameter start with slash.

Then, with out: /dist/si-portal-framework.d.ts the final path must be C:\dev\S-Innovations\S-Innovations.PortalFramework\src\dist\si-portal-framework.d.ts and with out: dist/si-portal-framework.d.ts the final path will continue being C:\dev\S-Innovations\S-Innovations.PortalFramework\src\artifacts\dev\dist\si-portal-framework.d.ts.

If anyone says otherwise, I think it would be good.

tolemac commented 8 years ago

Working on it:

    // Calculate out file path (see #26 https://github.com/TypeStrong/dts-bundle/issues/26)
    function calcOutFilePath(out: any, baseDir: any) {
        var result = path.resolve(baseDir, out);
        // if path is absolute and start with "/" and not is a net route ("//") resolve from local
        if (path.isAbsolute(out) 
            && stringStartsWith(out, path.sep) 
            && !stringStartsWith(out, `${path.sep + path.sep}`)) {

            result = path.resolve(".", out.substr(1));
        }    
        return result;
    }

When the path is absolute and start with slash and not is a net route resolve with the current path.

tolemac commented 8 years ago

Fail to pass tests on travis.

I had tested it on windows where local absolute paths starts with "C:" ... but in linux the absolute paths starts with "/" then no way to determinate if you want absolute path or relative to current path.

I have added the possibility of pass out parameter starting with "~", this way:

    // Calculate out file path (see #26 https://github.com/TypeStrong/dts-bundle/issues/26)
    function calcOutFilePath(out: any, baseDir: any) {
        var result = path.resolve(baseDir, out);
        // if path start with ~, out parameter is relative from current dir
        if (out[0] === "~") {
            result = path.resolve(".", out.substr(1));
        }
        return result;
    }

If test passed I will approve it..

tolemac commented 8 years ago

The out parameter have to start with "~/".

Final code:

// Calculate out file path (see #26 https://github.com/TypeStrong/dts-bundle/issues/26)
    function calcOutFilePath(out: any, baseDir: any) {
        var result = path.resolve(baseDir, out);
        // if path start with ~, out parameter is relative from current dir
        if (stringStartsWith(out, "~" + path.sep)) {
            result = path.resolve(".", out.substr(2));
        }
        return result;
    }