ember-cli / ember-load-initializers

MIT License
14 stars 37 forks source link

Stop using `require` to load initializers, pass the modules instead #305

Open BlueCutOfficial opened 2 months ago

BlueCutOfficial commented 2 months ago

📝 This is a draft PR to stop using require to load initializers, it proposes an implementation but if we want to merge it at some point we'll also have to clean up the types.

Context: This PR was drafted in the context of the Embroider Initiative. At some point, we thought we would need it to support initializers when building with Embroider + Vite. It turns out we didn't have to go that far to get initializers work, but this PR can still be interesting in the context of Strict ES Module Support RFC.

How to use: The idea is to pass the app modules to the loadInitializers function directly, with a new argument compatModules. The app.js file of an Ember app would look like this:

// my-ember-app/app/app.js
import compatModules from '@embroider/core/entrypoint';

// Define modules imported from Embroider internals
let d = window.define;
for (const [name, module] of Object.entries(compatModules)) {
  d(name, function () {
    return module;
  });
}

export default class App extends Application { /* modulePrefix = ... */ }

// Pass modules to loadInitializers
loadInitializers(App, config.modulePrefix, compatModules);