Skip to content

Better support for disabling sourcemaps of node_modules as plugins can't cover all cases. #3310

Open
@AshHeskes

Description

@AshHeskes

Referencing this issue which discusses disabling sourcemaps for node_modules. @evanw quite kindly provided a code snippet as a plugin that would filter out sourcemaps for node modules by basically appending an inline sourcemap containing nothing to each file matching the filter.

While this snippet does work in the majority of cases where the imported file is a flavour of JS that supports such comments. It doesn't for example work for require('something.json') which is valid in node > 16 (perhaps earlier). If you append the empty inline sourcemap you get an error during bundling that comments are not supported in JSON. Which is also correct.

As a quick example project:

+ app
  - index.js (`require('some-lib')`)
+ node_modules
  + some-lib
    - index.js (`require('./some-json.json')` - valid)
    - some-json.json (`{ my-prop: 'foo' }` - valid )

With an esbuild config of (using the snippet provided by @evanw):

const esbuild = require('esbuild');
const { readFile } =  require('node:fs/promises');

esbuild.build( {
  entryPoints : [ 'src/index.js' ],
  bundle : true,
  outdir : './dist',
  format : 'cjs',
  platform: 'node',
  plugins: [
    {
      name: 'excludeNodeModulesFromSourceMaps',
      setup(build) {
        build.onLoad({ filter: /node_modules/ }, async (args) => {
          return {
            contents: `${await readFile(
              args.path,
              'utf8'
            )}\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==`,
            loader: 'default'
          };
        });
      }
    }
  ]
} );

Will error due to the fact you can't have a comment in json. Which means you have to filter out json imports from excludeNodeModulesFromSourceMaps. Which means node_modules importing json will still be included in sourcemaps.

That might not sound like a big deal and normally I would agree. However this method of excluding sourcing maps via a comment doesn't only apply to json it can apply to anything that doesn't use // as a comment. Like a text file, or a binary but it seems insane to handle on a case by case basis. So I think there needs to be a better mechanism to say don't sourcemap dependencies. Than just appending an inline empty sourcemap comment.

An extreme example but very popular one would be require('aws-sdk') which makes heavy use of require('some-json.json'). There's no way to stop esbuild from creating sourcemaps for all that json which bloats the sourcemap file by several MB from what should be just a few hundred KB. Even with all the actual aws-sdk code excluded via the above plugin.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions