-
Notifications
You must be signed in to change notification settings - Fork 45
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
Naming inline modules #42
Comments
This would not be possible under the current spec due to there not being a concept of module names any more. Everything is keyed by url so virtual modules are an awkward fit now (hopefully this decision will be reconsidered, read #28). Add this to the pile of benefits of module ids over urls. Also, neither System.define nor System.module have counterparts in the current spec. |
Surely it makes the most sense, as @dherman pointed out at jorendorff/js-loaders#83 (comment), to do the following:
I can't imagine any conflicts occurring with this system. |
@matthewp that's not accurate, you can have any arbitrary entry in the registry.
The normalization process (to a url), including the |
@caridy Interesting, |
@timbur This spec is quite a bit different from the one referenced in that thread, we need a |
In my opinion, es6-module-loader and systemjs are handling everything perfectly and intuitively, so it would make sense to model anything missing from this spec after their functionality. Plus, those projects are designed to match this spec, so it's a bit of a win-win for everyone. |
@timbur systemjs should not dictate what we do here, it is the other way around. We are considering those ideas that we have tried, and play around with in systemjs, but ultimately we have to be pragmatic. @matthewp I think you're confused. |
I didn't mean to imply that SystemJS should dictate anything here. I was only saying that there is some extra functionality present within SystemJS which makes a lot of sense, and this functionality exists because of practical, real-world necessity/usefulness. I'll provide an example of said necessity shortly (in addition to the example I provided above). |
This spec has a @timbur note that |
@caridy I might be confused, let's break this down:
This normalization process is (potentially) asynchronous, if it goes through the |
@matthewp the normalization process only happens when importing a module (or a dep), not when installing a new entry in the registry. |
Ok, then I did misunderstand you. So this mean keys are really just arbitrary strings, I can do: System.install("@@foobar", { default: function() { return "bar"; } }); And anyone importing that will get the correct module, cool. |
@guybedford Can that same flexible behavior be achieved directly through As for the real-world necessity, it makes no sense to render inline modules inaccessible. They're modules, too, right? It should be possible to import them. It also doesn't make sense to give them the The way I see it, from a simple point of view and for predictability's sake, internally, when paths are set and modules are registered, all that matters in the end is the corresponding key. A few simple rules and everything gets insanely simple:
Anything beyond that would be convoluted, adding unnecessary complexity and wasting everyone's time. Keep it as simple and predictable as that and the only conceivable way developers could screw up is if they purposely shoot themselves in the foot. |
I like the simplicity of having just What about this: <module src="hello.js">
export default 'I am a module'
</module> |
@MajorBreakfast that means |
@matthewp Hmm, yes. Maybe a |
@timbur no. we have discarded that a while ago, and we've settled on the idea that inline modules will probably be focus on initialization (just like inline scripts are today), and therefore there is really no need to export things into the runtime from them, instead they just import other modules, and carry on with some initialization routines. I'm curious about what sort of inline script code you have today that modifies the runtime to extend the available functionality. |
@guybedford I missed your comment but you're right that |
@caridy My issue with that line of thinking is that if developers want to do something, they'll figure out a way to do it. It's always better to provide an elegant, predictable solution for people to use, rather than imposing arbitrary restrictions that will lead developers to create ugly hacks/workarounds. And for this particular case I'm 100% certain that will happen. Consider this scenario: Users load some module specific to their profile. We'll refer to it as a Suppose we have my duck at export function quack () {
// let's pretend this is all kinds of complex logic
console.log('quack!');
} And we have your duck at export function quack () {
// let's pretend this is all kinds of complex logic
alert('quack!');
} And when I load the page, the HTML generated for me looks something like this: <module>
import duck from 'timbur/duck.js';
duck.quack();
</module> And yours of course looks something like this: <module>
import duck from 'caridy/duck.js';
duck.quack();
</module> And later on, perhaps some other module wants to make the user's particular Without the ability to name (and register) the <script>
import duck from 'caridy/duck.js';
duck.quack();
window.main = {duck: duck};
</script> Or for the sake of consistency and "elegance" (if you could call it that), one might pull some other module designed specifically for storing this information: <script>
import store from 'store.js';
import duck from 'caridy/duck.js';
duck.quack();
store.set('duck', duck);
</script> And then for our window.main.duck.quack(); Or: import store from 'store.js';
store.get('duck').quack(); Now let's be honest. The above solutions are ugly and they go against the grain of the module system. Of course, all of this so far might seem like a contrived example, and most people might find this particular usage an edge case. But again, it's never a good idea to impose restrictions on what should or shouldn't be possible only because it isn't some scenario that is easy to imagine. People are obviously already aware of these possibilities, so why limit them? It's much better to provide an elegant solution at the core. And finally, let's consider a solution that I would probably use if naming an inline module was not built into the core of HTML snippet: <module name="main">
import duck from 'timbur/duck.js';
duck.quack();
export { duck };
</module>
<script src="register-inline-modules.js"></script>
var modules = document.getElementsByTagName('module');
for (var i = 0; i < modules.length; i++) {
var module = modules[i];
if (module.hasAttribute('name')) {
var name = module.getAttribute('name');
var source = module.innerHTML.substr(1);
System.define(name, source);
}
} Then import main from 'main';
main.duck.quack(); That is way simpler and follows the module paradigm much more consistently. But we aren't finished yet. Last but certainly not least, consider that the HTML snippet: <module name="main">
import duck from 'timbur/duck.js';
duck.quack();
export { duck };
</module>
import main from 'main';
main.duck.quack(); It's entirely possible that I'm being stupid and completely overlooking an easier way to achieve this. And if that is the case, please let me know and I will gladly shut up about this particular example. Either way, I think there are more than enough use cases out there that we haven't thought of which will resemble the above functionality, so why not make it elegant? |
@timbur I think the idea is to have a
Not if the solution can be considered a dead end. I think it's good to separate JavaScript from HTML. |
But it isn't a dead end. The key point is that it should be possible to elegantly register dynamic modules under some common name without resorting to extra scripts. Yes, in production, all static modules will be minified and bundled. But it would be stupid to create separate bundles for each user due to some dynamic module reference. The dynamic module would obviously automatically be excluded from the bundle, and the most efficient way for modules within the bundle to use it is to have its reference exported upon initialization. I don't know how I can make it any clearer, and I've yet to see a reasonable argument against this functionality, nor a more elegant solution to the problem. |
Can the same effect be created with <script> System.sites = {main: 'timbur/main.js'}; </script>? Unlike Tim's example it does not require a with a name. (To be sure, IMO we don't need a tag without a name either. A <script> tag with System calls are clearer and provide better developer ergonomics in realistic code.) jjb On Fri, Apr 3, 2015 at 5:08 AM, Tim Burgess [email protected] wrote: > But it isn't a dead end. The key point is that it should be possible to > elegantly register dynamic modules under some common name without resorting > to extra scripts. Yes, in production, all static modules will be minified > and bundled. But it would be stupid to create separate bundles for each > user due to some dynamic module reference. The dynamic module would > obviously automatically be excluded from the bundle, and the most efficient > way for modules within the bundle to use it is to have its reference > exported upon initialization. I don't know how I can make it any clearer, > and I've yet to see a reasonable argument against this functionality along, > nor a more elegant solution to the problem. > > — > Reply to this email directly or view it on GitHub > https://github.com//issues/42#issuecomment-89268037. |
That's a pretty solid suggestion @johnjbarton, albeit still not nearly as elegant and intuitive as it could be. And I actually agree that a We're already seeing a ton of movement towards minimizing the feedback loop with development - e.g., webpack hot module replacement, react-hot-loader, workspaces within dev tools that automatically save changes to CSS within the inspector, etc., etc. So how can we get the feedback loop to an absolute minimum? By having all of our development tools directly in the browser. And of course, modules are undoubtedly the future, so we need a really solid module system that works seamlessly with this paradigm. It needs to be effortless to develop modules and make them work together, and even inline modules should be identifiable. It's important to know what's what. With that said, let's compare what an in-browser development environment would look like without named modules. Also consider that there might end up being more than a handful of anonymous modules in the development environment, for whatever purposes. And this is what it looks like with the named inline module. It's more intuitive, much easier to decipher. |
@timbur are you talking about bucket testing? jejejeje Anyway, @johnjbarton is righty, |
@timbur Each HTML file should only contain one module tag. The named file that shows up in the dev tools cannot be edited and saved because it exists on disk inside the index.html. Why not use a |
@caridy I'm not talking about bucket testing at all, but that's a perfect example of one of the many possibilities that would be easier (or more elegant, at least) if naming inline modules is supported. Can you provide an example using @MajorBreakfast You're thinking inside the box. In an ideal in-browser development workflow, there are all kinds of possibilities that could potentially result in multiple html files within the sources panel and thus, multiple inline modules. As for using a |
@timbur and co, our goal is not to provide sugar for every use case but for the most common ones, and make sure that the rest can be implemented in userland. This use case can be implemented in userland, no doubt about it, and We haven't seen this one very often when people embrace the ES module philosophy, It is really more like a side effect of trying to make modules to behave like scripts, which is fine but ... it is not really something we will promote or try to provide sugar for. If you really want to continue this conversation, you should demontrate that the currect spec does not allow you to do it on userland, or came up with a real very common use case that will requiere named inline modules. Makes sense? |
Of course just about anything can be achieved in userland. The point I've made is that including this functionality within the spec would only result in one or two lines extra of code at core of |
one that I've just been investigating is HTML Imports. Currently most existing web components that I've seen that use HTML Import have an inline template plus an inline script to deal with the template. In some cases, that script will just be an initialisation of some sort, but in others the component can be interactive and expose some sort of API - getThis, setThat. If these scripts are converted to modules, either there will have to be a rule that they should always link to an outside file (which might make sense from CSP point of view), or they will have to be named, so other modules can import them. |
a simple example of this is Polymer. https://github.com/Polymer/polymer/blob/master/dist/polymer.html is the main component which runs a script to load the Polymer class function which all Polymer components use. This is currently a global. If you change this structure to modules, then all components have to import the Polymer module. To do that, it has to be named. |
AFAIK custom elements are global in nature. The API can live on the element. It's essentially: Edit: Only |
One could imagine that script being upgraded to a module: <script type="module" src="polymer.js"></script> Where 'src' above is an unnormalized module name (it may be called 'key' or something else though). There is no need to name the module in the above, it is loaded anonymously and stored in the registry like any normal module load. Defining how modules affect rendering is the next question there though. |
@guybedford You're saying that The basic issue here is that you are not importing a module file with a url, you are importing a document fragment which contains one or more module definitions which may need to be loaded in the registry, System.define() in the old spec, loader.install() in the new. |
@MajorBreakfast HTML imports don't have to be custom elements. They could be a form or a bunch of divs or whatever grouping of html elements you like. |
Having raised this issue, I'm not sure there is actually a problem. :-) The simple solution for the case where a component has to export values is to split the code in two: the In summary, a web app is an html document that can import stylesheets and components, and can contain an anonymous |
correct, none of these examples really require an inline module to be named, lets keep it that way for now. We can always revisit this if needed :) |
I was referred here to discuss this. Apologies if I'm going about it wrong. :)
The idea is to allow inline modules to be cached within the loader just like any other module by giving
<module>
aname
attribute.Suppose you have some inline module within HTML like so:
And
some/static/script.js
might look for themain
module:Two other relevant discussions:
ModuleLoader/es-module-loader#343
jorendorff/js-loaders#83 (comment)
The text was updated successfully, but these errors were encountered: