-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Question: Building react with sourcemaps #14361
Comments
Per discussion in Reactiflux, this shouldn't require any special builds of React. The app build setup is generally responsible for generating sourcemaps of any libraries that are being used at build time. I just tried starting up a CRA app in dev mode, and I can both see the original-ish source of |
What is the real issue here |
To add more info to the question when adding a breakpoint here I expected to step through the original source here |
I was just curious to step through code and see how it works. |
I don't know how to do it but you should be able to step through the code in development mode without sourcemaps. |
Hi @gaearon - With today's React you can't do what the OP is asking, which is to be able to set breakpoints in and step through the original React source (not the transpiled code) when debugging a React app using a debugger like Chrome DevTools or VS Code. You can step-through/set-breakpoints-in the transpiled source which is less clear than the original source. How "less clear" it is depends on the amount of transpilation. Many other libraries that make heavy use of ES6+ features (esp. async/await) are almost unreadable when transpiled to ES5. React doesn't seem to use much ES6 so it's transpiled code is more readable, but it's still less developer-friendly than doing what many other libraries do which is:
If React starts using more ES6+ features, I'd strongly recommend adding sourcemaps and shipping original source to npm. |
https://github.com/rollup/rollup/wiki/Troubleshooting#sourcemap-is-likely-to-be-incorrect |
@tsangint any updates? |
@tsangint 什么时候修复好呢? |
mark |
@tsangint did you ever figure out how to get around this issue? |
Hi Jase! BTW, the URL above has been moved. New URL is this: https://rollupjs.org/guide/en/#warning-sourcemap-is-likely-to-be-incorrect |
Hey @justingrant thanks for the link. I got this working somewhat, I updated Rollup and all of the plugins, set sourcemaps to true and Some I had to comment out though as i don't know how to add mapping support to some of these internal plugins I think I've had to give up for now though, if anyone else was looking into it that's what i've done Comparison: https://github.com/facebook/react/compare/main...jasonwilliams:addSourcMaps?expand=1 Clone it then run |
I was deep diving into this the other week as well.. was writing up notes/observations as I went.. but then got sidetracked and never completed it. Will include the notes I do have in an expandable below in case it helps anyone else: Click to expandIn case it's of use for anyone else that stumbles across this issue: This is where I changed the rollup build script to try and enabled sourcemaps: diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js
index 41b068509..aad155b45 100644
--- a/scripts/rollup/build.js
+++ b/scripts/rollup/build.js
@@ -235,7 +235,9 @@ function getRollupOutputOptions(
freeze: !isProduction,
interop: false,
name: globalName,
- sourcemap: false,
+ // TODO: if we change sourcemap to true here will we get sourcemaps output?
+ // sourcemap: false,
+ sourcemap: true,
esModule: false,
};
} And this is how I attempted to build the code, which resulted in the same error as the OP hit:
The two main sources that come up when googling that are:
Neither of which seemed particularly helpful on their own. Modifying the build script a bit more: diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js
index 41b068509..928c7610d 100644
--- a/scripts/rollup/build.js
+++ b/scripts/rollup/build.js
@@ -159,6 +159,7 @@ function getBabelConfig(
configFile: false,
presets: [],
plugins: [...babelPlugins],
+ sourceMaps: true, // Ref: https://babeljs.io/docs/en/options#sourcemaps
};
if (isDevelopment) {
options.plugins.push(
@@ -235,7 +236,9 @@ function getRollupOutputOptions(
freeze: !isProduction,
interop: false,
name: globalName,
- sourcemap: false,
+ // TODO: if we change sourcemap to true here will we get sourcemaps output?
+ // sourcemap: false,
+ sourcemap: true,
esModule: false,
};
}
@@ -363,7 +366,8 @@ function getPlugins(
bundleType === RN_FB_PROD ||
bundleType === RN_FB_PROFILING;
const shouldStayReadable = isFBWWWBundle || isRNBundle || forcePrettyOutput;
- return [
+
+ const thePlugins = [
// Extract error codes from invariant() messages into a file.
shouldExtractErrors && {
transform(source) {
@@ -371,18 +375,23 @@ function getPlugins(
return source;
},
},
+
// Shim any modules that need forking in this environment.
useForks(forks),
+
// Ensure we don't try to bundle any fbjs modules.
forbidFBJSImports(),
+
// Use Node resolution mechanism.
resolve({
skip: externals,
}),
+
// Remove license headers from individual modules
stripBanner({
exclude: 'node_modules/**/*',
}),
+
// Compile to ES2015.
babel(
getBabelConfig(
@@ -393,12 +402,14 @@ function getPlugins(
!isProduction
)
),
+
// Remove 'use strict' from individual source files.
{
transform(source) {
return source.replace(/['"]use strict["']/g, '');
},
},
+
// Turn __DEV__ and process.env checks into constants.
replace({
__DEV__: isProduction ? 'false' : 'true',
@@ -410,10 +421,12 @@ function getPlugins(
// NOTE: I did not put much thought into how to configure this.
__VARIANT__: bundle.enableNewReconciler === true,
}),
+
// The CommonJS plugin *only* exists to pull "art" into "react-art".
// I'm going to port "art" to ES modules to avoid this problem.
// Please don't enable this for anything else!
isUMDBundle && entry === 'react-art' && commonjs(),
+
// Apply dead code elimination and/or minification.
isProduction &&
closure(
@@ -424,9 +437,11 @@ function getPlugins(
renaming: !shouldStayReadable,
})
),
+
// HACK to work around the fact that Rollup isn't removing unused, pure-module imports.
// Note that this plugin must be called after closure applies DCE.
isProduction && stripUnusedImports(pureExternalModules),
+
// Add the whitespace back if necessary.
shouldStayReadable &&
prettier({
@@ -435,6 +450,7 @@ function getPlugins(
trailingComma: 'none',
bracketSpacing: true,
}),
+
// License and haste headers, top-level `if` blocks.
{
renderChunk(source) {
@@ -447,6 +463,7 @@ function getPlugins(
);
},
},
+
// Record bundle size.
sizes({
getSize: (size, gzip) => {
@@ -465,7 +482,14 @@ function getPlugins(
};
},
}),
- ].filter(Boolean);
+ ]
+
+ const thePluginsFiltered = thePlugins.filter(Boolean);
+
+ console.log(thePlugins);
+ console.log(thePlugins.length);
+
+ return thePluginsFiltered;
}
function shouldSkipBundle(bundle, bundleType) { I get the following output:
Since the array of plugins is filtered to only use the 'truthy' plugins, the 'position 6' referred to in the error corresponds with: // Remove 'use strict' from individual source files.
{
transform(source) {
return source.replace(/['"]use strict["']/g, '');
},
}, If we comment that out then run it again, we get the following error:
This seems to correspond with the following: // License and haste headers, top-level `if` blocks.
{
renderChunk(source) {
return Wrappers.wrapBundle(
source,
bundleType,
globalName,
filename,
moduleType
);
},
}, If we comment that out then run it again, we get the following error:
This seems to correspond with the following: // Apply dead code elimination and/or minification.
isProduction &&
closure(
Object.assign({}, closureOptions, {
// Don't let it create global variables in the browser.
// https://github.com/facebook/react/issues/10909
assume_function_wrapper: !isUMDBundle,
renaming: !shouldStayReadable,
})
), If we look at
|
|
You can give each plugin a name, then instead of saying “position 6” it will say “use strict” for example. In my branch I did this |
@nprasath002 @gaearon @others-who-meet-same-problem I struggled with this question for long time before, finally approached debugging splitted files in a different way: https://github.com/Terry-Su/debug-react-source-code. Hope this helps. |
I haven't looked too deeply into this, so not sure if they are covering the same use cases, but for the "Remove 'use strict' from individual source files" I came across the following when googling:
I just stumbled upon this benchmark repo that includes benchmarks for
|
The error means one of plugins (rollup-plugin-babel) not returns source map. I tried build with source map myself, and then wrote a gist for record: debug react source code. Hope it can you. |
My PR is here if anyone wanted to take a look #21946 |
For anyone still following this issue that isn't also following #20186, that is where the majority of updates RE: sourcemap support are happening now.
|
How do I build react with source maps?
I am in
master
branch with the latest code. I tried setting rollup config tosourcemap: true
and setting"sourceMaps": true
in .babalrc. I am getting the following error.Sourcemap is likely to be incorrect: a plugin was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help
I want to generate source maps so that I can set breakpoints in the actual es6 package code and step through it.
The text was updated successfully, but these errors were encountered: