|
| 1 | +# Embroider Vite/Webpack |
| 2 | + |
| 3 | +As of v0.13.0, Ember Engines is compatible with the new default Ember build system based on [Vite](https://vite.dev) and [Embroider](https://github.com/embroider-build/embroider). For your engines to work with Embroider you will need to make sure you are suing at least `ember-resolver@13.0.0` and you will need to update your `engine.js` entrypoint to pass `compatModules` to the resolver. |
| 4 | + |
| 5 | +This method of using the `ember-resolver` with `compatModules` is usually not compatible with the Classic build system but `ember-engines` automatically provides a polyfill so that an engine that has been updated to work with Embroider+Vite will continue to work with a Classic app that is still using `ember-cli` for its build. |
| 6 | + |
| 7 | +## How to update your `engine.js` entrypoint to support Embroider+Vite |
| 8 | + |
| 9 | +To support Embroider+Vite you need to first import `compatModules` from the virtual entrypoint `@embroider/virtual/compat-modules` and then pass it to your resolver using the `withModules()` function. You also need to pass `compatModules` to the `loadInitializers()` function. |
| 10 | + |
| 11 | +```diff |
| 12 | +// addon/engine.js |
| 13 | +import Engine from 'ember-engines/engine'; |
| 14 | +import Resolver from 'ember-resolver'; |
| 15 | +import loadInitializers from 'ember-load-initializers'; |
| 16 | +import config from './config/environment'; |
| 17 | ++ import compatModules from '@embroider/virtual/compat-modules'; |
| 18 | + |
| 19 | +const { modulePrefix } = config; |
| 20 | + |
| 21 | +class Eng extends Engine { |
| 22 | +modulePrefix = config.modulePrefix; |
| 23 | +- Resolver = Resolver; |
| 24 | ++ Resolver = Resolver.withModules(compatModules); |
| 25 | +} |
| 26 | + |
| 27 | +- loadInitializers(Eng, modulePrefix); |
| 28 | ++ loadInitializers(Eng, modulePrefix, compatModules); |
| 29 | + |
| 30 | +export default Eng; |
| 31 | +``` |
| 32 | + |
| 33 | +## Lazy Engines |
| 34 | + |
| 35 | +With the new Embroider+Vite build system you are required to use [`@embroider/router`](https://github.com/embroider-build/embroider/blob/main/packages/router/) as a replacement for your regular Ember Router if you want to maintain the behaviour of your Lazy Engines. All bundle splitting is handled by Embroider and the `@embroider/router` controls loading bundles as you navigate between routes that may have been split by the build, or a lazy engine that may be mounted on a particular route. |
| 36 | + |
| 37 | +The `@embroider/router` is the default for new applications but if you are upgrading an existing app you will need to update your router to use the new drop-in replacement: |
| 38 | + |
| 39 | +```diff |
| 40 | +// <your app>/app/router.js |
| 41 | +-import EmberRouter from '@ember/routing/router'; |
| 42 | ++import EmberRouter from '@embroider/router'; |
| 43 | +``` |
| 44 | + |
| 45 | +## Legacy Embroider |
| 46 | + |
| 47 | +Before Embroider+Vite became the default for newly generated applications, there was a previous Embroider version that used a mixture of a classic build (with AMD) and Webpack. This version of Embroider is no longer being actively maintained so anyone still using it should actively try to upgrade to Embroider+Vite. |
| 48 | + |
| 49 | +Since `ember-engines` is providing an automatic polyfill that supports classic builds, that same polyfill allows Engines that have been updated to work with Vite to work with the legacy Emberoider@3 build system that relies on Webpack. |
0 commit comments