Skip to content

Commit 5e25d3d

Browse files
committed
feat: add prettier for code formatting and enhance build process with local externals plugin
1 parent a20c26d commit 5e25d3d

3 files changed

Lines changed: 81 additions & 20 deletions

File tree

esbuild.ts

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,111 @@
1+
import type { Plugin } from 'esbuild';
12
import { build } from 'esbuild';
2-
import { copyFileSync, readFileSync, existsSync } from 'node:fs';
3-
import { dirname, resolve } from 'node:path';
3+
import {
4+
copyFileSync,
5+
readFileSync,
6+
readdirSync,
7+
writeFileSync,
8+
existsSync,
9+
} from 'node:fs';
10+
import { dirname, join, resolve } from 'node:path';
411
import { fileURLToPath } from 'node:url';
512
import AdmZip from 'adm-zip';
13+
import { format } from 'prettier';
614

715
interface ExtensionMetadata {
816
name: string;
917
version: string;
1018
uuid: string;
1119
}
1220

21+
// Externalize local .ts imports and rewrite paths to .js
22+
const localExternals: Plugin = {
23+
name: 'local-externals',
24+
setup(ctx) {
25+
ctx.onResolve({ filter: /\.ts$/ }, (args) => {
26+
if (args.kind === 'entry-point') return;
27+
if (args.path.startsWith('.')) {
28+
return {
29+
path: args.path.replace(/\.ts$/, '.js'),
30+
external: true,
31+
};
32+
}
33+
});
34+
},
35+
};
36+
1337
const currentDir = dirname(fileURLToPath(import.meta.url));
1438
const metadataPath = resolve(currentDir, 'metadata.json');
1539
const metadata = JSON.parse(readFileSync(metadataPath, 'utf8')) as ExtensionMetadata;
1640

41+
const srcDir = resolve(currentDir, 'src');
42+
const entryPoints = (readdirSync(srcDir, { recursive: true }) as string[])
43+
.filter((file) => file.endsWith('.ts') && !file.endsWith('.d.ts'))
44+
.map((file) => join('src', file));
45+
1746
console.debug(`Building ${metadata.name} v${metadata.version}...`);
1847

19-
build({
20-
entryPoints: ['src/extension.ts', 'src/prefs.ts'],
48+
const sharedOptions = {
2149
outdir: 'dist',
50+
outbase: 'src',
2251
bundle: true,
52+
plugins: [localExternals],
2353
// Do not remove the functions `enable()`, `disable()` and `init()`
2454
treeShaking: false,
2555
// firefox60 // Since GJS 1.53.90
2656
// firefox68 // Since GJS 1.63.90
2757
// firefox78 // Since GJS 1.65.90
2858
// firefox91 // Since GJS 1.71.1
2959
// firefox102 // Since GJS 1.73.2
30-
target: 'firefox102',
31-
format: 'esm',
60+
target: 'firefox102' as const,
61+
format: 'esm' as const,
3262
alias: {
3363
// Not exported in @girs/gnome-shell v49; map directly to runtime resource
3464
'@girs/gnome-shell/ui/dash': 'resource:///org/gnome/shell/ui/dash.js',
3565
},
3666
external: ['gi://*', 'resource://*', 'system', 'gettext', 'cairo'],
37-
})
38-
.then(() => {
39-
const metaDist = resolve(currentDir, 'dist/metadata.json');
40-
const extensionSrc = resolve(currentDir, 'dist/extension.js');
41-
const prefsSrc = resolve(currentDir, 'dist/prefs.js');
67+
};
68+
69+
Promise.all(
70+
entryPoints.map((entryPoint) =>
71+
build({ ...sharedOptions, entryPoints: [entryPoint] }),
72+
),
73+
)
74+
.then(async () => {
75+
const distDir = resolve(currentDir, 'dist');
76+
const metaDist = resolve(distDir, 'metadata.json');
4277
const schemasSrc = resolve(currentDir, 'schemas');
4378
const styleFiles = [
44-
'dist/stylesheet.css',
45-
'dist/stylesheet-light.css',
46-
'dist/stylesheet-dark.css',
47-
].map((file) => resolve(currentDir, file));
79+
'stylesheet.css',
80+
'stylesheet-light.css',
81+
'stylesheet-dark.css',
82+
];
4883
const zipFilename = `${metadata.uuid}.zip`;
49-
const zipDist = resolve(currentDir, 'dist', zipFilename);
84+
const zipDist = resolve(distDir, zipFilename);
85+
86+
// Format output files with prettier
87+
const jsFiles = (readdirSync(distDir, { recursive: true }) as string[])
88+
.filter((file) => file.endsWith('.js'));
89+
90+
for (const file of jsFiles) {
91+
const filePath = resolve(distDir, file);
92+
const content = readFileSync(filePath, 'utf8');
93+
const formatted = await format(content, { parser: 'babel', filepath: filePath });
94+
writeFileSync(filePath, formatted);
95+
}
5096

5197
copyFileSync(metadataPath, metaDist);
5298

5399
const zip: AdmZip = new AdmZip();
54-
zip.addLocalFile(extensionSrc);
55-
if (existsSync(prefsSrc)) {
56-
zip.addLocalFile(prefsSrc);
100+
101+
for (const file of jsFiles) {
102+
const filePath = resolve(distDir, file);
103+
const dir = dirname(file);
104+
zip.addLocalFile(filePath, dir === '.' ? '' : dir);
57105
}
58-
styleFiles.forEach((stylePath) => {
106+
107+
styleFiles.forEach((styleFile) => {
108+
const stylePath = resolve(distDir, styleFile);
59109
if (existsSync(stylePath)) {
60110
zip.addLocalFile(stylePath);
61111
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"esbuild": "^0.25.11",
2727
"eslint": "^9.39.1",
2828
"jiti": "^2.6.1",
29+
"prettier": "^3.8.1",
2930
"sass": "^1.93.2",
3031
"tsx": "^4.20.6",
3132
"typescript": "^5.9.3",

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,7 @@ __metadata:
16501650
esbuild: "npm:^0.25.11"
16511651
eslint: "npm:^9.39.1"
16521652
jiti: "npm:^2.6.1"
1653+
prettier: "npm:^3.8.1"
16531654
sass: "npm:^1.93.2"
16541655
tsx: "npm:^4.20.6"
16551656
typescript: "npm:^5.9.3"
@@ -2832,6 +2833,15 @@ __metadata:
28322833
languageName: node
28332834
linkType: hard
28342835

2836+
"prettier@npm:^3.8.1":
2837+
version: 3.8.1
2838+
resolution: "prettier@npm:3.8.1"
2839+
bin:
2840+
prettier: bin/prettier.cjs
2841+
checksum: 10c0/33169b594009e48f570471271be7eac7cdcf88a209eed39ac3b8d6d78984039bfa9132f82b7e6ba3b06711f3bfe0222a62a1bfb87c43f50c25a83df1b78a2c42
2842+
languageName: node
2843+
linkType: hard
2844+
28352845
"proc-log@npm:^6.0.0":
28362846
version: 6.1.0
28372847
resolution: "proc-log@npm:6.1.0"

0 commit comments

Comments
 (0)