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

Research on tools for generating importmaps #1

Open
taras opened this issue Sep 12, 2023 · 8 comments
Open

Research on tools for generating importmaps #1

taras opened this issue Sep 12, 2023 · 8 comments

Comments

@taras
Copy link
Member

taras commented Sep 12, 2023

@taras
Copy link
Member Author

taras commented Sep 12, 2023

npx jspm@latest link --map plugins/manifest-manager-backend/src/service/index.html -o test.html -p nodemodules -r backstage-plugin-plugin-a=backstage-plugin-plugin-a -r backstage-plugin-plugin-b=backstage-plugin-plugin-b

Fails to import svg with the following error Error parsing file:///Users/tarasmankovski/Repositories/backstage/importmaps-poc/node_modules/@backstage/core-components/dist/components/EmptyState/assets/createComponent.svg:1:4853

@cowboyd
Copy link
Member

cowboyd commented Sep 12, 2023

@taras what does importing svg mean? Like on-the-fly transpilation of a .svg to a ESM representation?

@taras
Copy link
Member Author

taras commented Sep 12, 2023

Here is an example

import missingAnnotation from './components/EmptyState/assets/missingAnnotation.svg';

Webpack would put the file into the public directory and return a URL to that file. In our case, we need to generate a URL without attempting to treat it as a JS file.

@cowboyd
Copy link
Member

cowboyd commented Sep 12, 2023

Hm.... looks like a non-standard Webpack extension

https://github.com/thefrontside/backstage/blob/master/packages/core-components/src/components/EmptyState/EmptyStateImage.tsx#L68

In order to use this technique, the plugins do need to export valid JavaScript, so it would need to be added to the build step like the TypeScript compilation, or find another way to do it.

@taras
Copy link
Member Author

taras commented Sep 12, 2023

I'm not sure that's true. I'm pretty sure it's just using a loader that loads static files. You can see that it's just a regular svg file https://github.com/thefrontside/backstage/blob/master/packages/core-components/src/components/EmptyState/assets/createComponent.svg

I'm unsure if it's a plugin that comes with webpack, but it's pretty common.

@cowboyd
Copy link
Member

cowboyd commented Sep 12, 2023

By non-standard, I mean something that your browser's JavaScript engine does not understand. For example, it is also very common to import .css files too, but at the end of the day, ES modules imported into the browser need to be ES.

Today, Webpack is a kitchen sink that does:

  • transpilation with TypeScript + custom extensions (let's call it JS++)
  • bundling
  • module resolution

Using import maps severs the latter two responsibilities and offloads them onto the browser, which I think is a major plus long term, but it does mean we still need to handle the first responsibility somehow.

In this particular case, my personal preference would be to find a different way to do it (Something about publishing an ES module that assumes a path on a web server feels wrong) but in the general case, we would need to treat cases like these just like we would TypeScript: before publishing it needs to transpiled to standard JS with a tool like babel or esbuild.

@taras
Copy link
Member Author

taras commented Sep 12, 2023

We have control over the bundles generated by Backstage CLI. We could create a new bundle that would include only the modules that browsers understand.

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

3 participants