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

Require.cache causes errors #4616

Open
m-allanson opened this issue May 16, 2020 · 6 comments
Open

Require.cache causes errors #4616

m-allanson opened this issue May 16, 2020 · 6 comments

Comments

@m-allanson
Copy link
Contributor

m-allanson commented May 16, 2020

🐛 bug report

Parcel does not transform require.cache when bundling, which cause an error when running the bundled file.

🎛 Configuration (.babelrc, package.json, cli command)

// in package.json
"esm": "dist/index.js",
"targets": {
  "esm": {
    "includeNodeModules": true,
    "outputFormat": "esmodule",
    "isLibrary": true
  }
},

The bundle command is parcel build index.js --target esm --no-minify

🤔 Expected Behavior

The bundled output should run without error.

😯 Current Behavior

Running the bundled file will cause the following error:

file:///<path>/parcel-repro/dist/index.js:4
for (const file in require.cache) {
                           ^

ReferenceError: require is not defined
    at file:///<path>/parcel-repro/dist/index.js:4:28
    at ModuleJob.run (internal/modules/esm/module_job.js:110:37)
    at async Loader.import (internal/modules/esm/loader.js:179:24)

💁 Possible Solution

Parcel 1 created a shim for require.cache. See this earlier issue and PR:

In the v2 branch there is a skipped test for the shim.

🔦 Context

I want to package node_modules dependencies into my bundle. Some of these dependencies make use of require.cache.

💻 Code Sample

Repro here: https://github.com/m-allanson/parcel-require-repro

The dist directory is committed too.

Example code that will cause the error:

// in src file index.js
for (const file in require.cache) {
  if (/color/g.test(file)) {
    result[file] = true;
  }
}

🌍 Your Environment

Software Version(s)
Parcel 2.0.0-alpha.3.2
Node 12 or 14 (for ESM support)
npm/Yarn npm
Operating System macOS
@mischnic
Copy link
Member

Needs to be added here:

if (t.matchesPattern(path.node, 'module.id')) {
path.replaceWith(t.stringLiteral(asset.id));
}
if (t.matchesPattern(path.node, 'module.hot')) {
path.replaceWith(t.identifier('null'));
}
if (t.matchesPattern(path.node, 'module.require') && !asset.env.isNode()) {
path.replaceWith(t.identifier('null'));
}
if (t.matchesPattern(path.node, 'module.bundle')) {
path.replaceWith(t.identifier('parcelRequire'));
}

@m-allanson
Copy link
Contributor Author

A quick trial shows this working for me 👍 It reinstates the parcel v1 shim behaviour. Is there somewhere I can add tests?

     if (t.matchesPattern(path.node, 'module.id')) { 
       path.replaceWith(t.stringLiteral(asset.id)); 
     } 
  
     if (t.matchesPattern(path.node, 'module.hot')) { 
       path.replaceWith(t.identifier('null')); 
     } 
  
     if (t.matchesPattern(path.node, 'module.require') && !asset.env.isNode()) { 
       path.replaceWith(t.identifier('null')); 
     } 
  
+    if (t.matchesPattern(path.node, 'require.cache')  && !asset.env.isNode()) {
+      path.replaceWith(t.identifier('{}'));
+    }

     if (t.matchesPattern(path.node, 'module.bundle')) { 
       path.replaceWith(t.identifier('parcelRequire')); 
     } 

@m-allanson
Copy link
Contributor Author

Great, thank you! I aim to open a PR this week.

m-allanson added a commit to m-allanson/parcel that referenced this issue May 18, 2020
@navaru
Copy link
Contributor

navaru commented Jun 26, 2020

This issue should be closed fixed by #4621

@DeMoorJasper
Copy link
Member

@navaru it did not get fixed, the PR has been closed not merged.

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

Successfully merging a pull request may close this issue.

4 participants