Skip to content

Cli build should support loading modules with *.tsx extension. #8046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
majo44 opened this issue Oct 13, 2017 · 37 comments
Closed

Cli build should support loading modules with *.tsx extension. #8046

majo44 opened this issue Oct 13, 2017 · 37 comments
Labels
feature Issue that requests a new feature P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@majo44
Copy link
Contributor

majo44 commented Oct 13, 2017

Bug Report or Feature Request (mark with an x)

- [x] bug report
- [ ] feature request

Versions.

Angular CLI: 1.5.0-rc.0
Node: 6.11.0
OS: win32 x64
Angular: 5.0.0-rc.2
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router

@angular/cli: 1.5.0-rc.0
@angular-devkit/build-optimizer: 0.0.27
@angular-devkit/core: 0.0.20
@angular-devkit/schematics: 0.0.33
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.0-rc.0
@schematics/angular: 0.0.45
typescript: 2.5.3
webpack: 3.7.1

Repro steps.

  1. call ng new testproject
  2. cd testproject
  3. rename src\app\app.component.ts to src\app\app.component.tsx
  4. add to tsconfig.json compilerOptions
    "jsx": "react",
    "jsxFactory": "h"
  5. call ng build

The log given by the failure.

ERROR in ./src/app/app.module.ts
Module not found: Error: Can't resolve './app.component' in 'c:\temp\testproject\src\app'
resolve './app.component' in 'c:\temp\testproject\src\app'
  using description file: c:\temp\testproject\package.json (relative path: ./src/app)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: c:\temp\testproject\package.json (relative path: ./src/app)
    using description file: c:\temp\testproject\package.json (relative path: ./src/app/app.component)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        c:\temp\testproject\src\app\app.component doesn't exist
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        c:\temp\testproject\src\app\app.component.ts doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        c:\temp\testproject\src\app\app.component.js doesn't exist
      as directory
        c:\temp\testproject\src\app\app.component doesn't exist
[c:\temp\testproject\src\app\app.component]
[c:\temp\testproject\src\app\app.component.ts]
[c:\temp\testproject\src\app\app.component.js]
[c:\temp\testproject\src\app\app.component]
 @ ./src/app/app.module.ts 9:0-47
 @ ./src/main.ts
 @ multi ./src/main.ts

Desired functionality.

The tsx is providing just extended syntax sugar, which can be used for different porpoises. In my case I'm using mono-repo approach and I'm delivering part of components as web-components where I'm using jsx/tsx.
For compiler there should not be difference in extension of module.

Ropen of angular/angular#19691 (comment)

@filipesilva filipesilva added type: discussion feature Issue that requests a new feature labels Oct 16, 2017
@filipesilva
Copy link
Contributor

filipesilva commented Oct 16, 2017

Discussed offline, we should support .tsx. Will make as a feature request to be worked on.

@filipesilva filipesilva assigned filipesilva and unassigned hansl Oct 16, 2017
@filipesilva filipesilva added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent and removed type: discussion labels Oct 16, 2017
@majo44
Copy link
Contributor Author

majo44 commented Oct 30, 2017

Any chance to got this in 1.5.0 ?

majo44 added a commit to majo44/angular-cli that referenced this issue Nov 2, 2017
majo44 added a commit to majo44/angular-cli that referenced this issue Nov 2, 2017
@rsxdalv
Copy link

rsxdalv commented Nov 22, 2017

What work is left to pull this PR?

wholysz added a commit to wholysz/angular-cli that referenced this issue Nov 24, 2017
Add support for tsx syntax in component files
Closes angular#8046
wholysz added a commit to wholysz/angular-cli that referenced this issue Nov 24, 2017
Add support for tsx syntax in component files
Closes angular#8046
@mcharmas
Copy link

Is there any workaround this issue?

@majo44
Copy link
Contributor Author

majo44 commented Nov 24, 2017

UGLY workaround: https://gist.github.com/majo44/4862b8e7db6254975669e0a3f9d67a2a#file-angular-cli-1-5-0-tsx-loading-ugly-fix (we have this in postinstall)

We are working on PR.

@mcharmas
Copy link

@majo44 thx, I guess it's better solution then ejecting to webpack

@urish
Copy link

urish commented Feb 8, 2018

@majo44 any news regarding the PR ?

@majo44
Copy link
Contributor Author

majo44 commented Feb 9, 2018

