diff --git a/packages/core/src/virtual-test-support-styles.ts b/packages/core/src/virtual-test-support-styles.ts index 240aeaf67..0afd88b16 100644 --- a/packages/core/src/virtual-test-support-styles.ts +++ b/packages/core/src/virtual-test-support-styles.ts @@ -1,4 +1,9 @@ import type { Package } from '@embroider/shared-internals'; +import type { V2AddonPackage } from '@embroider/shared-internals/src/package'; +import { readFileSync } from 'fs'; +import { sortBy } from 'lodash'; +import resolve from 'resolve'; +import type { Engine } from './app-files'; import type { Resolver } from './module-resolver'; import type { VirtualContentResult } from './virtual-content'; @@ -15,7 +20,51 @@ export function renderTestSupportStyles(filename: string, resolver: Resolver): V } function getTestSupportStyles(owner: Package, resolver: Resolver): string { - console.log(owner); - console.log(resolver); - return `h1 { color: white; }`; + let engineConfig = resolver.owningEngine(owner); + let engine: Engine = { + package: owner, + addons: new Map( + engineConfig.activeAddons.map(addon => [ + resolver.packageCache.get(addon.root) as V2AddonPackage, + addon.canResolveFromFile, + ]) + ), + isApp: true, + modulePrefix: resolver.options.modulePrefix, + appRelativePath: 'NOT_USED_DELETE_ME', + }; + + return generateTestSupportStyles(engine); +} + +function generateTestSupportStyles(engine: Engine): string { + let result: string[] = impliedAddonTestSupportStyles(engine).map((sourcePath: string): string => { + let source = readFileSync(sourcePath); + return `${source}`; + }); + + return result.join('') as string; +} + +function impliedAddonTestSupportStyles(engine: Engine): string[] { + let result: Array = []; + for (let addon of sortBy(Array.from(engine.addons.keys()), pkg => { + switch (pkg.name) { + case 'loader.js': + return 0; + case 'ember-source': + return 10; + default: + return 1000; + } + })) { + let implicitStyles = addon.meta['implicit-test-styles']; + if (implicitStyles) { + let options = { basedir: addon.root }; + for (let mod of implicitStyles) { + result.push(resolve.sync(mod, options)); + } + } + } + return result; }