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

vite-plugin-external: Allow externals as function that is called on runtime #44

Open
irshadahmad21 opened this issue Jul 28, 2024 · 3 comments

Comments

@irshadahmad21
Copy link

irshadahmad21 commented Jul 28, 2024

Hello,

Thank you for this wonderful package. It has been so helpful.

We have been using it to externalize some packages we develop in our monorepo and then externalize them. It works pretty well.

The only problem we have been facing is that we have to statically pass a key-value object to externals option and whenever there is a new package or a package gets removed/replaced, we need to update that externals list. We also externalize some third-party packages which belong to a namespace like @org-name and we have to regularly check that list upstream and ensure that we don't unintentionally bundle any newly created package from them.

So, sometimes we forget and it's only when we see bugs in production that we notice that list of external packages is outdated.

Thus, it would be great if we could use externals as a function which would work in the same way as rollupOptions.external and we could decide at runtime whether we want to externalize a package or not using some regex match - ^(@our-namespace|@third-party)\//.match(id)

Or another solution could be to fallback on rollupOptions.external, which we could then use to handle it dynamically.

@fengxinming
Copy link
Owner

@irshadahmad21 Can you provide a simple demo? I will use this demo to confirm after optimization

@irshadahmad21
Copy link
Author

Here is the simplest demo

https://stackblitz.com/edit/vite-external-demo?file=vite.config.ts

Instead of

createExternal({
  externals: {
    react: 'React',
    'react-dom': 'ReactDOM',
  },
})

We would want it to be something like this

createExternal((id: string) => {

	if (id.startsWith('@wordpress/')) {
		// "@wordpress/block-editor" => "wp.blockEditor"
		// "@wordpress/components" => "wp.components"
		const variable = dashToCamelCase(id.replace('@wordpress/', ''));

		return `wp.${variable}`;
	}
	// Other packages to externalize
	switch (id) {
		case 'react':
			return 'React';
		case 'react-dom':
			return 'ReactDOM';
	}

	// returning undefined means the package should not be externalized
});
function dashToCamelCase(input: string): string {
	return input.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
}

rollup-plugin-external-globals uses the same technique.

@irshadahmad21
Copy link
Author

Any update on 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

No branches or pull requests

2 participants