Giter VIP home page Giter VIP logo

react-prod-sourcemaps's Introduction

react-prod-sourcemaps

A tool to update app sourcemaps with the original code of ReactDOM's production builds .

Background

React has never shipped sourcemaps for any of its production build artifacts. This makes it impossible to meaningfully debug errors inside of React in production. React's source code is already hard to understand in its original form - trying figure out what's happening when all you have is single-character variable names and no comments is impossible.

I have a PR up at facebook/react#26446 that updates React's build pipeline to generate sourcemaps for production artifacts. If and when that eventually gets merged, future releases of React will include sourcemaps.

However, that doesn't help debug current versions of React.

I've done the work to check out the tagged source code for earlier React versions, rebuilt those versions locally, and verified that the artifacts are byte-for-byte identical. I've then backported the build pipeline changes from my PR onto those older checked-out versions, and built the sourcemaps that would have been generated for each version.

The actual build changes used can be seen here:

Contents

This package includes:

  • the actual sourcemaps
  • logic to search an input sourcemap for specific ReactDOM prod artifacts by content hash and replace them with the "original" pre-minified bundle source via the sourcemaps
  • a CLI tool that will load a given input sourcemap file and rewrite it
  • a build tool plugin that will automatically replace react-dom sourcemaps

(This is my first attempt at writing a Node CLI tool. It seems to run, but there's a good chance I got something wrong - let me know!)

React Versions

This package currently includes sourcemaps for:

  • ReactDOM
    • 18.2.0
    • 18.1.0
    • 17.0.2

I plan to also include ReactDOM 16.14.0 and 16.13.1, which will cover the majority of current React version downloads per NPM stats.

CLI Usage

yarn add @acemarke/react-prod-sourcemaps
./node_modules/.bin/react-prod-sourcemaps --inputFile path/to/your/appBuild/sourcemap.js.map
# Output file will currently be written to sourcemap.remapped.js.map

Build plugin usage

The build plugin is built using unplugin, meaning we currently supports webpack, esbuild, rollup, vite and rspack (experimental).

The plugin supports the following options:

key value required default recommended functionality
debug boolean no false false enables debug logging
preserve boolean no false false preserves original sourcemaps and outputs remapped sourcemaps under path/to/output/sourcemap/[name].js.remapped.map
mode "strict" no undefined "strict" causes the build plugin to throw an error if no sourcemap files are generated by the build tool

Warning: if sourcemap generation is not enabled by your build tool (or if it is not setup correctly), the plugin will silently fail and not perform any sourcemap remapping. We recommend setting using mode: "strict" in case you want the plugin to error in that case.

Webpack:

import { WebpackReactSourcemapsPlugin } from "@acemarke/react-prod-sourcemaps";

module.exports = {
  // ...webpack config
  devtool: "source-map", // or any other option that generates separate .map.js files
  plugins: [WebpackReactSourcemapsPlugin({ debug: false, preserve: false })],
};

esbuild:

import { EsbuildReactSourcemapsPlugin } from "@acemarke/react-prod-sourcemaps";

esbuild.build({
  // ...esbuild config
  sourcemap: true, // or any other option that generates separate .map.js files
  plugins: [EsbuildReactSourcemapsPlugin({ debug: false, preserve: false })],
});

Rollup:

import { RollupReactSourcemapsPlugin } from "@acemarke/react-prod-sourcemaps";

rollup({
  // ...rollup config
  output: {
    sourcemap: true, // or any other option that generates separate .map.js files
  },
  plugins: [RollupReactSourcemapsPlugin({ debug: false, preserve: false })],
});

Vite:

import { ViteReactSourcemapsPlugin } from "@acemarke/react-prod-sourcemaps";

vite.build({
  // ...vite config
  build: {
    sourcemap: true, // or any other option that generates separate .map.js files
  },
  plugins: [ViteReactSourcemapsPlugin({ debug: false, preserve: false })],
});

Future Plans

  • Add sourcemaps for more React versions
  • Add more options to the CLI tool:
    • Glob sourcemaps in a folder
    • Overwrite original sourcemap paths

react-prod-sourcemaps's People

Contributors

jasikpark avatar jonasba avatar markerikson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jonasba jasikpark

react-prod-sourcemaps's Issues

feat: sourcemaps for react-dom/profiling

We currently do not provide sourcemaps for react-dom/profiling bundle. It would be useful if we could provide these as they might be used by a lot of folks + the current remapping behavior is slightly confusing. e.g. I only noticed this after using strict mode in our webpack build step at Sentry and have it throw and error before I noticed a webpack resolve alias for react-dom to react-dom/profiling...

@markerikson iirc you mentioned we could add these somewhat trivially? I would be happy to look into building these and checking them into the repo, wdyt?

Add `x_google_ignoreList` (Ignore-listing code) support to sourcemaps

Introduce the x_google_ignoreList extension in the sourcemaps generated by this project. This will facilitate a more streamlined debugging experience in Chrome (and other supporting browsers) by automatically filtering out framework and dependency code.

Benefit

Implementing x_google_ignoreList in the sourcemaps will align this project with modern web development practices, offering a more focused and efficient debugging experience for developers using Chrome DevTools. This change will particularly benefit those who regularly engage in debugging complex applications with numerous dependencies.

Background Context

See Also

Tangentially related issues for improving modern debugging support:

[ERR_REQUIRE_ESM]: require() of ES Module when using CJS

Seems that we have an error originating from yargs when importing the package in CJS

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/runner/work/sentry/sentry/node_modules/string-width/index.js from /home/runner/work/sentry/sentry/node_modules/jest-cli/node_modules/cliui/build/index.cjs not supported. 
Instead change the require of index.js in /home/runner/work/sentry/sentry/node_modules/jest-cli/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules.     

at Object.<anonymous> (/home/runner/work/sentry/sentry/node_modules/jest-cli/node_modules/cliui/build/index.cjs:291:21)    
at Object.<anonymous> (/home/runner/work/sentry/sentry/node_modules/jest-cli/node_modules/yargs/build/index.cjs:1:59901)    
at Object.<anonymous> (/home/runner/work/sentry/sentry/node_modules/jest-cli/node_modules/yargs/index.cjs:5:30)     
at _yargs (/home/runner/work/sentry/sentry/node_modules/jest-cli/build/run.js:30:39)     
at buildArgv (/home/runner/work/sentry/sentry/node_modules/jest-cli/build/run.js:143:26)     
at Object.run (/home/runner/work/sentry/sentry/node_modules/jest-cli/build/run.js:118:24)     
at Object.<anonymous> (/home/runner/work/sentry/sentry/scripts/test.js:27:6)

Bundling doesn't seem to be right

When trying to integrate the Vite plugin, I get this error:

webclient on  update-react-sourcemaps-tool [!] via  v18.16.0 
❯ pnpm build

> [email protected] build /Users/calebjasik/Git/defined.net/webclient
> vite build && pnpm post-build

failed to load config from /Users/calebjasik/Git/defined.net/webclient/vite.config.ts
error during build:
file:///Users/calebjasik/Git/defined.net/webclient/vite.config.ts.timestamp-1694618874841-ea67fb622aaf5.mjs:3
import { ViteReactSourcemapsPlugin } from "file:///Users/calebjasik/Git/defined.net/webclient/node_modules/.pnpm/@[email protected]_patch_hash=jpyptpvw6t3tnchzyqe4intg7m/node_modules/@acemarke/react-prod-sourcemaps/lib/index.js";
         ^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'ViteReactSourcemapsPlugin' not found. The requested module 'file:///Users/calebjasik/Git/defined.net/webclient/node_modules/.pnpm/@[email protected]_patch_hash=jpyptpvw6t3tnchzyqe4intg7m/node_modules/@acemarke/react-prod-sourcemaps/lib/index.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'file:///Users/calebjasik/Git/defined.net/webclient/node_modules/.pnpm/@[email protected]_patch_hash=jpyptpvw6t3tnchzyqe4intg7m/node_modules/@acemarke/react-prod-sourcemaps/lib/index.js';
const { ViteReactSourcemapsPlugin } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)
 ELIFECYCLE  Command failed with exit code 1.

webclient on  update-react-sourcemaps-tool [!] via  v18.16.0 
❯ 

which matches the errors described in https://arethetypeswrong.github.io/?p=%40acemarke%2Freact-prod-sourcemaps%400.0.5

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.