-
Notifications
You must be signed in to change notification settings - Fork 26
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
Bikeshed possible syntaxes for import statements #6
Comments
I'd personally prefer out of band and just wanted to be sure that is left considered. |
Would this be the right place to consider syntaxes that don't use |
It's pretty hard to introduce a new keyword into JS syntax-wise. What would be the motivation? (Here is a good place to discuss IMO) |
I came to this repo from the webcomponents issue I linked above, because I'm interested in the implications these decisions will have on the scripting security model. The goal that I see here is to have a static import syntax that works more or less the same as I'm not an expert in the current state of the various standards involved, so I could be wrong, but my understanding is that the static Am I right in thinking that this second "mode" isn't actually part of a common standard? At least when I use it in my own projects today, it winds up going through a transpiler / webpack loader under the hood. But the goal, as I understand it, is to formalize this behavior so that we can have consistency across environments, with first-class support that doesn't require polyfills or transpilers. That leaves us with two distinct security models: ES modules that load via a script execution engine with global state, and a bunch of module types that load via stateless parsers that handle "passive" content like JSON, HTML, etc. Today's transpilers implement both of these features with the same keyword, but why should that be so? Sure, it's easy to piggy-back on So, that's my motivation. Strictly from the perspective of security-model concerns, using two different keywords solves it and neatly sidesteps basically every concern raised in this repo -- attribute syntax, in- vs out-of-band, handling in Node vs web, interactions with MIME types, etc. None of that is necessary if we just say that |
@thw0rted all JS Spec defined modules are part of an Abstract Module Record type intended to allow interoperability with other Module types potentially not in the specification. The design was to allow a single system of loading a variety of types. In addition, a variety of uses are possible without side-effects but still going through a JS module of sorts such as:
I'm unclear how a split would occur naturally for these differing types and it seems like they should be left to the same mechanism for loading. We could try and force an artificial separation of types, but that seems unclear in motivation and benefits to me currently. |
I always thought of As for the second bullet, I'm aware that a given JS module could be written to have no side effects, but you can't reason about whether they will or not when importing them, because they're evaluated in the same global scope -- any JS module could have side effects, from the security model's POV. The "forced, artificial" separation of types is actually a natural division between evaluating a resource that has access to the global scope (i.e. a JS module) versus evaluating a resource that does not have that access (i.e. everything else). |
It looks weird to me that this proposal aims for a single attribute.
This resembles |
@phistuck while the proposal promotes a single |
The current proposal supports arbitrary key-value syntax, as |
Whatever syntax we choose, I think it is important to separate different kinds of attributes at syntax level. Otherwise, it will get harder to write import for different platforms. (See #30 (comment) #30 (comment) and #30 (comment) ) |
I don't have any non-assert attributes to propose. What if we say that |
I went through #3, #5 and a few other related issues. I agree with various sayings but the most consequential to me are the following. Since Going through various @chicoxyzzy's comments in #3 (first, second, third) I generally agree with @justinfagnani in #5 that something like the object notation would look more readable. However, I do not agree that the record syntax (comment) is a good idea in this case. Trying to combine these arguments it took some ideas from C++ and Pascal and baked it into the syntax. Dynamic importI thought about 2 options given in the current README#Before stage 3 I vote for the short syntax. If we define function import(moduleUrl, moduleAttributes) {
...
} passing an object like Also, defining As a counter-argument to this may be wishing to add one more keyword to the import statement, i.e. for the following static import: import package from './package.json' with type = 'json' having rel = 'fallback' the dynamic call might look like: const package = await import('./package.json', {
with: { type: 'json' },
having: { rel: 'fallback' },
}) However, this does seem to be too complicated and can be easily blended into the current proposal as an attribute: const package = await import('./package.json', { type: 'json', rel: 'fallback' }) Currently, I do not see any possible need to add another keyword to the import statement, hence no need to use such keyword in the configurational object to pass attributes. Conclusion and proposalTo conclude my arguments from above I propose the following example: const GLOBAL_CONFIG_JSON_VERSION = 2
const GLOBAL_CONFIG_DEFAULT_ATTRIBUTES = {
syntax: `json-${GLOBAL_CONFIG_JSON_VERSION}`,
}
import('./config.json', {
... GLOBAL_CONFIG_DEFAULT_ATTRIBUTES,
type: 'json',
}) Since it's dynamic import, dynamic constructing of attributes should be allowed. I did not find any sayings about this, perhaps it's obvious. Static importFor the static import, I propose the following syntax: Shorthand syntaxSuch syntax could be used when a single attribute is passed. import package from './package.json' using type = 'json' Full syntaximport package from './package.yml' using {
type = 'json';
syntax = 'YAML';
} The object-like-literal may be confusing but this is not an object constructing but more a block scope. Also, import given from './given'
given('Viktor Yakubiv') // should return 'Viktor' the engine should possibly throw AlternativesIn my example, the assignment operator may be as confusing as the object notation. Hence I propose a few alternatives: Two colons (
|
The The syntax with operators wasn't mentioned before but it looks overloaded to me. |
The proposal has settled on the Thanks for contributing to the discussion! |
Various syntax alternatives are proposed in #3, #5 and WICG/webcomponents#839.
What do you think of these alternatives? Are there other syntaxes we should consider?
The text was updated successfully, but these errors were encountered: