Skip to content

Commit

Permalink
Merge pull request #2233 from embroider-build/builder-api
Browse files Browse the repository at this point in the history
Add a builder API to @embroider/compat to support ember commands like `ember test`
  • Loading branch information
ef4 authored Jan 21, 2025
2 parents 1c0f616 + 961cea4 commit d346bd1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
19 changes: 19 additions & 0 deletions packages/compat/src/default-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Variant, EmberAppInstance } from '@embroider/core';
import type { Node } from 'broccoli-node-api';
import writeFile from 'broccoli-file-creator';
import mergeTrees from 'broccoli-merge-trees';
import Plugin from 'broccoli-plugin';

export interface PipelineOptions<PackagerOptions> extends Options {
packagerOptions?: PackagerOptions;
Expand Down Expand Up @@ -35,3 +36,21 @@ export function prebuild(emberApp: EmberAppInstance, options?: Options): Node {

return mergeTrees([embroiderApp.asStage(addons).tree, writeFile('.stage2-output', () => outputPath)]);
}

export function compatBuild(
emberApp: EmberAppInstance,
buildOnce: (outputPath: string, emberEnv: 'development' | 'test' | 'production') => Promise<void>,
options?: Options
): Node {
if (process.env.EMBROIDER_PREBUILD) {
return prebuild(emberApp, options);
}

class Builder extends Plugin {
build(): Promise<void> {
return buildOnce(this.outputPath, emberApp.env);
}
}

return new Builder([], {});
}
2 changes: 1 addition & 1 deletion packages/compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export { default as App } from './compat-app';
export { default as Addons } from './compat-addons';
export { default as Options, recommendedOptions } from './options';
export { default as V1Addon } from './v1-addon';
export { prebuild, PipelineOptions } from './default-pipeline';
export { prebuild, compatBuild, PipelineOptions } from './default-pipeline';
export { PackageRules, ModuleRules } from './dependency-rules';
export type { Options as ResolverTransformOptions } from './resolver-transform';
1 change: 1 addition & 0 deletions packages/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './src/assets.js';
export * from './src/content-for.js';
export * from './src/classic-ember-support.js';
export * from './src/ember.js';
export * from './src/build-once.js';
16 changes: 16 additions & 0 deletions packages/vite/src/build-once.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { spawn } from 'child_process';

export function buildOnce(outputPath: string, emberEnv: 'development' | 'test' | 'production'): Promise<void> {
return new Promise((resolve, reject) => {
const child = spawn(
`npx vite build --outDir ${outputPath} --mode ${emberEnv === 'production' ? 'production' : 'development'}`,
{
cwd: process.cwd(),
shell: true,
stdio: 'inherit',
env: { ...process.env },
}
);
child.on('exit', code => (code === 0 ? resolve() : reject(new Error('vite build failed'))));
});
}
7 changes: 4 additions & 3 deletions tests/app-template/ember-cli-build.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const { maybeEmbroider } = require('@embroider/test-setup');
const { compatBuild } = require('@embroider/compat');

module.exports = function (defaults) {
module.exports = async function (defaults) {
const { buildOnce } = await import('@embroider/vite');
let app = new EmberApp(defaults, {});

return maybeEmbroider(app);
return compatBuild(app, buildOnce);
};

0 comments on commit d346bd1

Please sign in to comment.