Skip to content

To use route splitting in a vite app without compat / ember-cli #2521

@NullVoxPopuli

Description

@NullVoxPopuli

I noticed that when I extend from @embroider/router, I'm... not -- the build is still using the ember router.

Key information:

In babel.config.cjs:

const { buildMacros } = require('@embroider/macros/babel');

const macros = buildMacros({
  configure(config) {
    config.setGlobalConfig(__filename, '@embroider/core', { active: true });
  },
});

In your router.js:

// the normal part
import EmbroiderRouter from '@embroider/router';

export default class Router extends EmbroiderRouter {
  // ...
}

Router.map(function () {
  this.route('edit');
  this.route('output');

  // our split bundle
  this.route('docs', function () {
    this.route('repl-sdk');
    this.route('ember-repl');
    this.route('embedding');
    this.route('editor');
  });

  this.route('error', { path: '*' });
});


// The secret part:


function bundle(name: string, loader: () => Promise<{ default: unknown }>[]) {
  return {
    names: [name],
    load: async () => {
      const [template, route, controller] = await Promise.all(loader());
      let slashName = name.replaceAll('.', '/');
      let results: Record<string, unknown> = {};

      if (template) results[`limber/templates/${slashName}`] = template.default;
      if (route) results[`limber/routes/${slashName}`] = route.default;
      if (controller) results[`limber/controllers/${slashName}`] = controller.default;

      return {
        default: results,
      };
    },
  };
}

(window as any)._embroiderRouteBundles_ = [
  bundle('docs', () => [import('./templates/docs.gts')]),
  bundle('docs.repl-sdk', () => [import('./templates/docs/repl-sdk.gts')]),
  bundle('docs.ember-repl', () => [import('./templates/docs/ember-repl.gts')]),
  bundle('docs.embedding', () => [import('./templates/docs/embedding.gts')]),
  bundle('docs.editor', () => [import('./templates/docs/editor.gts')]),
  bundle('docs.whatever', () => [
    import('./the/template.gts'),
    import('./the/route.ts'),
    import('./the/controller.ts'),
  ]),
];

and ✨ tada ✨ you have bundle splitting without compat mode.

(I'm using this over here: NullVoxPopuli/limber#1962 )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions