TypeStrong / dts-bundle

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

Houston, we have a problem #32

Closed iby closed 8 years ago

iby commented 8 years ago

Trying to compile a bundle and either I'm doing something wrong or something wrong with dts-bundle. It all gets stuck on the last line with CPU through the roof.

../../dependency/typings/node/node (/Users/ianbytchek/Development/ianbytchek/guild/dependency/typings/node/node.d.ts)
 - declare buffer
 - declare querystring
 - declare events
 - declare http
 - import external events
 - import external net
 - import external stream
 - declare cluster
 - import external child_process
 - import external events
 - declare zlib
 - import external stream
 - declare os

I assume that I don't need anything from the typings that I installed with tsd command, my config looks like this:

{
    baseDir: '../product/js',
    exclude: /typings/,
    externals: false,
    main: '../product/js/guild.d.ts',
    name: 'tst',
    referenceExternals: false,
    verbose: true
}

If I understand it right, externals should not include anything from the baseDir, and referenceExternals should not include /// <reference path="…"/> stuff. Both fail?

// guild.d.ts aka main

/// <reference path="../../dependency/typings/reference.d.ts" />
/// <reference path="../../source/dts/reference.d.ts" />
export { PathConfiguration } from './Configuration/PathConfiguration';
import { GuildConfiguration } from './Configuration/GuildConfiguration';
import { GulpHelp } from 'gulp-help';
export declare function guild(gulp: GulpHelp, configuration: GuildConfiguration): void;
tolemac commented 8 years ago

I have to confirm it but I think '../*' and '. /' aren't externals. If you use relative path then it isn't external, I think this is the rule.

iby commented 8 years ago

The documentation says on externals:

include typings outside of the 'baseDir' (i.e. like node.d.ts)

Also forgot to mention, that I've tried using exclude pattern, which also failed in all variations. And maybe it's worth checking why the typings/node/node itself fails – it feels there's a recursion.

tolemac commented 8 years ago

Yes, however the code is:

          // import() statement or es6 import
            if ((match = line.match(importExp) || line.match(importEs6Exp))) {
                const [_, lead, quote, moduleName, trail] = match;
                assert(moduleName);

                const impPath = path.resolve(path.dirname(file), moduleName);

                // filename (i.e. starts with a dot, slash or windows drive letter)
                if (fileExp.test(moduleName)) {
                    // TODO: some module replacing is handled here, whereas the rest is
                    // done in the "rewrite global external modules" step. It may be
                    // more clear to do all of it in that step.
                    let modLine: ModLine = {
                        original: lead + quote + getExpName(impPath) + trail
                    };
                    res.lines.push(modLine);

                    const full = path.resolve(path.dirname(file), impPath + '.d.ts');
                    trace(' - import relative %s (%s)', moduleName, full);

                    pushUnique(res.relativeImports, full);
                    res.importLineRef.push(modLine);
                }
                // identifier
                else {
                    let modLine: ModLine = {
                        original: line
                    };
                    res.lines.push(modLine);
                    trace(' - import external %s', moduleName);

                    pushUnique(res.externalImports, moduleName);
                    if (externals) {
                        res.importLineRef.push(modLine);
                    }
                }
            }

Anyway I will test it.

PS: do you use 0.4.1 version?

iby commented 8 years ago

Yes, 0.4.1.

tolemac commented 8 years ago

I have created a github repository to test dts-bundle with examples from issues.

I have tried to reproduce your env and this is the result: https://github.com/tolemac/dts-test-cases/blob/master/ianbytchek/code-tree/product/js/tst.d.ts

### settings object passed ###
{ baseDir: 'code-tree/product/js',
  exclude: /typings/,
  externals: false,
  main: 'code-tree/product/js/guild.d.ts',
  name: 'tst',
  referenceExternals: false,
  verbose: true }
### settings ###
main:         code-tree/product/js/guild.d.ts
name:         tst
out:          tst.d.ts
baseDir:      c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js
mainFile:     c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\guild.d.ts
outFile:      c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\tst.d.ts
externals:    no
exclude:      /typings/
removeSource: no
comments:     no
emitOnIncludedFileNotFound:   no
emitOnNoIncludedFileNotFound: no

### find typings ###
source typings (will be included in output if actually used)
 - c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\GuildConfiguration.d.ts 
 - c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\PathConfiguration.d.ts 
 - c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\guild.d.ts 
 - c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\tst.d.ts 
excluded typings (will always be excluded from output)

### parse files ###
guild (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\guild.d.ts)
 - reference external typing ../../dependency/typings/reference.d.ts (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\dependency\typings\reference.d.ts) (relative: ../../dependency/typings/reference.d.ts)
 - reference external typing ../../source/dts/reference.d.ts (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\source\dts\reference.d.ts) (relative: ../../source/dts/reference.d.ts)
 - import relative ./Configuration/PathConfiguration (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\PathConfiguration.d.ts)
 - import relative ./Configuration/GuildConfiguration (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\GuildConfiguration.d.ts)
 - import external gulp-help
