Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 39 ยท Build, Addons and Deployment

Ember CLI and the Build Pipeline

Learn the Ember CLI commands you use daily, the layout of an Ember project, environment configuration, how a production build works, and Embroider with Vite.

Ember apps are built and run with Ember CLI, a command-line tool that creates projects, generates code, serves the app with live reload, runs tests and produces the production build. The CLI is one of Ember's defining features: every Ember project has the same structure and the same commands, so you can open an app you have never seen and know where things are.

This lesson covers the commands, the project layout, how configuration works per environment, what a production build does to your code, and how the build tooling has moved to Embroider and Vite in recent versions. Because the site cannot run Ember or its toolchain, code is shown read-only and the runnable stand-ins model the build steps in plain JavaScript.

Real Ember vs stand-in. Command lines and file-named blocks are real and read-only. Runnable blocks are small models of what the build does.

The commands you will use

Command Purpose
npx ember-cli new my-app create a project (or ember new my-app with the CLI installed)
ember serve (alias ember s) build in development and serve at http://localhost:4200, rebuilding on change
ember serve --port 4300 use another port
ember generate component post-card (alias ember g) create a component and its test
ember generate route posts create a route, template, test and a router entry
ember generate service session create a service and a unit test
ember generate helper format-price create a helper
ember destroy component post-card remove what generate created
ember test build and run the test suite
ember build --environment=production create an optimized build in dist/
ember install some-addon install an addon and run its set-up
ember help generate list generators and their options

Newer projects expose the same ideas through package scripts, so npm start, npm test and npm run build also work. Generators are the biggest day-to-day time-saver: they create files in the right place with the right name, and they update the router when needed, so you never wonder where a file should go.

Useful generator flags

  • --pod or --in-repo-addon change layout choices (pods are rarely used now).
  • ember g component x --typescript and ember g component x -gjs choose the file format when the project supports it.
  • ember g --dry-run component x prints what would be created without writing files.

The project layout

Text
my-app/
  app/
    components/      one file per component (.gjs/.gts or .js + .hbs)
    routes/          route classes
    templates/       route templates
    services/        services
    helpers/         template helpers
    modifiers/       element modifiers
    models/          Ember Data models
    adapters/        data adapters
    serializers/     data serializers
    router.js        the route map
    app.js           the application class
    index.html       the HTML shell
    styles/app.css
  config/
    environment.js   per-environment settings
    targets.js       browsers to support
  public/            copied as-is: images, robots.txt, favicons
  tests/             unit, integration, acceptance, helpers
  ember-cli-build.js (classic) or vite.config.mjs (Vite)
  package.json

Everything under app/ is turned into modules, and the resolver maps file paths to the lookup names (app/services/cart.js is service:cart) described in Dependency Injection and the Owner. The folder is the convention; you do not register anything.

Environment configuration

Settings live in config/environment.js and differ per environment. The function receives the environment name and returns an object:

config/environment.js

JavaScript
'use strict';

module.exports = function (environment) {
  const ENV = {
    modulePrefix: 'my-app',
    environment,
    rootURL: '/',
    locationType: 'history',
    EmberENV: {},
    APP: {
      apiHost: 'http://localhost:3000',
    },
  };

  if (environment === 'test') {
    ENV.locationType = 'none';
    ENV.APP.autoboot = false;
    ENV.APP.apiHost = '';
  }

  if (environment === 'production') {
    ENV.APP.apiHost = 'https://api.example.com';
  }

  return ENV;
};

Your code reads it with an import (import config from 'my-app/config/environment'; config.APP.apiHost). Three settings deserve attention:

  • rootURL: the base path the app is served under (/ for a domain root, /my-app/ for a subfolder).
  • locationType: history gives clean URLs (needs server support, see Deploying); hash uses #/path and works anywhere; none is for tests.
  • Anything in this file ends up in public JavaScript. Never put secrets in it; the browser can read all of it.

The model below mimics how the config function adapts to each environment, which makes the idea concrete.

JS Stand-in: environment-specific config

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What a production build does

ember build --environment=production takes your source and prepares it for real users:

  1. Transpile modern syntax and decorators with Babel for the browsers listed in config/targets.js.
  2. Compile templates to JavaScript, so no template parsing happens in the browser.
  3. Bundle modules and tree-shake unused exports.
  4. Minify JavaScript and CSS.
  5. Fingerprint file names (a hash of the content in the name, such as app-3f9a1c.js) and rewrite references.
  6. Write everything to dist/, ready for any static host.

Fingerprinting is what makes long-lived caching safe: a changed file gets a new name, so users never run a mismatched old and new pair. The following model runs steps 4 and 5 on tiny sources with Node's crypto.

Node.js Stand-in: minify and fingerprint

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Run it twice and the names are identical, because the hash depends only on the content. Change one character in the source and the name changes.

Embroider and Vite

Older Ember apps used a custom build system (Broccoli, configured in ember-cli-build.js). Modern Ember uses Embroider, which treats an app as an ordinary package of ES modules, so standard tools can build it. New apps use Vite: a vite.config.mjs with the Ember plugins, a fast dev server with hot module replacement, and the usual Vite production build.

What this means in practice:

  • ember serve / npm start starts the Vite dev server, which loads modules on demand and starts very fast.
  • Imports are resolved like any modern JavaScript project: a dependency you install with npm can be imported.
  • Addons in the v2 format are plain packages the bundler understands; older v1 addons are adapted automatically.
  • Features such as code splitting by route (splitAtRoutes in Embroider) can load rarely used sections lazily, shrinking the first download.

If you maintain an older app, the migration path is incremental (move from classic to Embroider "compat" mode, then to Vite). The application code you write, components, routes and services, stays the same either way.

Browser targets

config/targets.js lists browsers the build must support, in a browserslist format:

JavaScript
'use strict';

module.exports = {
  browsers: [
    'last 1 Chrome versions',
    'last 1 Firefox versions',
    'last 1 Safari versions',
  ],
};

Broader targets mean more transpilation and bigger bundles; narrower targets give smaller, faster code. Pick the narrowest list your audience allows (and add ie-style legacy browsers only if you truly must).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command creates a service and a matching unit test?

    ember generate (alias g) creates files in the right folder with a test, using the project's conventions.
  2. 2. Why does a production build fingerprint file names?

    A new hash in the name means a changed file is never confused with a cached old one, so long cache lifetimes are safe.
  3. 3. What must you never put in config/environment.js?

    Everything in the config is shipped to the browser. Secrets belong on a server.
  4. 4. What does Embroider change for Ember developers?

    Embroider is the build system. Your components, routes and services stay the same; the way they are bundled becomes standard.

Summary

  • Use ember generate for files, ember serve for development, ember test for tests and ember build --environment=production for releases.
  • The project layout is a convention the resolver understands; no manual registration is needed.
  • config/environment.js adapts settings per environment and is public: no secrets.
  • A production build transpiles, compiles templates, bundles, minifies and fingerprints into dist/.
  • Recent apps use Embroider with Vite; your application code does not change.

Next: Ember Addons and Packages