refactor: lint, config

This commit is contained in:
cha0s 2023-12-02 22:31:14 -06:00
parent de41c1e01e
commit ab6d1f1720
10 changed files with 97 additions and 84 deletions

View File

@ -44,7 +44,7 @@ export const hooks = {
/**
* Make sure you return them as an array expression, like this.
*/
['mygeneralrc.js', {specifier: (specific) => `${specific}.mygeneralrc.js`}],
['something.config.js', {specifier: (specific) => `something.${specific}.config.js`}],
],
/**

View File

@ -1,8 +1,31 @@
const configFn = require('../src/server/build/fleck.webpack.config');
const {executable} = require('../src/server/webpack');
const ESLintPlugin = require('eslint-webpack-plugin');
module.exports = (env, argv) => {
const config = configFn(env, argv);
const configFn = require('../src/server/build/webpack.config');
const {executable} = require('../src/server/webpack');
const eslintConfigFn = require('../src/server/build/default.eslint.config');
const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;
module.exports = async (env, argv) => {
const config = await configFn(env, argv);
config.plugins.push(...executable());
const eslint = await eslintConfigFn();
eslint.settings['import/resolver'].webpack = {
config: {
resolve: config.resolve,
},
};
config.plugins.push(
new ESLintPlugin({
cache: true,
cwd: FLECKS_CORE_ROOT,
emitWarning: argv.mode !== 'production',
failOnError: argv.mode === 'production',
useEslintrc: false,
overrideConfig: eslint,
}),
);
return config;
};

View File

@ -36,8 +36,8 @@
"server/build/default.eslint.config.js.map",
"server/build/eslint.config.js",
"server/build/eslint.config.js.map",
"server/build/fleck.webpack.config.js",
"server/build/fleck.webpack.config.js.map",
"server/build/webpack.config.js",
"server/build/webpack.config.js.map",
"server/build/fleckspack.config.js",
"server/build/fleckspack.config.js.map",
"src",

View File

@ -3,7 +3,7 @@ const globals = require('globals');
const R = require('../../require');
module.exports = (flecks) => {
module.exports = async (flecks) => {
const merging = [
{
plugins: [R.resolve('@babel/plugin-syntax-dynamic-import')],
@ -40,7 +40,8 @@ module.exports = (flecks) => {
__non_webpack_require__: true,
},
ignorePatterns: [
'**/dist/**',
'dist/**',
// Not even gonna try.
'build/dox/hooks.js',
],
overrides: [

View File

@ -9,12 +9,10 @@ const {join} = require('path');
const D = require('../../debug');
const {default: Flecks} = require('../flecks');
// const R = require('../../require');
const debug = D('@flecks/core/server/build/eslint.config.js');
const {
FLECKS_CORE_BUILD_TARGET = 'fleck',
FLECKS_CORE_ROOT = process.cwd(),
FLECKS_CORE_SYNC_FOR_ESLINT = false,
} = process.env;
@ -25,10 +23,10 @@ if (FLECKS_CORE_SYNC_FOR_ESLINT) {
debug('bootstrapping flecks...');
const flecks = Flecks.bootstrap();
debug('bootstrapped');
const eslintConfigFn = __non_webpack_require__(flecks.buildConfig('default.eslint.config.js', FLECKS_CORE_BUILD_TARGET));
const eslintConfig = eslintConfigFn(flecks);
const webpackConfigFn = __non_webpack_require__(flecks.buildConfig('webpack.config.js', FLECKS_CORE_BUILD_TARGET));
const webpackConfig = webpackConfigFn({}, {mode: 'development'}, flecks);
const eslintConfigFn = __non_webpack_require__(flecks.buildConfig('default.eslint.config.js'));
const eslintConfig = await eslintConfigFn(flecks);
const webpackConfigFn = __non_webpack_require__(flecks.buildConfig('webpack.config.js', 'fleck'));
const webpackConfig = await webpackConfigFn({}, {mode: 'development'}, flecks);
eslintConfig.settings['import/resolver'].webpack = {
config: {
resolve: webpackConfig.resolve,

View File

@ -8,12 +8,10 @@ const {
const babelmerge = require('babel-merge');
const CopyPlugin = require('copy-webpack-plugin');
const glob = require('glob');
const ESLintPlugin = require('eslint-webpack-plugin');
const D = require('../../debug');
const R = require('../../require');
const {defaultConfig, externals, regexFromExtensions} = require('../webpack');
const eslintConfigFn = require('./default.eslint.config');
const {
FLECKS_CORE_ROOT = process.cwd(),
@ -137,22 +135,6 @@ module.exports = (env, argv, flecks) => {
],
},
);
const eslint = eslintConfigFn(flecks);
eslint.settings['import/resolver'].webpack = {
config: {
resolve: config.resolve,
},
};
config.plugins.push(
new ESLintPlugin({
cache: true,
cwd: FLECKS_CORE_ROOT,
emitWarning: argv.mode !== 'production',
failOnError: argv.mode === 'production',
useEslintrc: false,
overrideConfig: eslint,
}),
);
// Automatic entry registration.
files
.filter(resolveValidModulePath(source))

View File

@ -2,6 +2,7 @@ import {spawn} from 'child_process';
import {join, normalize} from 'path';
import {Argument} from 'commander';
import glob from 'glob';
import flatten from 'lodash.flatten';
import rimraf from 'rimraf';
@ -33,6 +34,8 @@ export const spawnWith = (cmd, opts = {}) => {
...(opts.env || {}),
},
});
process.stderr.setMaxListeners(100);
process.stdout.setMaxListeners(100);
child.stderr.pipe(process.stderr);
child.stdout.pipe(process.stdout);
return child;
@ -97,32 +100,34 @@ export default (program, flecks) => {
);
},
};
commands.lint = {
description: 'run linter',
args: [
program.createArgument('[target]', 'target').choices(targets),
],
action: (targetArgument) => {
const promises = [];
for (let i = 0; i < targets.length; ++i) {
const target = targets[i];
if (targetArgument && targetArgument !== target) {
// eslint-disable-next-line no-continue
continue;
}
process.env.FLECKS_CORE_BUILD_TARGET = target;
}
commands.lint = {
description: 'run linter',
args: [],
action: async () => {
const promises = [];
const packages = await new Promise((r, e) => {
glob(
join('packages', '*'),
(error, result) => (error ? e(error) : r(result)),
);
});
if (0 === packages.length) {
packages.push('.');
}
packages
.map((pkg) => join(process.cwd(), pkg))
.forEach((cwd) => {
const cmd = [
'npx', 'eslint',
'--config', flecks.buildConfig('eslint.config.js', target),
'--config', flecks.buildConfig('eslint.config.js'),
'.',
];
promises.push(new Promise((resolve, reject) => {
const child = spawnWith(
cmd,
{
env: {
FLECKS_CORE_BUILD_TARGET: target,
},
cwd,
},
);
child.on('error', reject);
@ -131,28 +136,27 @@ export default (program, flecks) => {
resolve(code);
});
}));
}
const promise = Promise.all(promises)
.then(
(codes) => (
codes.every((code) => 0 === parseInt(code, 10))
? 0
: codes.find((code) => code !== 0)
),
);
return {
off: () => {},
on: (type, fn) => {
if ('error' === type) {
promise.catch(fn);
}
else if ('exit' === type) {
promise.then(fn);
}
},
};
},
};
}
});
const promise = Promise.all(promises)
.then(
(codes) => (
codes.every((code) => 0 === parseInt(code, 10))
? 0
: codes.find((code) => code !== 0)
),
);
return {
off: () => {},
on: (type, fn) => {
if ('error' === type) {
promise.catch(fn);
}
else if ('exit' === type) {
promise.then(fn);
}
},
};
},
};
return commands;
};

View File

@ -118,13 +118,18 @@ export default class ServerFlecks extends Flecks {
if (!config) {
throw new Error(`Unknown build config '${path}'`);
}
const paths = [`${specific}.${path}`];
if ('specifier' in config) {
if (false === config.specifier) {
paths.pop();
const paths = [];
if (specific) {
if ('specifier' in config) {
if (false === config.specifier) {
paths.shift();
}
else {
paths.push(config.specifier(specific));
}
}
else {
paths.push(config.specifier(specific));
paths.push(`${specific}.${path}`);
}
}
paths.push(path);

View File

@ -31,7 +31,7 @@ export * from './webpack';
export {webpack};
export const hooks = {
'@flecks/core.build': (target, config, env, argv, flecks) => {
'@flecks/core.build': async (target, config, env, argv, flecks) => {
const {
'eslint.exclude': exclude,
profile,
@ -45,7 +45,7 @@ export const hooks = {
}
if (!exclude.includes(target)) {
const eslintConfigFn = R(flecks.buildConfig('default.eslint.config.js', target));
const eslint = eslintConfigFn(flecks);
const eslint = await eslintConfigFn(flecks);
config.plugins.push(
new ESLintPlugin({
cache: true,
@ -80,11 +80,11 @@ export const hooks = {
* ESLint defaults. The generated `eslint.config.js` just reads from this file so that the
* build can dynamically configure parts of ESLint.
*/
'default.eslint.config.js',
['default.eslint.config.js', {specifier: false}],
/**
* ESLint configuration. See: https://eslint.org/docs/user-guide/configuring/
*/
'eslint.config.js',
['eslint.config.js', {specifier: false}],
/**
* Flecks webpack configuration. See: https://webpack.js.org/configuration/
*/

View File

@ -1,4 +1,4 @@
const flecksConfigFn = require('@flecks/core/server/build/fleck.webpack.config');
const flecksConfigFn = require('@flecks/core/server/build/webpack.config');
module.exports = async (env, argv, flecks) => {
const config = await flecksConfigFn(env, argv, flecks);