Description
Having read through the other issues, I note that this part of the proposal isn't exactly accurate:
Another option considered and not selected has been to use a single string as the attribute, indicating the type. This option is not selected due to its implication that any particular attribute is special; even though this proposal only specifies the
type
attribute, the intention is to be open to more attributes in the future.
As currently envisioned, the type
option is indeed special. It defines the loader to use to transform the resource into its imported form. Any other additional attributes are really arguments for that loader.
I mean, the reason there's a need for type
in the first place is that we can't trust that a network request for a .json
file actually resolves to be a JSON file; type
is fulfilling the role that file extensions have traditionally taken.
Making a keyword-based type
explicitly special would also provide a way for those attributes not to need to be defined inline and separately for each import, by providing a key to use in a central registry for other attribute defaults.
Finally, the simplicity of a string as
value and the power of a with
key:value set of attributes are not mutually exhaustive. Why not have both?
import.meta.registerType('custom', loadFunction, { some: 'attributes' })
import foo from './foo' as 'json'
import foo from './foo' as 'json' with foo: 'bar'
import foo from './bar' as 'custom'
import foo from './bar' as 'custom' with some: 'override'
This sort of syntax would provide both simplicity and flexibility, allowing for often-used attribute sets to be controlled centrally, while maintaining the liberty of defining specific values for a single instance.
Overall, the role of module attributes seems rather similar to that fulfilled e.g. by Webpack loaders. This is what the Webpack docs say on this now, based on a few years of experience:
Use
module.rules
whenever possible, as this will reduce boilerplate in your source code and allow you to debug or locate a loader faster if something goes south.