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

Replace module.require in scope hoisting #2875

Merged
merged 3 commits into from May 8, 2019
Merged

Conversation

mischnic
Copy link
Member

@mischnic mischnic commented Apr 1, 2019

↪️ Pull Request

When scope hoisting, the check if (typeof module !== "undefined" && module.require) to determine whether the script is running on Node creates a non-working bundle, because typeof module is replaced by a string and this if check also fails:

if (
path.node.name === 'module' &&
(!path.parentPath.isMemberExpression() || path.parent.computed) &&
!(
path.parentPath.isUnaryExpression() &&
path.parent.operator === 'typeof'
) &&
!path.scope.hasBinding('module') &&
!path.scope.getData('shouldWrap')
) {

This causes the asset to not be wrapped and so module isn't being declared (causing Uncaught ReferenceError: module is not defined)

This replace module.require with null in the browser (as is done for server other module properties).

Closes #2841

🚨 Test instructions

Not sure how to add this as a test, because module is actually being declared as a global by run test function.

//index.js
const {getEnv} = require("./env.js");
console.log(getEnv());

//env.js
exports.getEnv = function () {
	if (typeof module !== "undefined" && module.require) {
		return "Node";
	} else {
		return "Browser";
	}
};

REPL Link (Open console to see the error as well)

✔️ PR Todo

  • Added/updated unit tests for this change
  • Filled out test instructions (In case there aren't any unit tests)
  • Included links to related issues/PRs

@mischnic
Copy link
Member Author

mischnic commented Apr 1, 2019

This replace module.require with null in the browser

I'm not even sure if that's what we want to do. The "normal" require was already parsed by the hoister

Is if (typeof module !== "undefined" && module.require) the "canonical" way to check for Node?

module.require(...) might not work with this because if during hoisting the require wasn't possible to parse, then this would produce an error null(...)

@devongovett
Copy link
Member

Could you add a test?

@devongovett
Copy link
Member

module.require appears not to be implemented by webpack, so I'd say replacing it with null is fine.

@devongovett devongovett merged commit a50ddd5 into master May 8, 2019
@devongovett devongovett deleted the hoist-module-require branch May 8, 2019 04:22
@mischnic
Copy link
Member Author

mischnic commented May 8, 2019

@devongovett You did what I mentioned in my last comment:
adding a test as you did passes even without the changes to hoist.js because module is actually defined in the test environment:

var ctx = Object.assign(
{
exports,
module: {exports},
document: fakeDocument,
WebSocket,
console,
location: {hostname: 'localhost', reload() {}},
fetch(url) {

twome added a commit to twome/parcel that referenced this pull request May 12, 2019
…e/strip-type-module-attr

* 'master' of github.com:parcel-bundler/parcel:
  Update deps & gitattributes (parcel-bundler#3006)
  Fix assigning to exports from inside a function in scope hoisting (parcel-bundler#2994)
  Define __esModule interop flag when requiring ES module from CommonJS (parcel-bundler#2993)
  Replace module.require in scope hoisting (parcel-bundler#2875)
  Clear scope cache before crawling (parcel-bundler#2986)
  Fix CI (parcel-bundler#2990)
  Shake exports with pure property assignments (parcel-bundler#2979)
  Update postcss.js (parcel-bundler#2922)
  Fail immediately if yarn.lock updates are needed (parcel-bundler#2945)

# Conflicts:
#	packages/core/parcel-bundler/package.json
#	yarn.lock
twome added a commit to twome/parcel that referenced this pull request Sep 10, 2019
…e/strip-type-module-attr

* 'master' of github.com:parcel-bundler/parcel:
  fix source maps on coffeescript assets (parcel-bundler#3423)
  Fixes parcel-bundler#3133 by upgrading serialize-to-js from 1.1.1 to 3.0.0  (parcel-bundler#3451)
  Fix up misleading usage information (parcel-bundler#3158)
  bump chokidar to get a reload fix for linux (parcel-bundler#2878)
  Use uppercase for the first letter of the issue template (parcel-bundler#3192)
  Update dotenv-expand to allow overriding of falsy values (parcel-bundler#2971)
  Fixes 3076: HMR update breaks in webworker due to window (and location.reload) not existing in web worker context. (parcel-bundler#3078)
  Scope hoisting destructuring (parcel-bundler#2742)
  Create FUNDING.yml (parcel-bundler#3074)
  Added new info command  (parcel-bundler#3068)
  Fix typo (parcel-bundler#3043)
  Update deps & gitattributes (parcel-bundler#3006)
  Fix assigning to exports from inside a function in scope hoisting (parcel-bundler#2994)
  Define __esModule interop flag when requiring ES module from CommonJS (parcel-bundler#2993)
  Replace module.require in scope hoisting (parcel-bundler#2875)
  Clear scope cache before crawling (parcel-bundler#2986)
  Fix CI (parcel-bundler#2990)
  Shake exports with pure property assignments (parcel-bundler#2979)
  Update postcss.js (parcel-bundler#2922)
  Fail immediately if yarn.lock updates are needed (parcel-bundler#2945)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tree shaking enabled results in a script that has ReferenceError on line 1 (precompiled purescript source)
2 participants