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

question: why not strict mode in compiled output? #2098

Closed
frehner opened this issue Jan 14, 2020 · 15 comments
Closed

question: why not strict mode in compiled output? #2098

frehner opened this issue Jan 14, 2020 · 15 comments

Comments

@frehner
Copy link
Contributor

frehner commented Jan 14, 2020

Not really an issue, more of a question:

is there a reason that strict mode is explicitly turned off in the compiled output?

https://github.com/systemjs/systemjs/blob/master/rollup.config.js#L11

I just tested it with strict mode on, and all the tests continue to pass. So it doesn't appear (on the surface) to have to do with features you're building or anything.

Anyway, just curious.

@joeldenning
Copy link
Collaborator

I’m not sure if it’s about saving the bytes for ”use strict”; or if there were/are features of javascript that are being used that strict mode removes. I would be in favor of changing to strict mode if it doesn’t break things.

@guybedford do you have thoughts?

@guybedford
Copy link
Member

Yes exactly those bytes are deemed unnecessary :)

@joeldenning
Copy link
Collaborator

@frehner could you explain how strict mode / not strict mode impacts things and how you even noticed?

@frehner
Copy link
Contributor Author

frehner commented Jan 16, 2020

@frehner could you explain how strict mode / not strict mode impacts things and how you even noticed?

I came across it as I was debugging something else. In the end, it turned out that it was because my code wasn't being executed in strict mode.

However, I think there may still be some value in having systemjs itself execute in strict mode: when it's in non-strict mode (aka sloppy mode) there is the extremely small but still possible chance that there's an error that would throw in strict mode, but since it's in sloppy mode, it would fail silently.

Those types of situations would be extremely hard for users of SystemJS to debug properly.

@joeldenning
Copy link
Collaborator

I support turning on strict mode - I think a better error message (that only rarely shows) is worth the 10 bytes.

@guybedford
Copy link
Member

SystemJS itself not running in strict mode doesn't affect code it evaluates executing in strict mode.

Whenever converting modules to SystemJS module format, the 'use strict string should be present in the function wrapper for compatibility.

If that is missing, that is a bug in the transpiler outputting the System format.

@frehner
Copy link
Contributor Author

frehner commented Jan 17, 2020

From my testing, I think systemjs bring in strict mode does actually affect things. But I could be wrong here.

This fails silently, even though it was called in strict mode. This would be similar to calling SystemJS functions from my own code, correct?

// fails silently, even though sloppy() is called from a location where strict mode is active
function sloppy() {
  myGlobal = 5
}

(function() {
  "use strict";
  sloppy()
})()

And just for reference: this throws an error, even though it was called in sloppy mode

// throws an error, even though strict() is called from a location where sloppy mode is active
function strict() {
  "use strict";
  myGlobal = 5
}

(function() {
  strict()
})()

so even if I call systemjs functions in strict mode, whatever systemjs itself does appear to depend on whether it itself is in strict mode.

If I'm reading and interpreting this correctly.

@frehner
Copy link
Contributor Author

frehner commented Jan 17, 2020

updated the above comment now that I'm at a computer.

@frehner
Copy link
Contributor Author

frehner commented Apr 4, 2020

Just had a report that systemjs fails to work in older versions of chrome (v45) due to this. The error:

Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

@guybedford
Copy link
Member

guybedford commented Apr 4, 2020

@frehner can you share your SystemJS module output here? It should look like -

System.register([..], function (...) {
  'use strict';
  // ...
});

if the 'use strict' is missing then you might get an error like this.

Alternatively if using AMD or globals you would also get an error like this.

@frehner
Copy link
Contributor Author

frehner commented Apr 4, 2020

It may have actually been an issue in single-spa itself. We just corrected it in this PR single-spa/single-spa#507 (comment)

@joeldenning
Copy link
Collaborator

It's true that single-spa was not in strict mode, but that would not cause the error Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode. Single-spa does not have any let or const variables in its output, due to the browserslist that has been set. See https://unpkg.com/single-spa@5.3.1/lib/umd/single-spa.min.js - there are no let or const variables in there.

I think that this is a problem with SystemJS - that's what the person reporting the issue indicated. SystemJS currently doesn't work in certain older versions of Chrome due to its use of const and let outside of strict mode. See https://unpkg.com/systemjs@6.2.6/dist/system.min.js

I think the fix is to enable strict mode, even though it incurs about 10 bytes to the bundles.

@guybedford
Copy link
Member

Ahh I didn't realise const and let were an issue in Chrome 45 - I'd be happy to just go back to var everywhere to avoid this.

@guybedford
Copy link
Member

(kind of odd to have IE11 support but not Chrome 45!)

joeldenning added a commit that referenced this issue Apr 7, 2020
* Removing const / let in favor of var, for Chrome 45. See #2098.

* Review feedback

* Fix issue with iteration scope

* Self review
@guybedford
Copy link
Member

This was resolved in #2162.

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