Skip to content

Conversation

@mho22
Copy link
Collaborator

@mho22 mho22 commented Dec 9, 2025

Motivation for the change, related issues

Based on #2999

Intl dynamic extension added a third file where its import had to be ignored : icu.dat. The pull request created a third preserve-data-loaders-imports Vite plugin. This pull request aims to regroup every preserve-{extension}-loaders-imports into a unique Vite extension.

This pull request is more a suggestion than its ignore-*-imports counterpart.

Implementation details

Regrouping repeatedly copy pasted vite plugins inside a vite extension file named `vite-preserve-loaders-imports.ts.

Testing Instructions

CI - test-built-npm-packages should not fail

@mho22 mho22 mentioned this pull request Dec 9, 2025
2 tasks
{
regex: /php_\d_\d\.js$/,
transform: (specifier) =>
`../${specifier.split('/').slice(-3).join('/')}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not easy to parse just by looking at it, let's document these with examples of paths we're targeting with this expression

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. But I don't know if this became more understandable...

{
regex: /intl\.so$/,
transform: (specifier) =>
`../../../${specifier.split('/').slice(-6).join('/')}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

{
regex: /icu\.dat$/,
transform: (specifier) =>
`../../../${specifier.split('/').slice(-2).join('/')}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

if (command !== 'build' || typeof specifier !== 'string') return;

for (const rule of rules) {
if (rule.regex.test(specifier)) {
Copy link
Collaborator

@adamziel adamziel Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would it take to introduce a poka-yoke and throw an error when a rule matches zero specifiers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a const matchedRules = new Set<PreserveLoadersRule>(); and add a matching rule :

if (rule.regex.test(specifier)) {
	matchedRules.add(rule);
...

And during buildEnd we could throw an error if a rule has not been specified :

buildEnd() {
	if (command !== 'build') return;

	const unusedRules = rules.filter(rule => !matchedRules.has(rule));

	if (unusedRules.length > 0) {
		const details = unusedRules
			.map((rule) => `- ${rule.regex}`)
			.join('\n');

		this.error(
			`vite-preserve-loaders-imports: The following rules did not match any dynamic imports:\n${details}\n\n` +
			`This is likely a misconfiguration or a stale regex.`
		);
	}
},

But I think if some regex were not correct Vite would crash anyway.

Copy link
Collaborator

@adamziel adamziel Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably would! Still, having an explicit line of defense is always reassuring – we can stay resilient even if something implicit changes and we never realize. Let's add that buildEnd() logic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! Thank you Adam.

@mho22 mho22 force-pushed the create-vite-preserve-loaders-imports branch from bec8e01 to 921dfb0 Compare December 9, 2025 14:41
* web/src/lib/get-php-loader-module.ts > web/src/get-php-loader-module.ts
*
* slice(-3) strips the `public` directory from the path
* web/public/php/jspi/php_8_4.js > ./web/php/jspi/php_8_4.js
Copy link
Collaborator

@adamziel adamziel Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"web/public/php/jspi/php_8_4.js".split('/').slice(-3).join('/')

yields php/jspi/php_8_4.js, not ./web/php/jspi/php_8_4.js. Let's double check all the examples are accurate

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exactly proves why these comments are important 😄. I corrected them and added a "resulting in" paragraph.

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants