-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Non-global IIFE format for compile API #345
Comments
This seems like a reasonable addition, especially since it doesn't cost anything to people who don't use it. What if we called the new format In addition, maybe we could add a const MyComponent = svelte.create( input, {...});
// equivalent to
const output = svelte.compile( input, { format: 'eval', ... });
const MyComponent = eval( output.code ); |
I like your idea about naming it If we create a |
Agree, no reason not to expose it. Only minor distinction is that it should use indirect eval rather than direct eval, since direct eval is a security and minification risk: (function () {
var foo = 42;
console.log(eval('foo')); // 42
var eval2 = eval;
console.log(eval2('foo')); // ReferenceError — can't access local vars
}()); |
Ah perfect, I can't say I've used |
I wrote an implementation, and have some test cases. I'd like to do some more testing before I submit that PR. But things are going smoothly! Do you have any preference of indirect eval vs |
Great! I think they're basically equivalent — |
|
The ECMAScript standard states that an indirect eval uses the global scope, as well as Is this what you're referring to when you say "doesn't reuse the scope"? If not, could you elaborate on what you meant? |
It's local scope that's the issue — in the example above (modified to use the (function () {
var foo = 42;
console.log(eval('foo')); // 42
console.log((1,eval)('foo')); // ReferenceError — can't access local vars
}()); ...anything which was in the global scope would be accessible in both cases. The reason this is important is that direct |
I'm talking about this: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#2-unsupported-syntax |
If my reading is correct, |
I don't see why not. Would just have to be |
I have made the necessary change in #361. |
Changed `create` API method to use `new Function()`
It would be nice to have a compiler format that simply outputs an IIFE that doesn't not assign the output to a global variable.
My reasoning behind this is such that I'd like to be able to compile a component in the browser, eval that code, and then assign the output to a state tree node, or a local variable.
Currently I'm using the IIFE format and just running
replace
on the output code before eval-ing the code.Example:
I would like to be able to do this, to avoid any possible collisions. It's possible that the variable I'm using as the IIFE
name
could be used (I understand that I could use some jargon, but I consider that a naïve practice)Example:
I'm willing to make this PR (it should be as simple as creating a new intro/outro), I just wanted to have an open discussion before spending any time on this.
My questions are as follows:
a. Or, should it still be
iife
, but with another flag (e.g.{ global: true || false }
)The text was updated successfully, but these errors were encountered: