Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Enable babel-plugin-transform-react-jsx-source in development for better warning messages #739

Closed
gaearon opened this issue Mar 6, 2017 · 13 comments

Comments

@gaearon
Copy link

gaearon commented Mar 6, 2017

In Create React App, we enable babel-plugin-transform-react-jsx-source in development configuration. I would suggest you to do the same.

It must only be enabled in development (since it produces bigger bundles). When it is enabled, React warnings get component stack traces with exact line numbers.

Example before:

Example after:

Hope you find this useful!

@DanHarman
Copy link
Contributor

DanHarman commented Mar 9, 2017

I don't currently have time to make a pull request for this and get up to speed with how the templates are defined etc, but if anyone wants to add this feature either locally or contribute then this seems to work ok:

npm install --save-dev babel-plugin-transform-react-jsx-source

and modifying the webpack rule for babel-loader as below:

module: {
            rules: [
                { test: /\.tsx?$/, include: /ClientApp/, use: { loader: 'babel-loader', options: { plugins: isDevBuild ? ["transform-react-jsx-source"] : [] } } },
                { test: /\.tsx?$/, include: /ClientApp/, use: 'awesome-typescript-loader?silent=true' }
            ]
        },

@DanHarman
Copy link
Contributor

Addtionally wondering if we shouldn't be setting cacheDirectory: true in the babel options. Theoretically improves compilation times, but I don't know enough about this template to be sure it won't cause probs.

@DanHarman
Copy link
Contributor

DanHarman commented Mar 9, 2017

ok decided to man up, pull request incoming :) The cache does speed up the webpack build by 30-40%.

DanHarman added a commit to DanHarman/JavaScriptServices that referenced this issue Mar 9, 2017
- Added 'babel-plugin-transform-react-jsx-source' to development build
to add line numbers to debug output.
- Added 'cacheDirectory: true' to babel config which improves webpack
build time c. 40%.
@gaearon
Copy link
Author

gaearon commented Mar 9, 2017

We use cacheDirectory: true in babel-loader options in CRA and it works great.

@DanHarman
Copy link
Contributor

That might have been where I got the idea from ;)

@SteveSandersonMS
Copy link
Member

Thanks for the suggestion. Since @DanHarman is implementing this in #749, I'll close this one.

BTW @gaearon thanks for the wonderful work on Redux. I especially like the docs - really easy and fun to read through.

@SteveSandersonMS
Copy link
Member

@gaearon Do you know if there's some way to make babel-plugin-transform-react-jsx-source work with source maps, or some other equivalent Webpack loader that supports compile-to-JS languages such as TypeScript?

From what I can tell, if we include babel-plugin-transform-react-jsx-source as-is in the Webpack config, it does add line numbers to warnings, but they are the wrong line numbers because (presumably) they refer to some intermediate .jsx representation of the source, not the original .tsx files.

@gaearon
Copy link
Author

gaearon commented Mar 13, 2017

Oh, I see. It uses information as provided by Babel. I don’t know about how TS and Babel interaction is set up so I can’t really answer that.

@SteveSandersonMS
Copy link
Member

OK, thanks for the info! I'll see whether anyone else knows if there's a way to get the original line numbers to flow through somehow. If not, I guess we'd have to just not add this change.

@gaearon
Copy link
Author

gaearon commented Mar 13, 2017

Maybe @hzoo could say whether there’s a any way to tell Babel to infer the original location information from a TS sourcemap (or something like this) or not.

@hzoo
Copy link

hzoo commented Mar 14, 2017

I don't think we do anything special with sourcemaps other than use the source-map package.?

The location that is referenced ^ is just from the AST passed into Babel that is reading the JSX. Just my thoughts, but I don't think it has to do with sourcemaps because the Babel plugin runs on the source code itself. I believe we'd need to support TS in babylon for this feature

@loganfsmyth
Copy link

We'd either have to have Babylon take the input sourcemap into account, or we'd have parse and then iterate over the AST afterward in babel-core to adjust the positions. I don't think there's anything technically blocking that, but we do not do that right now.

@SteveSandersonMS
Copy link
Member

Thanks for the info! If this gets implemented one day, we will change the templates to take advantage of it. In the meantime we'll leave things as-is.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants