Skip to content

Commit ad1b5e4

Browse files
committed
use eslint with stylistic for tidying javascript
eslint and stylistic is more configurable than prettier and can have better alignment. We are also already using eslint.
1 parent d7c0291 commit ad1b5e4

20 files changed

+1036
-514
lines changed

build-assets.mjs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
#!/usr/bin/env node
22

3-
import * as esbuild from 'esbuild'
4-
import {
5-
lessLoader
6-
}
7-
from 'esbuild-plugin-less';
3+
import * as esbuild from 'esbuild';
4+
import { lessLoader } from 'esbuild-plugin-less';
85
import {
96
writeFile,
107
opendir,
11-
unlink
12-
}
13-
from 'node:fs/promises';
8+
unlink,
9+
} from 'node:fs/promises';
10+
import console from 'node:console';
11+
import process from 'node:process';
1412
import path from 'node:path';
1513
import parseArgs from 'minimist';
1614

@@ -21,16 +19,16 @@ const config = {
2119
],
2220
assetNames: '[name]-[hash]',
2321
entryNames: '[name]-[hash]',
24-
format: 'esm',
25-
outdir: 'root/assets',
26-
bundle: true,
27-
sourcemap: true,
28-
inject: ['root/static/js/inject.mjs'],
29-
loader: {
30-
'.eot': 'file',
31-
'.svg': 'file',
32-
'.ttf': 'file',
33-
'.woff': 'file',
22+
format: 'esm',
23+
outdir: 'root/assets',
24+
bundle: true,
25+
sourcemap: true,
26+
inject: ['root/static/js/inject.mjs'],
27+
loader: {
28+
'.eot': 'file',
29+
'.svg': 'file',
30+
'.ttf': 'file',
31+
'.woff': 'file',
3432
'.woff2': 'file',
3533
},
3634
plugins: [
@@ -40,18 +38,18 @@ const config = {
4038

4139
setup(build) {
4240
build.onResolve({
43-
filter: /^\//
44-
},
45-
() => ({
46-
external: true
47-
}),
41+
filter: /^\//,
42+
},
43+
() => ({
44+
external: true,
45+
}),
4846
);
4947

5048
build.initialOptions.metafile = true;
5149
build.onStart(() => {
52-
console.log('building assets...')
50+
console.log('building assets...');
5351
});
54-
build.onEnd(async result => {
52+
build.onEnd(async (result) => {
5553
const outputs = result?.metafile?.outputs;
5654
if (outputs) {
5755
const files = Object.keys(outputs).sort()
@@ -70,7 +68,7 @@ const config = {
7068
}
7169
});
7270
}
73-
},
71+
}(),
7472
],
7573
};
7674

@@ -86,7 +84,7 @@ if (args.minify) {
8684
}
8785
if (args.clean) {
8886
for await (const file of await opendir(config.outdir, {
89-
withFileTypes: true
87+
withFileTypes: true,
9088
})) {
9189
const filePath = path.join(file.parentPath, file.name);
9290
if (file.name.match(/^\./)) {
@@ -105,7 +103,7 @@ if (args.clean) {
105103
const ctx = await esbuild.context(config);
106104
if (args.watch) {
107105
await ctx.watch();
108-
const sig = await new Promise(resolve => {
106+
const sig = await new Promise((resolve) => {
109107
[
110108
'SIGTERM',
111109
'SIGQUIT',

eslint.config.mjs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
1-
import globals from "globals";
2-
import pluginJs from "@eslint/js";
1+
import globals from 'globals';
2+
import stylistic from '@stylistic/eslint-plugin';
3+
import js from '@eslint/js';
34

4-
export default [{
5-
ignores: ["root/assets/"],
6-
},
5+
export default [
76
{
8-
languageOptions: {
9-
globals: globals.browser,
10-
}
7+
ignores: ['root/assets/'],
118
},
129
{
13-
files: ['build-assets.mjs'],
10+
files: ['**/*.mjs'],
1411
languageOptions: {
15-
globals: globals.nodeBuiltin,
12+
sourceType: 'module',
1613
},
1714
},
1815
{
19-
files: ['**/*.js'],
16+
files: ['**/*.js'],
2017
languageOptions: {
2118
sourceType: 'commonjs',
22-
}
19+
},
2320
},
2421
{
25-
files: ['**/*.mjs'],
22+
files: ['root/**/*.mjs', 'root/**/*.js'],
2623
languageOptions: {
27-
sourceType: 'module',
28-
}
24+
globals: globals.browser,
25+
},
26+
},
27+
js.configs.recommended,
28+
stylistic.configs.customize({
29+
semi: true,
30+
}),
31+
{
32+
rules: {
33+
'@stylistic/indent': ['error', 4],
34+
'@stylistic/multiline-ternary': 'off',
35+
'@stylistic/key-spacing': [
36+
'error',
37+
{
38+
align: 'value',
39+
},
40+
],
41+
},
2942
},
30-
pluginJs.configs.recommended,
3143
];

0 commit comments

Comments
 (0)