Skip to content
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

exclude node_modules from source map #1685

Closed
sushantdhiman opened this issue Oct 16, 2021 · 4 comments
Closed

exclude node_modules from source map #1685

sushantdhiman opened this issue Oct 16, 2021 · 4 comments

Comments

@sushantdhiman
Copy link

Is there a way to exclude a folder from source maps while keeping files in bundle.

Similar issues

Background:

My source maps are too big even with sourcesContent: false (I only need stack-traces). Big source maps take some time to load with node --enable-source-maps

image

@evanw
Copy link
Owner

evanw commented Oct 16, 2021

Here is a plugin that does what #1227 does but for anything with node_modules in the path:

let plugin = {
  name: 'excludeVendorFromSourceMap',
  setup(build) {
    build.onLoad({ filter: /node_modules/ }, args => {
      return {
        contents: fs.readFileSync(args.path, 'utf8')
          + '\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==',
        loader: 'default',
      }
    })
  },
}

Feel free to customize the plugin further for your needs. You can read more about esbuild plugins here: https://esbuild.github.io/plugins/.

@sushantdhiman
Copy link
Author

Thanks, I have modified it to work only on js files and it works great.

const excludeVendorFromSourceMapPlugin = ({ filter }) => ({
  name: 'excludeVendorFromSourceMap',
  setup(build) {
    build.onLoad({ filter }, (args) => {
      if (args.path.endsWith('.js')) {
        return {
          contents:
            readFileSync(args.path, 'utf8') +
            '\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==',
          loader: 'default',
        };
      }
    });
  },
});

image

If you don't want to add this to esbuild api, you can close this issue

@evanw
Copy link
Owner

evanw commented Oct 16, 2021

Yes, I think this makes sense to do as a plugin instead of as part of esbuild's API. Closing.

@zach-betz-hln
Copy link

zach-betz-hln commented Jul 3, 2024

I came here from this comment.

After enabling the --enable-source-maps flag, some of our node apps doubled in memory usage (measured directly after startup).

Using your plugin to make the source map file smaller did the trick to lower memory usage to expected levels 👍

The file went from 9.8mb to 493.1kb. Thank you.

Modified plugin
const excludeVendorFromSourceMapPlugin = {
  name: 'excludeVendorFromSourceMapPlugin',
  setup(build) {
    const emptySourceMapAsBase64 = Buffer.from(JSON.stringify({ version: 3, sources: [''], mappings: 'A' })).toString(
      'base64'
    );
    build.onLoad({ filter: /node_modules.+\.(js|ts)$/ }, (args) => {
      return {
        contents: `${fs.readFileSync(args.path, 'utf8')}\n//# sourceMappingURL=data:application/json;base64,${emptySourceMapAsBase64}`,
        loader: 'default'
      };
    });
  }
};

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

No branches or pull requests

3 participants