From c2a48f9099fa01ca1876e05e88f728b3e2d344ad Mon Sep 17 00:00:00 2001 From: Chris Manson Date: Tue, 21 Jan 2025 10:35:55 +0000 Subject: [PATCH] implement broccoli builder for compatPrebuild --- packages/compat/src/default-pipeline.ts | 8 ++++++++ packages/compat/src/index.ts | 2 +- packages/vite/index.ts | 1 + packages/vite/package.json | 1 + packages/vite/src/broccoli-builder.ts | 16 ++++++++++++++++ pnpm-lock.yaml | 3 +++ tests/app-template/ember-cli-build.js | 7 ++++--- 7 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 packages/vite/src/broccoli-builder.ts diff --git a/packages/compat/src/default-pipeline.ts b/packages/compat/src/default-pipeline.ts index 0d901d3d2..867c5ff03 100644 --- a/packages/compat/src/default-pipeline.ts +++ b/packages/compat/src/default-pipeline.ts @@ -35,3 +35,11 @@ export function prebuild(emberApp: EmberAppInstance, options?: Options): Node { return mergeTrees([embroiderApp.asStage(addons).tree, writeFile('.stage2-output', () => outputPath)]); } + +export function compatBuild(emberApp: EmberAppInstance, Builder: any, options?: Options): Node { + if (process.env.EMBROIDER_PREBUILD) { + return prebuild(emberApp, options); + } + + return new Builder([]); +} diff --git a/packages/compat/src/index.ts b/packages/compat/src/index.ts index de84f3eb4..fd05a8e41 100644 --- a/packages/compat/src/index.ts +++ b/packages/compat/src/index.ts @@ -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'; diff --git a/packages/vite/index.ts b/packages/vite/index.ts index 7025b6548..a011d52b3 100644 --- a/packages/vite/index.ts +++ b/packages/vite/index.ts @@ -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/broccoli-builder.js'; diff --git a/packages/vite/package.json b/packages/vite/package.json index c11cf46ee..807588eca 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -21,6 +21,7 @@ "@embroider/reverse-exports": "workspace:*", "@rollup/pluginutils": "^5.1.0", "assert-never": "^1.2.1", + "broccoli-plugin": "^4.0.7", "browserslist": "*", "browserslist-to-esbuild": "^2.1.1", "content-tag": "^2.0.2", diff --git a/packages/vite/src/broccoli-builder.ts b/packages/vite/src/broccoli-builder.ts new file mode 100644 index 000000000..d217331f8 --- /dev/null +++ b/packages/vite/src/broccoli-builder.ts @@ -0,0 +1,16 @@ +import Plugin from 'broccoli-plugin'; +import { spawn } from 'child_process'; + +export class Builder extends Plugin { + build(): Promise { + return new Promise((resolve, reject) => { + const child = spawn(`npx vite build --outDir ${this.outputPath}`, { + cwd: process.cwd(), + shell: true, + stdio: 'inherit', + env: { ...process.env, FORCE_BUILD_TESTS: 'true' }, + }); + child.on('exit', code => (code === 0 ? resolve() : reject(new Error('vite build failed')))); + }); + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 44c31def3..a6fae6a20 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1072,6 +1072,9 @@ importers: assert-never: specifier: ^1.2.1 version: 1.4.0 + broccoli-plugin: + specifier: ^4.0.7 + version: 4.0.7 browserslist: specifier: ^4.14.0 version: 4.24.4 diff --git a/tests/app-template/ember-cli-build.js b/tests/app-template/ember-cli-build.js index ebb76e53a..eb602ced1 100644 --- a/tests/app-template/ember-cli-build.js +++ b/tests/app-template/ember-cli-build.js @@ -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 { Builder } = await import('@embroider/vite'); let app = new EmberApp(defaults, {}); - return maybeEmbroider(app); + return compatBuild(app, Builder); };