porsager / wright

šŸš€ Work Fast Work Wright
Do What The F*ck You Want To Public License
180 stars 15 forks source link

Some projects only work with --jail false #40

Closed shofetim closed 4 years ago

shofetim commented 5 years ago

Hello @porsager

I recently picked up several projects that I know used to work with Wright, but now don't. I am still trying to track down whether it was Wright, or Chrome, or Mithril.js that changed and caused things to stop working, but I have found that turning jailing off gets things working again.

Looking at jailing, I don't understand what is going on here: https://github.com/porsager/wright/blob/81a2e193cda8408b1c527076ea98b93a1b9ab71d/lib/jail.js#L7

How does appending an empty eval("") change the context(?) of the variables created?

Here is my test case that works with --jail false but not without:

index.html

<!DOCTYPE html>
<html>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
   <script src="https://unpkg.com/mithril/mithril.js"></script>
    <body>
        <script src="/app.js"></script>
    </body>
</html>

app.js

"use strict";

var App = function () {
  return {
    view: function() {
      console.log("App.view()");
      return m("h1", "Hello World");
    }
  }
};

m.route(document.body, "/", {
  "/": {
    render: function() {
      return m(App);
    }
  }
});
porsager commented 4 years ago

Hi @shofetim .. I'm sorry ā€” I don't understand how this issue has escaped my attention, but I'll look closer at it now.

The reason wright does that is to avoid chrome cleaning out variable references between code injections. This ensures that if you're making changes back and forth HMR will still work.

porsager commented 4 years ago

I see I had a fix for this in the next branch already so I just applied that to master ;)