Code Monkey home page Code Monkey logo

demo-ember-vite's Introduction

Ember Vite

Vite logo Ember.js logo

This is Ember application running only on Vite. Netlify Status

  • It's TypeScript'ed by default.
  • It uses vite to build the app.
  • tailwind used for styling.
  • playwright used for testing.

Table of Contents

Description

Main difference with classic Ember application is that we don't trying to automatically resolve addons and it's dependencies - we just import them directly, only once needed.

This application does not use ember-cli or embroider at all.

Motivation

  • ๐Ÿš€ Hot Module Replacement support
  • โšก๏ธ No complex build process
  • ๐Ÿ’ก Instant Server Start
  • ๐Ÿ› ๏ธ Rich customization features
  • ๐Ÿ“ฆ Optimized Build
  • ๐Ÿ”ฉ A lot of supported addons
  • ๐Ÿ”‘ TypeScripted by default
  • ๐Ÿ€ Playwright tests

Features

  1. Hot Module Replacement
  2. Strict mode
  3. Ember-Data support
  4. Lazy-loading for routes
  5. Template-only components
  6. Style imports from components
  7. Ember Inspector
  8. Template imports support (.gts & .gjs)
  9. Glint support
  10. Testing support (QUnit)
  11. Template linting
  12. Playwright testing

Demo

  1. Production App on Netlify
  2. Development environment on Stackblitz

Installation

yarn install
yarn dev
# open http://localhost:4200

In scope of this app we able to run this addons

  1. ember-simple-auth
  2. ember-bootstrap
  3. ember-concurrency
  4. ember-intl
  5. ember-concurrency-decorators
  6. ember-render-modifiers
  7. ember-truth-helpers
  8. ember-basic-dropdown
  9. ember-power-select
  10. ember-style-modifier
  11. ember-assign-helper
  12. ember-element-helper
  13. ember-page-title
  14. ember-notify
  15. ember-ref-bucket
  16. ember-modal-dialog
  17. ember-responsive
  18. ember-event-helpers

(see code for samples)

