From 85aa9d78cad4d3449f447734f95ea74b6b59a1bf Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 16 Feb 2024 20:56:19 -0600 Subject: [PATCH] fix: allow colocated hook sources --- packages/core/build/flecks.js | 21 +++++++++++++++++-- .../core/test/hooks/@flecks/_colocated.js | 0 .../core/test/hooks/@flecks/core.config.js | 1 + packages/core/test/split-hooks.js | 11 ++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 packages/core/test/hooks/@flecks/_colocated.js create mode 100644 packages/core/test/hooks/@flecks/core.config.js create mode 100644 packages/core/test/split-hooks.js diff --git a/packages/core/build/flecks.js b/packages/core/build/flecks.js index 36619cc..65e5c61 100644 --- a/packages/core/build/flecks.js +++ b/packages/core/build/flecks.js @@ -420,6 +420,13 @@ class Flecks { /** * Get a fleck's implementation of a hook. * + * :::info + * + * The `flecks` instance is not automatically passed if you manually invoke this implementation + * and must be provided by you, the caller. + * + * ::: + * * @param {*} fleck * @param {string} hook * @returns {boolean} @@ -566,6 +573,11 @@ class Flecks { return get(this.config, path, defaultValue); } + /** + * Gather hooks from a require context. + * @param {webpack.Context} context The result from `require.context()`. + * @returns {Object} The hooks object. + */ static hooks(context) { const implementations = {}; context.keys() @@ -581,7 +593,12 @@ class Flecks { .map(([, keys]) => { // Shortest is the one without extension. const key = keys.reduce((l, r) => (r.length < l.length ? r : l)); - const trimmed = key.startsWith('./') ? key.slice(2) : key; + const hook = key.startsWith('./') ? key.slice(2) : key; + return [hook, key]; + }) + // Allow colocated files if they start with '_'. + .filter(([, key]) => !key.includes('/_')) + .map(([hook, key]) => { const M = context(key); if (!M.hook) { const hasDefault = !!M.default; @@ -590,7 +607,7 @@ class Flecks { ...(hasDefault ? ['Did you default export the implementation?'] : []), ].join(' ')); } - return [trimmed, context(key).hook]; + return [hook, context(key).hook]; }), ); } diff --git a/packages/core/test/hooks/@flecks/_colocated.js b/packages/core/test/hooks/@flecks/_colocated.js new file mode 100644 index 0000000..e69de29 diff --git a/packages/core/test/hooks/@flecks/core.config.js b/packages/core/test/hooks/@flecks/core.config.js new file mode 100644 index 0000000..fe79d43 --- /dev/null +++ b/packages/core/test/hooks/@flecks/core.config.js @@ -0,0 +1 @@ +export const hook = () => ({}); diff --git a/packages/core/test/split-hooks.js b/packages/core/test/split-hooks.js new file mode 100644 index 0000000..b56b642 --- /dev/null +++ b/packages/core/test/split-hooks.js @@ -0,0 +1,11 @@ +import {expect} from 'chai'; + +import {Flecks} from '@flecks/core'; + +it('can gather split hooks', () => { + const hooks = Flecks.hooks(require.context('./hooks')); + expect(hooks['@flecks/core.config']) + .to.not.be.undefined; + expect(hooks['@flecks/_colocated']) + .to.be.undefined; +});