thefrontside / importmap-poc

0 stars 0 forks source link

Research on tools for generating importmaps #1

Open taras opened 1 year ago

taras commented 1 year ago
taras commented 1 year ago
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 commented 1 year ago

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

taras commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.

Rugvip commented 1 year ago

Yep just a plain resource loader, https://github.com/thefrontside/backstage/blob/5b381397e5b28e6371be94442ee559b4abb3fe69/packages/cli/src/lib/bundler/transforms.ts#L149.

This is a custom Webpack extensions 😁 https://github.com/thefrontside/backstage/blob/5b381397e5b28e6371be94442ee559b4abb3fe69/packages/cli/src/lib/bundler/transforms.ts#L115-L137

cowboyd commented 1 year ago

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:

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 commented 1 year ago

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.