Skip to content
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

module-types use assert type to detect module type #2434

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wenerme
Copy link
Collaborator

@wenerme wenerme commented Oct 5, 2022

No description provided.

@wenerme
Copy link
Collaborator Author

wenerme commented Oct 5, 2022

xref #2432

@github-actions
Copy link

github-actions bot commented Oct 5, 2022

File size impact

Merging module-types into main will impact 7 files in 3 groups.

browser (2/2)
File raw gzip brotli Event
dist/s.min.js -105 (7,776) -54 (3,069) -56 (2,784) modified
dist/system.min.js -109 (12,110) -53 (4,668) -59 (4,198) modified
Total size impact -214 (19,886) -107 (7,737) -115 (6,982)
node (1/1)
File raw gzip brotli Event
dist/system-node.cjs -1,674 (517,525) -273 (125,094) -251 (84,042) modified
Total size impact -1,674 (517,525) -273 (125,094) -251 (84,042)
extras (4/8)
File raw gzip brotli Event
dist/extras/global.min.js -4 (861) -1 (508) -2 (428) modified
dist/extras/module-types.min.js +581 (3,434) +295 (1,526) +275 (1,356) modified
dist/extras/named-register.min.js -10 (993) -6 (464) -2 (399) modified
dist/extras/transform.min.js -4 (671) -2 (408) -33 (335) modified
Total size impact +563 (5,959) +286 (2,906) +238 (2,518)
Generated by file size impact during size-impact#3198880154

@guybedford
Copy link
Member

Unfortunately, this is not how assertions are supposed to work - the content-type header is supposed to be the authoritative source for the module type information, not the assertion. https://github.com/tc39/proposal-import-assertions#proposed-semantics-and-interoperability

@wenerme
Copy link
Collaborator Author

wenerme commented Oct 6, 2022

Now module-types will do the assert, but can fallback to given type if mime is acceptable based on https://github.com/tc39/proposal-import-assertions/blob/master/content-type-vs-file-extension.md , do you thinks is this ok ?

this pr need #2435

@guybedford
Copy link
Member

Unfortunately the assertion can only be a validation and cannot affect the way we do fetching.

@wenerme wenerme force-pushed the module-types branch 2 times, most recently from aa88e31 to 4e6cdd1 Compare October 6, 2022 16:33
@wenerme
Copy link
Collaborator Author

wenerme commented Oct 9, 2022

@guybedford changed throw to a warning log, is this ok ?

}

var fetch = systemJSPrototype.fetch;
systemJSPrototype.fetch = function (url, options) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that we were passing the assertion directly as options here. Does that mean that you can set custom fetch options this way as well via the new System.register attributes?

Comment on lines +82 to +90
var mime = res.headers.get('content-type');
var contentType
for (var i = 0; i < contentTypes.length; i++) {
var rule = contentTypes[i];
if (rule[0].test(mime)) {
contentType = rule[1]
break
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a shorter way to do this?

Comment on lines +93 to +101
if (assertType && contentType){
if(contentType === fallback){
contentType = assertType
}
if (assertType !== contentType){
console.warn(process.env.SYSTEM_PRODUCTION ? errMsg('W7') : errMsg('W7', `Module ${url} has an unexpected content type of "${mime}": expected module type ${assertType} got ${contentType}.`));
contentType = undefined
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me, just watch the spacing of the syntax. Also shorter code is always better for this lib!

Copy link
Member

@guybedford guybedford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can add this, let's just clarify how we are exposing the fetch options to the assertions system? Does that mean System.register([], () => {...}, [{ headers: { ... } }]) can eg customize fetch options?

@wenerme
Copy link
Collaborator Author

wenerme commented Oct 12, 2022

@guybedford Passing header is possible, but should or shouldn't is a question, for me, the Meta just like a black box with minimal know info, every hook can use this to pass addition options, if Meta is passing all the way along, the Meta became the context, this make more sense.

@wenerme
Copy link
Collaborator Author

wenerme commented Oct 12, 2022

BTW, for hook level customization, I prefer the createHook pattern like

https://github.com/wenerme/wode/blob/19c21023dd6ea68cc4fd9fd73423927c5faa7237/packages/system/src/loaders/hookSystem.ts#L28-L29

meta is better to pass one-shot options.

But systemjs is packed as iife, so the createHook pattern is impossible, for global wise customization/options, user can override the hook and always patch the meta, but I don't know is this a good practice.

@wenerme
Copy link
Collaborator Author

wenerme commented Oct 12, 2022

I think meta should not used for extend assertions actions (e.g. extra loader, add more content type), just passing extra fetch option is acceptable, like

override the request init

System.register([], () => {...}, [{ request:{headers:{}} }])

or always use fetch as key

System.register([], () => {...}, [{ fetch:{headers:{}} }])

or override fetch

System.register([], () => {...}, [{ fetch:(u,o)=>fetch(u,{...o,headers:{}}) }])

Just adding headers will became confusing when there is a lot options for RequestInit, e.g. allowed passing cookie credential.

@guybedford
Copy link
Member

Passing header is possible, but should or shouldn't is a question, for me

This is what concerns me - perhaps we should restrict the functionality for now rather?

Copy link

@ExE-Boss ExE-Boss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +354 to +358

```js
import { foo } from 'foo.js' assert { type: 'json' };
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When using import assertion like
```js
import { foo } from 'foo.js' assert { type: 'json' };
```
When using import attributes like
```js
import { foo } from 'foo.js' with { type: 'json' };
```

e.g.

```js
System.import('https://example.com/my-module.txt', {assert: {type: 'css'}});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
System.import('https://example.com/my-module.txt', {assert: {type: 'css'}});
System.import('https://example.com/my-module.txt', {with: {type: 'css'}});

Comment on lines +14 to +15
var assertType = ((meta || {}).assert || {}).type;
return _shouldFetch(url, parent, meta) || moduleTypesRegEx.test(url) || !!loaders[assertType];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var assertType = ((meta || {}).assert || {}).type;
return _shouldFetch(url, parent, meta) || moduleTypesRegEx.test(url) || !!loaders[assertType];
var moduleType = ((meta || {}).with || {}).type;
return _shouldFetch(url, parent, meta) || moduleTypesRegEx.test(url) || !!loaders[moduleType];

Comment on lines +230 to +231
test('should load a.txt as css module by assert', function () {
return System.import('fixturesbase/css-modules/a.txt', {assert: {type: 'css'}}).then(function (m) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('should load a.txt as css module by assert', function () {
return System.import('fixturesbase/css-modules/a.txt', {assert: {type: 'css'}}).then(function (m) {
test('should load a.txt as css module by type attribute', function () {
return System.import('fixturesbase/css-modules/a.txt', {with: {type: 'css'}}).then(function (m) {

Comment on lines +238 to +239
test('should load a as css module by assert', function () {
return System.import('fixturesbase/css-modules/a', {assert: {type: 'css'}}).then(function (m) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('should load a as css module by assert', function () {
return System.import('fixturesbase/css-modules/a', {assert: {type: 'css'}}).then(function (m) {
test('should load a as css module by type attribute', function () {
return System.import('fixturesbase/css-modules/a', {with: {type: 'css'}}).then(function (m) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants