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

Stop indirect call warning for 'require.cache' for CommonJS modules #813

Merged
merged 3 commits into from
Feb 16, 2021

Conversation

huonw
Copy link
Contributor

@huonw huonw commented Feb 15, 2021

This follows/expands 4e503ba (#560) to suppress the warning about an indirect call to 'require' for require.cache, in addition to require.main, when building CommonJS modules. This is because manipulating/reading the cache shouldn't affect the overall bundling (AIUI).

I've validated that this suppresses the warning for the file from #812:

curl 'https://raw.githubusercontent.com/aws/aws-cdk/c6256992902fc4237ceb9f965e970e2c2ef00777/packages/%40aws-cdk/core/lib/private/runtime-info.ts' > runtime-info.ts
./esbuild --format=cjs runtime-info.ts

Output before:

 > runtime-info.ts: warning: Indirect calls to "require" will not be bundled (surround with a try/catch to silence this warning)
    16 │   for (const fileName of Object.keys(require.cache)) {
       ╵                                      ~~~~~~~

 > runtime-info.ts: warning: Indirect calls to "require" will not be bundled (surround with a try/catch to silence this warning)
    69 │   const mod = require.cache[fileName];
       ╵               ~~~~~~~

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getProtoOf = Object.getPrototypeOf;
...

Output after:

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getProtoOf = Object.getPrototypeOf;
...

(I'm not sure if I've updated the CHANGELOG correctly.)

Fixes #812

@evanw evanw merged commit d9cd6c1 into evanw:master Feb 16, 2021
@huonw huonw deleted the allow-cjs-require.cache branch February 16, 2021 07:17
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
// These shouldn't warn
Copy link

@UncleBill UncleBill Feb 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-				// These shouldn't warn
+				// These should warn

This should be a typo?

evanw added a commit that referenced this pull request Feb 17, 2021
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

Successfully merging this pull request may close these issues.

Warnings for use of require.cache
3 participants