@urish unfortunately we are not actively working on PR, so far we stick with workaround and old version of cli :(

@urish
Copy link

urish commented Feb 9, 2018 via email

@psamusev
Copy link
Contributor

@urish It has stopped work with @angular/cli >= 1.7.0

@lygstate
Copy link

lygstate commented Apr 1, 2018

@majo44 PR thanks.

@lygstate
Copy link

lygstate commented Apr 1, 2018

@psamusev I am working fine with 1.7.3, so what's the problem you faced?

@psamusev
Copy link
Contributor

psamusev commented Apr 2, 2018

@lygstate I'm talking about live reload with help of ng serve - server doesn't see changes and doesn't execute reload

@rsxdalv
Copy link

rsxdalv commented Apr 2, 2018

@psamusev It sees them one level deep (i.e myfile.ts ... import .. myotherfile.tsx ... myotherfile.ts ... import .. mythirdfile.tsx ..., only first and second will be seen)

@psamusev
Copy link
Contributor

psamusev commented Apr 2, 2018

@rsxdalv I expect it shouldn't work in this way :)

@rsxdalv
Copy link

rsxdalv commented Apr 2, 2018

@psamusev Eyes open for your PR.

@psamusev
Copy link
Contributor

psamusev commented Apr 13, 2018

workaround is to make import with .tsx at the end if import file name

import 'myTsxfile.tsx'

fredzqm pushed a commit to fredzqm/fredzqm that referenced this issue Apr 27, 2018
@sakalys
Copy link

sakalys commented May 3, 2018

@psamusev this does not work. Using your suggestion it errors out:

You may need an appropriate loader to handle this file type.

@psamusev
Copy link
Contributor

psamusev commented May 3, 2018

@sakalys you have to import only with string path. I mean

import 'myImportfile.tsx

works, however

import { SomeComponent } from 'myImportfile.tsx'

will not work

@sakalys
Copy link

sakalys commented May 4, 2018

Oh, thank you, sir. I'll definitely check.

@zh99998
Copy link

zh99998 commented May 12, 2018

@angular/cli 6.0.1 still can't use and the ugly fix not works any more

@rsxdalv
Copy link

rsxdalv commented May 12, 2018

@zh99998 Take advantage of versioning: downgrade. Very few updates have been worth their salt beyond reducing future upgrade pains. Later, you might be able to revert back to 6.0.1 when the fix is updated.

@urish
Copy link

urish commented May 19, 2018

Just sent a PR to make it work with the latest CLI (6.x). I'm using this in one of my projects successfully. Fingers crossed! 🤞

@psamusev
Copy link
Contributor

psamusev commented Jun 29, 2018

@urish looks like your fix doesn't work when build is for server module (I create universal product)
I'm using:
"@angular-devkit/build-angular": "~0.7.0-beta.2", "@angular/cli": "~6.1.0-rc.0",

Error is following
Module parse failed: Unexpected token You may need an appropriate loader to handle this file type.

@rhalff
Copy link

rhalff commented Jul 2, 2018

I'm currently solving this issue in an extra build step for projects containing jsx:

import * as ngPackage from 'ng-packagr'

ngPackage
  .ngPackagr()
  .forProject(
     'projects/<angular-lib-project>'
  )
  .withTsConfig(
    'projects/<angular-lib-project>/tsconfig.lib.json'
  )
  .build()
  .catch(error => {
    console.error(error)
    process.exit(1)
  })

The file layout of the library can remain as is in this case.

You'll also have to add "jsx": true in your ng-package.json and set "jsx": "react|react-native|preserve" inside the typescript configuration file.

@ar53n
Copy link

ar53n commented Jul 2, 2018

How i can test *.spec.tsx files? Have anyone that problem?

@lacolaco
Copy link
Contributor

lacolaco commented Jul 24, 2018

I'm using ngx-build-plus to compile .tsx files
https://github.com/manfredsteyer/ngx-build-plus/

webpack.extra.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.tsx$/,
        use: 'ts-loader'
      }
    ]
  },
  resolve: {
    extensions: ['.tsx'],
  }
};

I think ngcLoader should handle .tsx file as well as .ts by default

I wonder why this patch didn't fix it...

@davidenke
Copy link

Are there any updates on this? For us *.tsx support is working only on non-aot builds. Running ng serve --aot=true seams not to work.

Do you need a reproduction case?

@psamusev
Copy link
Contributor

@davidenke There is a PR to fix it - #11407

@davidenke
Copy link

@psamusev it's not reviewed yet, maybe because of the failed circle ci tests?

@psamusev
Copy link
Contributor

@davidenke I think so.

@clydin
Copy link
Member

clydin commented Sep 28, 2018

Support has been merged and will be available in 7.0.

@clydin clydin closed this as completed Sep 28, 2018
@jimthedev
Copy link
Contributor

I have been unable to get this working at all. Is it documented anywhere so that I can confirm I am on the right versions of everything?

@ahpoi
Copy link

ahpoi commented Jan 23, 2019

@jimthedev
I have managed to get it working, I had to upgrade to angular 7, and in the tsconfig for compiler option, you need to add `"jsx": "react".

@jimthedev
Copy link
Contributor

@ahpoi This worked for me. It seems to be working pretty well.

@FRSgit
Copy link

FRSgit commented Apr 5, 2019

For future travelers - when using jsx with stenciljs I needed to have these two config options in tsconfig.json:

  "jsx"        : "react",
  "jsxFactory" : "h"

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Issue that requests a new feature P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.