Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

could you please add example how to run it with @angular/cli #18

Closed
MariMax opened this issue Feb 27, 2018 · 10 comments
Closed

could you please add example how to run it with @angular/cli #18

MariMax opened this issue Feb 27, 2018 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@MariMax
Copy link

MariMax commented Feb 27, 2018

  1. I created new project
    ng new typewiz && cd ./typewiz
  2. ejected angular cli ng eject
  3. added typewiz-webpack: yarn add typewiz-webpack -D
  4. added loader, plugin and middleware
   {
     test: /\.ts$/,
     loaders: ['@ngtools/webpack', 'typewiz-webpack'],
   }
....
   plugins: [
      ...
      new TypewizPlugin(),
    ]
...
  "devServer": {
    "historyApiFallback": true,
    before: function(app) {
      typewizCollectorMiddleware(app, 'collected-types.json');
    }
  }

after build I can see an error in the console:

webpack: Compiled successfully.
/Users/max/projects/typewiz/src/index.html:68
                var xhr = new XMLHttpRequest();
                          ^

ReferenceError: XMLHttpRequest is not defined
    at Timeout._onTimeout (/Users/max/projects/typewiz/src/index.html:68:27)
    at ontimeout (timers.js:460:11)
    at tryOnTimeout (timers.js:298:5)
    at Timer.listOnTimeout (timers.js:261:5)
error An unexpected error occurred: "Command failed.
Exit code: 1

mac/high sierra/node 9.6.1/yarn 1.5.1/angular-cli 1.7.1

@urish urish self-assigned this Feb 27, 2018
@urish
Copy link
Collaborator

urish commented Feb 27, 2018

Thanks for the report @MariMax!

Seems strange that XMLHttpRequest is not defined - it seems like this code is running inside your Webpack process for some reason. Perhaps this happens because of Angular universal?

Any chance you can put the setup that you created in a github repo so I can further look into it?

@MariMax
Copy link
Author

MariMax commented Feb 27, 2018

@urish urish added the bug Something isn't working label Feb 28, 2018
@urish
Copy link
Collaborator

urish commented Feb 28, 2018

Thanks, seems to reproduce here - I added a throw statement to where we define the timer, and it seems to do with the HtmlWebpackPlugin:

ERROR in   Error: C:\P\demos\typewiz-18-example\src\index.html:67
              throw new Error('here');
              ^
  Error: here

  - index.html:67
    C:/P/demos/typewiz-18-example/src/index.html:67:19

  - index.js:231 HtmlWebpackPlugin.evaluateCompilationResult
    [typewiz-18-example]/[html-webpack-plugin]/index.js:231:26

  - index.js:117
    [typewiz-18-example]/[html-webpack-plugin]/index.js:117:21

We probably have to be more selective about adding the reporting code to the generated webpack bundles. I will keep investigating!

urish added a commit that referenced this issue Feb 28, 2018
otherwise, we end up with an
`XMLHttpRequest is not defined` error when HtmlWebpackPlugin is enabled.

should solve #18
@urish
Copy link
Collaborator

urish commented Feb 28, 2018

I fixed this and released as 0.3.2.

It seems like the loader does not add the code to collect the type information on your project, however. I will try to figure out why - I have a feeling it may be related to the way @ngtools/webpack works.

@urish
Copy link
Collaborator

urish commented Mar 3, 2018

Related issue: angular/angular-cli#9559

@urish urish added the help wanted Extra attention is needed label Mar 20, 2018
@krishna217
Copy link

@urish can u add a example how to integrate it with angular-cli.

@urish
Copy link
Collaborator

urish commented May 21, 2018

@krishna217 Angular CLI has a built-in webpack configuration and it does not allow you to make arbitrary changes to that configuration. In addition, the AngularCompilerPlugin does not take any input provided by previous loaders in the chain. Thus, making TypeWiz work with the Angular CLI is very challenging. The situation may improve once #41 is implemented, as we won't need to set up a separate loader just for the instrumentation.

There is a workaround though - you can run the instrumentation process manually, outside of webpack, using the instrument command TypeWiz CLI, and import $_$twiz from typewiz-core/type-collector-snippet somewhere in your code and register it on the global object (i.e. window). Then, you'd have to copy the collected type information - this could be done by running JSON.stringify($_$twiz.get()) in your browser console and copying the result into collected-types.json. Finally, use git to revert the instrumented source files to their original version. This process is quite cumbersome, but it can probably do the trick if you wanted to do just one shot.

@krishna217
Copy link

krishna217 commented May 21, 2018

@urish Thanks for the workaround.

@urish urish removed the help wanted Extra attention is needed label Jun 22, 2018
@urish
Copy link
Collaborator

urish commented Jun 22, 2018

Update: I had some progress on the angular branch. Once this is released, using TypeWiz with Angular should be as simple as:

npm i -D typewiz-webpack typewiz
npm run typewiz angular

@urish
Copy link
Collaborator

urish commented Oct 9, 2018

@urish urish closed this as completed Oct 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants