diff --git a/packages/core/src/flecks.js b/packages/core/src/flecks.js index ed7409d..ee11e25 100644 --- a/packages/core/src/flecks.js +++ b/packages/core/src/flecks.js @@ -9,6 +9,7 @@ import { import get from 'lodash.get'; import set from 'lodash.set'; +import compose from './compose'; import D from './debug'; import Digraph from './digraph'; import Middleware from './middleware'; @@ -374,6 +375,20 @@ export default class Flecks { return graph; } + /** + * Create a mixed instance of flecks. + * @param {Object} config Configuration. + * @returns {Flecks} A flecks instance. + */ + static from(config) { + const {flecks} = config; + const mixins = Object.entries(flecks) + .map(([, M]) => M.hooks?.['@flecks/core.mixin']) + .filter((e) => e); + const Flecks = compose(...mixins)(this); + return new Flecks(config); + } + /** * Gather and register class types. * diff --git a/packages/core/src/server/flecks.js b/packages/core/src/server/flecks.js index 8a09dbf..28d0514 100644 --- a/packages/core/src/server/flecks.js +++ b/packages/core/src/server/flecks.js @@ -19,7 +19,6 @@ import enhancedResolve from 'enhanced-resolve'; import {dump as dumpYml, load as loadYml} from 'js-yaml'; import {addHook} from 'pirates'; -import compose from '../compose'; import D from '../debug'; import Flecks from '../flecks'; import R from '../require'; @@ -124,14 +123,13 @@ export default class ServerFlecks extends Flecks { // Load RCs. const rcs = this.loadRcs(resolver); this.installCompilers(rcs, resolver); - // Load the flecks. - const entries = Object.keys(resolver).map((path) => [path, R(this.resolve(resolver, path))]); - // Flecks mixins. - const mixins = entries.map(([, M]) => M.hooks?.['@flecks/core.mixin']).filter((e) => e); - const MixedServerFlecks = compose(...mixins)(ServerFlecks); - return new MixedServerFlecks({ + // Instantiate with mixins. + return ServerFlecks.from({ config, - flecks: Object.fromEntries(entries), + flecks: Object.fromEntries( + Object.keys(resolver) + .map((path) => [path, R(this.resolve(resolver, path))]), + ), platforms, rcs, resolver, diff --git a/packages/core/test/gather.js b/packages/core/test/gather.js index 0c63970..878872b 100644 --- a/packages/core/test/gather.js +++ b/packages/core/test/gather.js @@ -6,7 +6,7 @@ const testOne = require('./one'); const testTwo = require('./two'); it('can gather', () => { - const flecks = new Flecks({ + const flecks = Flecks.from({ flecks: { '@flecks/core/one': testOne, '@flecks/core/two': testTwo, diff --git a/packages/core/test/instance.js b/packages/core/test/instance.js index b575003..f477349 100644 --- a/packages/core/test/instance.js +++ b/packages/core/test/instance.js @@ -16,12 +16,12 @@ it('can create an empty instance', () => { it('can gather config', () => { let flecks; - flecks = new Flecks({ + flecks = Flecks.from({ flecks: {'@flecks/core/one': testOne}, }); expect(flecks.get(['@flecks/core/one'])) .to.contain({foo: 'bar'}); - flecks = new Flecks({ + flecks = Flecks.from({ config: {'@flecks/core/one': {foo: 'baz'}}, flecks: {'@flecks/core/one': testOne}, }); diff --git a/packages/core/test/invoke.js b/packages/core/test/invoke.js index a830487..6297b6d 100644 --- a/packages/core/test/invoke.js +++ b/packages/core/test/invoke.js @@ -13,7 +13,7 @@ const testTwo = require('./two'); let flecks; beforeEach(() => { - flecks = new Flecks({ + flecks = Flecks.from({ flecks: { '@flecks/core/one': testOne, '@flecks/core/two': testTwo, diff --git a/packages/core/test/middleware.js b/packages/core/test/middleware.js index e925982..0779f72 100644 --- a/packages/core/test/middleware.js +++ b/packages/core/test/middleware.js @@ -7,7 +7,7 @@ const testTwo = require('./two'); const testThree = require('./three'); it('can make middleware', (done) => { - const flecks = new Flecks({ + const flecks = Flecks.from({ config: { '@flecks/core/test': { middleware: [ @@ -30,7 +30,7 @@ it('can make middleware', (done) => { }); it('respects explicit middleware configuration', (done) => { - const flecks = new Flecks({ + const flecks = Flecks.from({ config: { '@flecks/core/test': { middleware: [ @@ -53,7 +53,7 @@ it('respects explicit middleware configuration', (done) => { }); it('respects middleware elision', (done) => { - const flecks = new Flecks({ + const flecks = Flecks.from({ config: { '@flecks/core/test': { middleware: [ @@ -75,7 +75,7 @@ it('respects middleware elision', (done) => { }); it('throws on elision graph cycle', () => { - const flecks = new Flecks({ + const flecks = Flecks.from({ config: { '@flecks/core/test': { middleware: [ diff --git a/packages/server/src/entry.js b/packages/server/src/entry.js index 52b113e..3c00b25 100644 --- a/packages/server/src/entry.js +++ b/packages/server/src/entry.js @@ -2,8 +2,8 @@ import {mkdir} from 'fs/promises'; import {tmpdir} from 'os'; import {join} from 'path'; -import {compose, D} from '@flecks/core'; -import {Flecks as BaseFlecks} from '@flecks/core/server'; +import {D} from '@flecks/core'; +import {Flecks} from '@flecks/core/server'; const {version} = require('../package.json'); @@ -23,17 +23,12 @@ const {version} = require('../package.json'); debug('starting server...'); // Make resolver. // Flecks mixins. - const resolver = BaseFlecks.makeResolver(config); - const rcs = BaseFlecks.loadRcs(resolver); - BaseFlecks.installCompilers(rcs, resolver); - const flecks = await loadFlecks(); - const mixins = Object.entries(flecks) - .map(([, M]) => M.hooks?.['@flecks/core.mixin']) - .filter((e) => e); - const Flecks = compose(...mixins)(BaseFlecks); - global.flecks = new Flecks({ + const resolver = Flecks.makeResolver(config); + const rcs = Flecks.loadRcs(resolver); + Flecks.installCompilers(rcs, resolver); + global.flecks = Flecks.from({ config, - flecks, + flecks: await loadFlecks(), platforms, resolver, rcs, diff --git a/packages/web/src/server/build/entry.js b/packages/web/src/server/build/entry.js index dccbe63..344444b 100644 --- a/packages/web/src/server/build/entry.js +++ b/packages/web/src/server/build/entry.js @@ -1,4 +1,4 @@ -import {compose, D, Flecks as BaseFlecks} from '@flecks/core'; +import {D, Flecks} from '@flecks/core'; // eslint-disable-next-line import/no-extraneous-dependencies, import/no-unresolved const {version} = require('@flecks/web/package.json'); @@ -54,11 +54,7 @@ const {version} = require('@flecks/web/package.json'); progress.finish(); debug('starting client...'); // Flecks mixins. - const mixins = Object.entries(runtime.flecks) - .map(([, M]) => M.hooks?.['@flecks/core.mixin']) - .filter((e) => e); - const Flecks = compose(...mixins)(BaseFlecks); - const flecks = new Flecks(runtime); + const flecks = Flecks.from(runtime); window.flecks = flecks; try { await Promise.all(flecks.invokeFlat('@flecks/core.starting'));