Migrate from tsup (deprecated) to tsdown

tsup is deprecated in favor of tsdown.

Follow https://github.com/symfony/ux/pull/2935, https://github.com/symfony/ux/pull/2944, and many (local) tries were I was not really happy with the files generated by tsdown/rolldown, we are finally having something extra good!

We have the benefits from https://github.com/symfony/ux/pull/2935, https://github.com/symfony/ux/pull/2944, but without their drawbacks. The code correctly follow the `es2022` target and does not do anything weird anymore with static properties (needed by controllers).

I (Claude) added a plugin to remove the region and JSDoc comments (except if they contain `@deprecated``), since I think we want to keep the code non-minified.
This commit is contained in:
Hugo Alliaume
2026-02-28 08:36:49 +01:00
parent 0401477482
commit 90640ce587
4 changed files with 69 additions and 107 deletions

View File

@@ -1,15 +1,13 @@
import { ControllerConstructor } from '@hotwired/stimulus';
import { ControllerConstructor } from "@hotwired/stimulus";
interface EagerControllersCollection {
[key: string]: ControllerConstructor;
[key: string]: ControllerConstructor;
}
interface LazyControllersCollection {
[key: string]: () => Promise<{
default: ControllerConstructor;
}>;
[key: string]: () => Promise<{
default: ControllerConstructor;
}>;
}
declare const eagerControllers: EagerControllersCollection;
declare const lazyControllers: LazyControllersCollection;
declare const isApplicationDebug = false;
export { type EagerControllersCollection, type LazyControllersCollection, eagerControllers, isApplicationDebug, lazyControllers };
export { EagerControllersCollection, LazyControllersCollection, eagerControllers, isApplicationDebug, lazyControllers };

View File

@@ -1,8 +1,4 @@
const eagerControllers = {};
const lazyControllers = {};
const isApplicationDebug = false;
export {
eagerControllers,
isApplicationDebug,
lazyControllers
};
export { eagerControllers, isApplicationDebug, lazyControllers };

View File

@@ -1,7 +1,5 @@
import { Application } from '@hotwired/stimulus';
import { EagerControllersCollection, LazyControllersCollection } from './controllers.js';
import { EagerControllersCollection, LazyControllersCollection } from "./controllers.js";
import { Application } from "@hotwired/stimulus";
declare const loadControllers: (application: Application, eagerControllers: EagerControllersCollection, lazyControllers: LazyControllersCollection) => void;
declare const startStimulusApp: () => Application;
export { loadControllers, startStimulusApp };
export { loadControllers, startStimulusApp };

148
assets/dist/loader.js vendored
View File

@@ -1,100 +1,70 @@
import { eagerControllers, isApplicationDebug, lazyControllers } from "./controllers.js";
import { Application } from "@hotwired/stimulus";
import {
eagerControllers,
isApplicationDebug,
lazyControllers
} from "./controllers.js";
const controllerAttribute = "data-controller";
const loadControllers = (application, eagerControllers2, lazyControllers2) => {
for (const name in eagerControllers2) {
registerController(name, eagerControllers2[name], application);
}
const lazyControllerHandler = new StimulusLazyControllerHandler(
application,
lazyControllers2
);
lazyControllerHandler.start();
const loadControllers = (application, eagerControllers, lazyControllers) => {
for (const name in eagerControllers) registerController(name, eagerControllers[name], application);
new StimulusLazyControllerHandler(application, lazyControllers).start();
};
const startStimulusApp = () => {
const application = Application.start();
application.debug = isApplicationDebug;
loadControllers(application, eagerControllers, lazyControllers);
return application;
const application = Application.start();
application.debug = isApplicationDebug;
loadControllers(application, eagerControllers, lazyControllers);
return application;
};
var StimulusLazyControllerHandler = class {
constructor(application, lazyControllers) {
this.application = application;
this.lazyControllers = lazyControllers;
}
start() {
this.lazyLoadExistingControllers(document.documentElement);
this.lazyLoadNewControllers(document.documentElement);
}
lazyLoadExistingControllers(element) {
Array.from(element.querySelectorAll(`[${controllerAttribute}]`)).flatMap(extractControllerNamesFrom).forEach((controllerName) => {
this.loadLazyController(controllerName);
});
}
loadLazyController(name) {
if (!this.lazyControllers[name]) return;
const controllerLoader = this.lazyControllers[name];
delete this.lazyControllers[name];
if (!canRegisterController(name, this.application)) return;
this.application.logDebugActivity(name, "lazy:loading");
controllerLoader().then((controllerModule) => {
this.application.logDebugActivity(name, "lazy:loaded");
registerController(name, controllerModule.default, this.application);
}).catch((error) => {
console.error(`Error loading controller "${name}":`, error);
});
}
lazyLoadNewControllers(element) {
if (Object.keys(this.lazyControllers).length === 0) return;
new MutationObserver((mutationsList) => {
for (const { attributeName, target, type } of mutationsList) switch (type) {
case "attributes":
if (attributeName === controllerAttribute && target.getAttribute(controllerAttribute)) extractControllerNamesFrom(target).forEach((controllerName) => {
this.loadLazyController(controllerName);
});
break;
case "childList": this.lazyLoadExistingControllers(target);
}
}).observe(element, {
attributeFilter: [controllerAttribute],
subtree: true,
childList: true
});
}
};
class StimulusLazyControllerHandler {
constructor(application, lazyControllers2) {
this.application = application;
this.lazyControllers = lazyControllers2;
}
start() {
this.lazyLoadExistingControllers(document.documentElement);
this.lazyLoadNewControllers(document.documentElement);
}
lazyLoadExistingControllers(element) {
Array.from(element.querySelectorAll(`[${controllerAttribute}]`)).flatMap(extractControllerNamesFrom).forEach((controllerName) => {
this.loadLazyController(controllerName);
});
}
loadLazyController(name) {
if (!this.lazyControllers[name]) {
return;
}
const controllerLoader = this.lazyControllers[name];
delete this.lazyControllers[name];
if (!canRegisterController(name, this.application)) {
return;
}
this.application.logDebugActivity(name, "lazy:loading");
controllerLoader().then((controllerModule) => {
this.application.logDebugActivity(name, "lazy:loaded");
registerController(name, controllerModule.default, this.application);
}).catch((error) => {
console.error(`Error loading controller "${name}":`, error);
});
}
lazyLoadNewControllers(element) {
if (Object.keys(this.lazyControllers).length === 0) {
return;
}
new MutationObserver((mutationsList) => {
for (const { attributeName, target, type } of mutationsList) {
switch (type) {
case "attributes": {
if (attributeName === controllerAttribute && target.getAttribute(controllerAttribute)) {
extractControllerNamesFrom(target).forEach((controllerName) => {
this.loadLazyController(controllerName);
});
}
break;
}
case "childList": {
this.lazyLoadExistingControllers(target);
}
}
}
}).observe(element, {
attributeFilter: [controllerAttribute],
subtree: true,
childList: true
});
}
}
function registerController(name, controller, application) {
if (canRegisterController(name, application)) {
application.register(name, controller);
}
if (canRegisterController(name, application)) application.register(name, controller);
}
function extractControllerNamesFrom(element) {
const controllerNameValue = element.getAttribute(controllerAttribute);
if (!controllerNameValue) {
return [];
}
return controllerNameValue.split(/\s+/).filter((content) => content.length);
const controllerNameValue = element.getAttribute(controllerAttribute);
if (!controllerNameValue) return [];
return controllerNameValue.split(/\s+/).filter((content) => content.length);
}
function canRegisterController(name, application) {
return !application.router.modulesByIdentifier.has(name);
return !application.router.modulesByIdentifier.has(name);
}
export {
loadControllers,
startStimulusApp
};
export { loadControllers, startStimulusApp };