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

[@web/dev-server-import-maps] Support for external import maps #2562

Open
FluorescentHallucinogen opened this issue Nov 19, 2023 · 5 comments

Comments

@FluorescentHallucinogen
Copy link

FluorescentHallucinogen commented Nov 19, 2023

I've used https://github.com/jsenv/importmap-node-module to generate import map for my project.

After that I've tried to use the generated project.importmap file:

<html>
  <head>
+   <script type="importmap" src="./project.importmap"></script>
  </head>
  <body>
    <script type="module">
      import lodash from "lodash"
    </script>
  </body>
</html>

I've used http-server npm package to serve my project "as is" without any transformations. But I get the External import maps are not yet supported. error in the latest version of Chrome.

So I've tried to use the @web/dev-server with @web/dev-server-import-maps plugin, but get the Import maps with a "src" attribute are not yet supported. error. See https://github.com/modernweb-dev/web/blob/master/packages/dev-server-import-maps/src/importMapsPlugin.ts#L122.

Since import maps are now supported natively cross-browser, the need for the @web/dev-server-import-maps plugin is reduced. But if it supported external import maps, its value would immediately increase.

As far as I understand, all that's needed is to inline the content of the external import map file into <script type="importmap"></script> tag inside HTML.

@FluorescentHallucinogen
Copy link
Author

I've quickly patched the https://github.com/modernweb-dev/web/blob/master/packages/dev-server-import-maps/src/importMapsPlugin.ts#L121-L123 file, and it works!

      if (getAttribute(importMapScript, 'src')) {
-       throw new Error('Import maps with a "src" attribute are not yet supported.');
+       const importMapPath = getAttribute(importMapScript, 'src');
+       const content = fs.readFileSync(importMapPath, 'utf8');
+       setTextContent(importMapScript, content);
+       removeAttribute(importMapScript, 'src');
      }

@bashmish
Copy link
Member

bashmish commented Nov 20, 2023

There might be a good reasons not to support src.

From MDN https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap

The src, async, nomodule, defer, crossorigin, integrity, and referrerpolicy attributes must not be specified.

Pay attention that src is specifically mention as not allowed.

At the same time, in the proposal I see the src being described in https://github.com/wicg/import-maps#import-map-processing

You can install an import map for your application using a <script> element, either inline or with a src="" attribute:

Is there actually a consensus about this feature?

In general we try not to add non-standard features, at least without a good confidence that they will become a standard and got enough support from browser vendors and other stakeholders. In this case I can imagine the src involves an extra HTTP call which must be a blocking request due to how essential this information is for the module resolution, so maybe that's why it's not implemented natively.

@FluorescentHallucinogen
Copy link
Author

See WICG/import-maps#235. @domenic wrote that external import maps will be implemented in Chromium, but can't give an ETA.

Import maps can be very large and generated by third-party tools. Inline them manually into HTML is a poor DX.

@bashmish
Copy link
Member

Thanks for sharing this link, it does shed some light on this.
I quickly scrolled through and didn't find a confirmation about other browsers vendors, except Chrome. There is a link to a Mozilla/gecko repo where it's also raising an exception for external import maps.

I have a bad feeling about the future of this feature given it's taking already more than 3 years for Chrome and others to implement it, so I'd rather not run ahead of the locomotive.

@bashmish
Copy link
Member

Not sure if we had a practice before in Modern Web with experimental feature plugins, maybe that can be a sort of in the middle solution. Curious if @thepassle @Westbrook @koddsson have an idea?

You can also make your own plugin meanwhile and publish it yourself.

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

No branches or pull requests

2 participants