..\..\dependency\typings\reference (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\dependency\typings\reference.d.ts)
..\..\source\dts\reference (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\source\dts\reference.d.ts)
Configuration\PathConfiguration (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\PathConfiguration.d.ts)
Configuration\GuildConfiguration (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\GuildConfiguration.d.ts)

### map exports ###

### determine typings to include ###
queue
[ { file: 'c:\\Users\\jros\\Desarrollo\\GitHub\\tolemaC\\dts-test-cases\\ianbytchek\\code-tree\\product\\js\\guild.d.ts',
    name: 'guild',
    indent: '    ',
    exp: 'tst',
    refs: 
     [ 'c:\\Users\\jros\\Desarrollo\\GitHub\\tolemaC\\dts-test-cases\\ianbytchek\\code-tree\\dependency\\typings\\reference.d.ts',
       'c:\\Users\\jros\\Desarrollo\\GitHub\\tolemaC\\dts-test-cases\\ianbytchek\\code-tree\\source\\dts\\reference.d.ts' ],
    externalImports: [ 'gulp-help' ],
    relativeImports: 
     [ 'c:\\Users\\jros\\Desarrollo\\GitHub\\tolemaC\\dts-test-cases\\ianbytchek\\code-tree\\product\\js\\Configuration\\PathConfiguration.d.ts',
       'c:\\Users\\jros\\Desarrollo\\GitHub\\tolemaC\\dts-test-cases\\ianbytchek\\code-tree\\product\\js\\Configuration\\GuildConfiguration.d.ts' ],
    exports: [],
    lines: [ [Object], [Object], [Object], [Object], [Object] ],
    fileExists: true,
    importLineRef: [ [Object], [Object] ],
    relativeRef: [] } ]
guild (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\guild.d.ts)
 - exclude external gulp-help
 - import relative c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\PathConfiguration.d.ts
 - import relative c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\GuildConfiguration.d.ts
Configuration\PathConfiguration (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\PathConfiguration.d.ts)
Configuration\GuildConfiguration (c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\GuildConfiguration.d.ts)

### rewrite global external modules ###
guild
 - export { PathConfiguration } from 'tst/Configuration/PathConfiguration';  ==>  export { PathConfiguration } from 'tst/Configuration/PathConfiguration';
 - import { GuildConfiguration } from 'tst/Configuration/GuildConfiguration';  ==>  import { GuildConfiguration } from 'tst/Configuration/GuildConfiguration';
Configuration\PathConfiguration
Configuration\GuildConfiguration

### build output ###
## files not found ##

### write output ###
c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\tst.d.ts

### statistics ###
used sourceTypings
 - c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\GuildConfiguration.d.ts
 - c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\Configuration\PathConfiguration.d.ts
 - c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\guild.d.ts
unused sourceTypings
 - c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\product\js\tst.d.ts
excludedTypings
used external typings
unused external typings
 - c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\dependency\typings\reference.d.ts
 - c:\Users\jros\Desarrollo\GitHub\tolemaC\dts-test-cases\ianbytchek\code-tree\source\dts\reference.d.ts
external dependencies
 - gulp-help

### done ###
tolemac commented 8 years ago

I have found your repository (guild), I have download it and I'm testing. Seems like a infinite loop, ... continue testing ;) Stay tuned ;)

tolemac commented 8 years ago

Seems that there is a problem matching this regular expression /^([ \t]*(?:export|import) ?(?:(?:\* (?:as [^ ,]+)?)|.*)?,? ?(?:[^ ,]+ ?,?)(?:\{(?:[^ ,]+ ?,?)*\})? ?from )(['"])([^ ,]+)(\2;.*)$/

with this text: export function networkInterfaces(): {[index: string]: NetworkInterfaceInfo[]};

regex101.com says:

Catastrophic backtracking
Catastrophic backtracking has been detected and the execution of your expression has been halted. To find out more what this is, please read the following article: Runaway Regular Expressions.

I recommend you launch the debugger in the menu to the left and analyze the data to find out the cause.

Seems that exists problems running the regular expression ... it's the first time that its occurs me.

In addition, this happens when parse node.d.ts file, and I think this file don't have to be processed. dts-bundle parse all files recursivelly and decide to exclude or include after parse all files. I think it shouldn't parse the exclude files. I work on it, testing and I will make a decision.

iby commented 8 years ago

Thanks! That's the one I'm trying to convert into a pretty ts npm package :blush:

tolemac commented 8 years ago

@ianbytchek, if you need to bundle it, you can change the name of 'node.d.ts' in order to 'dts-bundle' doesn't find it. You can see 'files not found' section of the readme file to emit the result when there are files not found.

tolemac commented 8 years ago

0.4.3 version solve your problem. Thanks ;)

iby commented 8 years ago

seinfelddance