This is not complete list, and you could add any ember addon you want (if it don't have ember-cli logic)

Why?

I would like to bulletproof opinion, that modern ember application could be statically resolvable by default, and I would like to use Vite for that. It give as ability to use TypeScript and ESM by default, and it's very fast.

Disclaimer

I'm not planning to actively maintain this repo, but if you have any questions, feel free to ask.

In addition, if you looking for options to improve speed of your ember project and you open for contracts - don't shy to contact me.


PR's are welcome.


How to add new addon?

  1. Install dependency yarn add addon-name.
  2. Create addon-name.ts file in src/addons folder.
  3. Import needed helpers, modifiers, components, services from addon-name (check samples in same folder).

We should keep extensions while importing, don't forget to check correct path's from node_modules/addon-name folder.

Do not forget to setComponentTemplate for components.

import SayHello from 'addon-name/components/my-component.js';
import SayHelloTemplate from 'addon-name/templates/my-component.hbs';
import CalcHelper from 'addon-name/helpers/calc.js';
import SummaryModifier from 'addon-name/modifiers/summary.js';
import SomeService from 'addon-name/services/some-service.js';
import { setComponentTemplate } from '@glimmer/manager';

setComponentTemplate(SayHelloTemplate, SayHello);
  1. Create registry object for addon (check samples in same folder)
const registry = {
     'component:say-hello': SayHello,
     'helper:calc': CalcHelper,
     'modifier:summary': SummaryModifier,
     'service:some-service': SomeService,
}
  1. Export registry object in addon-name.ts file
export default registry;
  1. Import created registry object in src/addons/index.ts file
import AddonName from './addon-name';

const registry = {
     // ... other addons
     ...AddonName,
}

Now we have new addon in our project. It should work out of the box for classic ember components. If you need to use it from gts / gjs files - you should import it as classic dependency inside gts / gjs file.

Note: If you have aliasing / babel problems - add new Addon to vite.config.ts file (check samples in same file)

As we see, it's quite easy to redefine any part of addon, including component name, and it should be an easy way to fix possible breakage just overriding template / component with file from src folder.

If addon has more complex logic, we also have few samples:

  1. If addon define new registry namespace, search for ember-responsive, ember-intl mentions in codebase.
  2. If addon provide custom babel / handlebars plugins, search for ember-ref-bucket, ember-concurrency mentions in codebase.
  3. If addon provide custom styles, search for ember-bootstrap, ember-modal-dialog mentions in codebase.

Licence

MIT

demo-ember-vite's People

Contributors

kaermorchen avatar lifeart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

demo-ember-vite's Issues

add sample code with global modifier

we need to have sample application-wide modifier, in our codebase

Modifier could listen for body clicks and show message if user clicked inside or outside node.

Helper should be located in src/modifiers/click-tracker.ts
It should be registered in registry as modifier:click-tracker

We could update main route template to use it.

add `ember-simple-auth` to app

We need to add ember-simple-auth app.

  • App should have lazy-loaded private profile route.
  • App should have login route with Login component.
  • App should have logout route to log-out user (if route visited) and redirect it to main route.

ember-inspector support

At the moment, if we expose global ember, ember-inspector fails with:

image

It's semi-expected, because we trying to don't use amd logic from ember.

To reproduce issue find setupApplicationGlobals function and assing window.Ember instead of window._Ember

Hot reload route templates

Extending #54

Some refs about old implementations: https://github.com/lifeart/ember-ast-hot-load/blob/master/addon/services/hot-loader.js#L148

But at the moment, renderTemplate is not available https://github.com/emberjs/ember.js/pull/19659/files#diff-d933badede0c63e738ba4ef11ac2b7f9b4aa04b40cee6b8d71f97d0ba9a2ce63.

It seems we need to create wrapper for registry to handle it (now)

We could hook into register method of application and wrap route templates.

  register(
    key: `${string}:${string}`,
    value: object | any,
    options?: any
  ): void {
    if (key.startsWith('template:')) {
      // hot-reload hook
      console.log('hot-reload', key, value);
    }
    return super.register(key, value, options);
  }

But, it seems (in general), we need more metadata for props forwarding.

Hot wrapper may looks like:

<Hot @component={{template}} as |Template|>
    <Template @model={{@model}} />
</Hot>

In short, we need kinda props forwarding. And we have to collect it from original template. (@model, this....), but, this is not rally forward-able.

to reload route we could try to use refresh key
https://github.com/mixonic/ember.js/blob/8547049f81ec0f0928bc5786e9687f86f0f54f1a/packages/%40ember/-internals/routing/lib/system/route.ts#L1034

basic addons support

to support basic ember addons, we need 2 things

  • land #4 to be able to resolve hbs files
  • import addon components/services/routes/controllers into registry (if not used in strict mode)
  • if addon is used in strict mode, and don't rely on ember resolution, registry registration is not nessesary
  • support decorators compilation from node_modules path

how it should look like for global app usage

/* src/config/registry.ts */

import CalendarTemplate from 'ember-power-calendar/addon/components/calendar/template.hbs';
import CalendarComponent from 'ember-power-calendar/addon/components/calendar/component';

function registry(): IRegistry {
    return {
        'component:calendar': setComponentTemplate(CalendarComponent,  CalendarTemplate),
    };
}

how it should look like for scoped usage

/* src/components/calendar.ts */

import Component from "@glimmer/component";
import { precompileTemplate } from "@ember/template-compilation";
import CalendarTemplate from "ember-power-calendar/addon/components/calendar/template.hbs";
import CalendarComponent from "ember-power-calendar/addon/components/calendar/component";

export default class Calendar extends Component {
  static template = precompileTemplate(
    `
       <CalendarComponent @theme="dark" />
    `,
    {
      isStrictMode: true,
      scope: () => ({
        CalendarComponent,
      }),
    }
  );
}

glint integration

it will be cool if we able to add glint here and fix found typings issues

add sample code with global helpers

we need to have sample application-wide helper, in our codebase

Helper could show current page memory and update every second

Helper should be located in src/helpers/memory-usage.ts
It should be registered in registry as helper:memory-usage

We could update main route template to use it.

CLI

Right now, all project files are created manually. It is not friendly. We need a cli tool. This is not a nearby issue; we need to create it when all file structures and concepts will be stable. But some suggestions now can reduce refactoring in the future.

The goal is to reduce amount of manual work for repeated actions like to generate a new route or component (as @lifeart have suggested in #30)

My thought:

  • the commands should be similar ember-cli
  • the project file structure shouldn't be hardcoded
  • be as simple as possible, but has a modular structure, and anyone can simply extend logic, override settings and create new commands in the project.
  • should be out of the box. It can be an external package, (may be in the monorepo with this demo), it allows to execute it from node_modules/bin/ or npx

Any other ideas?

Playwright test integration

Let's assume we would like to run tests without "ember" magic, just have same exp as our application users.

For this purpose, we have to add proper playwright setup:

  • Create playwright config
  • Add sample e2e tests
  • Verify it's working on ci
  • Process coverage information from tests #50
  • Add code coverage reporter on ci #45
  • Capture test coverage from playwright 27fce1d

Add github CI integration

It will be great to have github CI integration to do in parallel:

  1. Linting files
  2. Having yarn build without errors

add `ember-intl` to app

Sample yaml handing from vue: https://www.npmjs.com/package/@vitejs/plugin-vue
Or https://github.com/Modyfi/vite-plugin-yaml

import vue from '@vitejs/plugin-vue'
import yaml from 'js-yaml'

const vueI18nPlugin = {
  name: 'vue-i18n',
  transform(code, id) {
    if (!/vue&type=i18n/.test(id)) {
      return
    }
    if (/\.ya?ml$/.test(id)) {
      code = JSON.stringify(yaml.load(code.trim()))
    }
    return `export default Comp => {
      Comp.i18n = ${code}
    }`
  },
}

export default {
  plugins: [vue(), vueI18nPlugin],
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.