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

Import maps as object #65

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

Conversation

Staremang
Copy link

Implements the ability to pass importMaps as an object

Example:

const baseUrl = '/foo'; // may by an environment variable
// ...
vitePluginSingleSpa({
  // ...
  importMaps: {
    type: 'importmap',
    dev: {
      imports: {
        '@learnSspa/spa01': 'http://localhost:4101/src/spa.ts',
        '@learnSspa/spa02': 'http://localhost:4102/src/spa.ts',
      },
    },
    build: {
      imports: {
        '@learnSspa/spa01': `${baseUrl}/spa01-prefix/spa.js`,
        '@learnSspa/spa02': `${baseUrl}/spa02-prefix/spa.js`,
      },
    },
  },
});

@webJose
Copy link
Contributor

webJose commented Jan 25, 2024

Hello, @Staremang. Thank you for your interest in the project.

Could you provide me with the motivation behind this change? What moved you to implement this alternative way of passing import maps? Are there specific scenarios where this is the only way of doing it? Please elaborate.

Thank you, and welcome!

@Staremang
Copy link
Author

Staremang commented Jan 26, 2024

Hi!

We pass BASE_URL through environment variables. I haven't found any other way.

Example

// vite.config.ts

export default ({ mode }: ConfigEnv) => {
  const env = loadEnv(mode, process.cwd(), '');

  return defineConfig({
    base: env.BASE_URL,
    // ...
    plugins: [
      vitePluginSingleSpa({
        // ...
        importMaps: {
          type: 'importmap',
          dev: {
            imports: {
              '@learnSspa/spa01': 'http://localhost:4101/src/spa.ts',
              '@learnSspa/spa02': 'http://localhost:4102/src/spa.ts',
            },
          },
          build: {
            imports: {
              '@learnSspa/spa01': `${env.BASE_URL}/spa01-prefix/spa.js`,
              '@learnSspa/spa02': `${env.BASE_URL}/spa02-prefix/spa.js`,
            },
          },
        },
      }),
    ],
  });
};

And run command

> BASE_URL=/nbo vite build

@webJose
Copy link
Contributor

webJose commented Jan 26, 2024

Interesting. Certainly I have thought about something similar, but I have been delaying this because I want to complete the separation of the merging algorithm I created for wj-config. The import map merging algorithm in this package is flaky and I don't like it. I have made progress (wj-merge), but I haven't finished.

Is the value of BASE_URL arbitrary? If not, you could prepare variations of the import map in different files and conditionally set the import map file names in vite.config.ts.

The following code snippet assumes you have a finite set of base URL's. There will be one importMap.<key>.json file per base URL. In this case, it is not really necessary to set an environment variable, and instead you could use the value of mode directly as a key.

export default function ({ mode }: ConfigEnv) {
    // The possible values of mode would be: a, b, c
    return defineConfig({
        plugins: [
            vitePluginSingleSpa({
                importMaps: {
                    type: 'importmap',
                    dev: 'importMap.dev.json',
                    build: `importMap.${mode}.json`
                }
            })
        ],
        ...
    });
};

If the possible values of BASE_URL are not finite so that import map files cannot be prepared ahead of time, then I guess you would need your current solution.

Now, having said that: If you would like to pursue this feature, I will support it. The first 2 comments I have for this PR are:

  1. The README instructions need to be updated to explain the possibility of using an object.
  2. Unit tests need to be added to the plugin-factory-tests.ts file.

@webJose
Copy link
Contributor

webJose commented Feb 25, 2024

Hello, @Staremang. There's been no activity or follow up from you on this PR. Let me know, if possible, if you would like to continue with this.

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.

2 participants