Protractor: NPM release package should replace `globals.ts` with `globals.d.ts`

Created on 18 Aug 2016  ·  3Comments  ·  Source: angular/protractor

Bug report

  • Node Version: 6.3.1
  • Protractor Version: 4.0.3
  • Typescript Version: 2.0.0

    Overview

This is specific to typescript projects with source files importing protractor/globals. Specifically, when importing protractor/globals, because the NPM release includes the globals.ts file instead of a corresponding definition file (globals.d.ts) the typescript compiler will compute the consuming typescript project's common source root to the common path between the Protractor global.ts dependency (likely node_modules/protractor) and wherever the consuming project's sources are contained (for example, src/test/**/*.ts). This leads the compiler to output the source files with all parent directories included.

Note that this is purely an issue with where the typescript compiler outputs the compiled files; the compilation happens fine, and protractor is consumed (as awesomely) as expected.

Steps to Reproduce

To better demonstrate the scenario I'm poorly describing, please see this example project.

Proposed Solution

I suspect resolving this here would be a combination of two things:

  1. update the current gulp compilation process to ensure a definition file is created when the globals.ts file is compiled; and
  2. update the Protractor NPM release process (whether manual or automated) to remove the globals.ts file from the NPM release package, leaving only the compiled javascript file and corresponding definition file.

If this is sound, I'd be happy to submit a pull request to handle the first part of the solution. If the release process to NPM still follows the project's release document, I'd also be happy to update that with a step for removing the globals.ts file, although that might be better doctored by someone who actually performs releases.

While I've thought this through and believe the Typescript compiler is behaving as expected, let me know if you disagree since I'm still getting my feet wet with Typescript.

(edit: updating format to reflect bug template)

Most helpful comment

@cnishina glad the example helped! Thanks for the quick response, this looks great except for one thing I might be confused about: while your change removes the global.ts file from the NPM package, won't not having a corresponding globals.d.ts declaration make the protractor/globals import invisible to the typescript compiler for consuming projects?

So I guess the first part of my proposal was not to remove the compiled globals.js in favor of the globals.d.ts declaration, but to have both so that NPM package will contain:

  • globals.js
  • globals.d.ts

just without the globals.ts source file.

I believe this can be achieved using tsc with the --declaration flag when compiling the global.ts file. For example, changing the tsc:globals gulp task like below should do the trick, although I noticed there are also tsc and tsc:w tasks in the package.json that might need to be updated as well provided that wouldn't modify non-global compilations adversely:

gulp.task('tsc:globals', function(done) {
  runSpawn(done, 'node', ['node_modules/typescript/bin/tsc', '--declaration', 'globals.ts'],
    'ignore');
});

Let me know if I'm missing something, and thanks again for the quick fix! By the way, using Protractor with Typescript is a serious blast!

All 3 comments

@tmeneau Thank you for this issue: I really liked the github example... 👍 👍 👍

So this is a side affect when using outDir. For example if outDir is set to output, in addition to the transpiled files, output/node_modules/protractor/globals.js would also be transpiled. I originally included the globals.ts file since (at the time) after some experimentation, you could not import a file types without having the TypeScript file.

To go through the proposals:

  1. Does not work since the globals.ts cannot just be a declaration file *d.ts since we are assigning variables from the global namespace.
  2. After experimenting with exampleTypescript this appears to work. I am updating the .npmignore to not publish that file and upgrade Protractor to TypeScript 2. See: https://github.com/angular/protractor/pull/3485

@cnishina glad the example helped! Thanks for the quick response, this looks great except for one thing I might be confused about: while your change removes the global.ts file from the NPM package, won't not having a corresponding globals.d.ts declaration make the protractor/globals import invisible to the typescript compiler for consuming projects?

So I guess the first part of my proposal was not to remove the compiled globals.js in favor of the globals.d.ts declaration, but to have both so that NPM package will contain:

  • globals.js
  • globals.d.ts

just without the globals.ts source file.

I believe this can be achieved using tsc with the --declaration flag when compiling the global.ts file. For example, changing the tsc:globals gulp task like below should do the trick, although I noticed there are also tsc and tsc:w tasks in the package.json that might need to be updated as well provided that wouldn't modify non-global compilations adversely:

gulp.task('tsc:globals', function(done) {
  runSpawn(done, 'node', ['node_modules/typescript/bin/tsc', '--declaration', 'globals.ts'],
    'ignore');
});

Let me know if I'm missing something, and thanks again for the quick fix! By the way, using Protractor with Typescript is a serious blast!

Yup... 👍 globals.d.ts should still be there to get protractor/globals import working. I updated the PR. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rafalf picture rafalf  ·  3Comments

mvolkmann picture mvolkmann  ·  3Comments

davidkarlsen picture davidkarlsen  ·  3Comments

luakri picture luakri  ·  3Comments

gamecheck80 picture gamecheck80  ·  3Comments