refactor: style and test discovery

This commit is contained in:
cha0s 2024-01-13 06:19:49 -06:00
parent e1ab724a41
commit 5b37c1f667
4 changed files with 52 additions and 68 deletions

View File

@ -7,7 +7,7 @@ const augmentBuild = (target, config, env, argv, flecks) => {
switch (target) { switch (target) {
case 'fleck': { case 'fleck': {
finalLoader = {loader: MiniCssExtractPlugin.loader}; finalLoader = {loader: MiniCssExtractPlugin.loader};
config.plugins.push(new MiniCssExtractPlugin({filename: '[name].css'})); config.plugins.push(new MiniCssExtractPlugin({filename: 'assets/[name].css'}));
break; break;
} }
case 'server': { case 'server': {

View File

@ -1,12 +1,13 @@
const {readFile, realpath, stat} = require('fs/promises'); const {access, readFile, realpath} = require('fs/promises');
const { const {
basename,
dirname, dirname,
extname,
join, join,
} = require('path'); } = require('path');
const {D} = require('@flecks/core'); const {D} = require('@flecks/core');
const {Flecks, require: R} = require('@flecks/core/server'); const {Flecks, glob, require: R} = require('@flecks/core/server');
const glob = require('glob');
const debug = D('@flecks/web/runtime'); const debug = D('@flecks/web/runtime');
@ -19,36 +20,23 @@ module.exports = async (config, env, argv, flecks) => {
const rootMap = {}; const rootMap = {};
Object.keys(webFlecks.resolver) Object.keys(webFlecks.resolver)
.forEach((fleck) => { .forEach((fleck) => {
rootMap[webFlecks.root(fleck)] = fleck; rootMap[fleck] = webFlecks.root(fleck);
}); });
const roots = Object.entries(rootMap)
.map(([root, fleck]) => (
[fleck, dirname(R.resolve(join(root, 'package.json')))]
));
const styles = ( const styles = (
await Promise.all( await Promise.all(
roots Object.entries(rootMap)
.map(([fleck, path]) => { .map(async ([fleck, root]) => {
try { // Compiled? It will be included with the compilation.
if (webFlecks.fleckIsCompiled(fleck)) { if (webFlecks.fleckIsCompiled(fleck)) {
return []; return undefined;
}
const {files} = R(join(path, 'package.json'));
return (
files
.filter((name) => name.match(/\.css$/))
.map((name) => join(path, name))
);
} }
catch (error) { const fleckResolved = R.resolve(fleck);
return []; const rootResolved = dirname(R.resolve(join(root, 'package.json')));
} const sub = fleckResolved.slice(rootResolved.length + 1);
}) const style = join(rootResolved, 'assets', `${basename(sub, extname(sub))}.css`);
.flat()
.map(async (filename) => {
try { try {
await stat(filename); await access(style);
return filename; return style;
} }
catch (error) { catch (error) {
return undefined; return undefined;
@ -112,25 +100,26 @@ module.exports = async (config, env, argv, flecks) => {
}); });
} }
// Styles. // Styles.
// @todo Not necessary for compiled flecks.
config.entry.index.push(...styles); config.entry.index.push(...styles);
// Tests. // Tests.
if (!isProduction) { if (!isProduction) {
const testPaths = []; // const testPaths = [];
roots.forEach(([fleck, root]) => { const roots = Array.from(new Set(Object.entries(rootMap).map(([root]) => root)));
testPaths.push(...( const testEntries = await Promise.all(
glob.sync(join(root, 'test/*.js')) roots.map(async (root) => {
.map((path) => [fleck, path]) const paths = [];
)); const resolved = dirname(__non_webpack_require__.resolve(root));
for (let i = 0; i < webFlecks.platforms.length; ++i) { const rootTests = await glob(join(resolved, 'test', '*.js'));
testPaths.push( paths.push(...rootTests);
...( const platformTests = await Promise.all(
glob.sync(join(root, `test/platforms/${webFlecks.platforms[i]}/*.js`)) webFlecks.platforms.map((platform) => (
.map((path) => [fleck, path]) glob(join(resolved, 'test', 'platforms', platform, '*.js'))
), )),
); );
} paths.push(...platformTests.flat());
}); return [root, paths];
}),
);
const tests = await realpath(R.resolve( const tests = await realpath(R.resolve(
join(webFlecks.resolve('@flecks/web'), 'server', 'build', 'tests'), join(webFlecks.resolve('@flecks/web'), 'server', 'build', 'tests'),
)); ));
@ -142,30 +131,16 @@ module.exports = async (config, env, argv, flecks) => {
loader: runtime, loader: runtime,
options: { options: {
source: testsSource.replace( source: testsSource.replace(
"await import('@flecks/web/tests');", " await import('@flecks/web/tests');",
[ testEntries
'const tests = {};', .filter(([, paths]) => paths.length > 0)
Object.entries( .map(([root, paths]) => (
testPaths [
.reduce( ` describe('${root}', () => {`,
(r, [fleck, path]) => ({ ` ${paths.map((path) => `require('${path}');`).join('\n ')}`,
...r, ' });',
[fleck]: [...(r[fleck] || []), `require('${path}');`], ].join('\n')
}), )).join('\n\n'),
{},
),
)
.map(
([original, paths]) => (
[
`describe('${original}', () => {`,
` ${paths.join('\n ')}`,
'});',
].join('\n')
),
).join('\n'),
'await Promise.all(Object.values(tests));',
].join('\n'),
), ),
}, },
}, },

View File

@ -211,6 +211,10 @@ module.exports = async (env, argv, flecks) => {
}, },
plugins, plugins,
resolve: { resolve: {
// Resolve the generated `url(assets/*)` style paths.
alias: {
assets: '.',
},
fallback: { fallback: {
buffer: R.resolve('buffer'), buffer: R.resolve('buffer'),
child_process: false, child_process: false,

View File

@ -225,6 +225,11 @@ export const hooks = {
'web', 'web',
...(flecks.get('@flecks/web/server.dll').length > 0 ? ['web-vendor'] : []), ...(flecks.get('@flecks/web/server.dll').length > 0 ? ['web-vendor'] : []),
], ],
'@flecks/fleck/server.packageJson': (json, compilation) => {
if (Object.keys(compilation.assets).some((filename) => filename.match(/^assets\//))) {
json.files.push('assets');
}
},
'@flecks/web.config': async (req, flecks) => ({ '@flecks/web.config': async (req, flecks) => ({
'@flecks/web/client': { '@flecks/web/client': {
appMountId: flecks.get('@flecks/web/server.appMountId'), appMountId: flecks.get('@flecks/web/server.appMountId'),