|
| 1 | +import type { Plugin } from 'esbuild'; |
1 | 2 | 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'; |
4 | 11 | import { fileURLToPath } from 'node:url'; |
5 | 12 | import AdmZip from 'adm-zip'; |
| 13 | +import { format } from 'prettier'; |
6 | 14 |
|
7 | 15 | interface ExtensionMetadata { |
8 | 16 | name: string; |
9 | 17 | version: string; |
10 | 18 | uuid: string; |
11 | 19 | } |
12 | 20 |
|
| 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 | + |
13 | 37 | const currentDir = dirname(fileURLToPath(import.meta.url)); |
14 | 38 | const metadataPath = resolve(currentDir, 'metadata.json'); |
15 | 39 | const metadata = JSON.parse(readFileSync(metadataPath, 'utf8')) as ExtensionMetadata; |
16 | 40 |
|
| 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 | + |
17 | 46 | console.debug(`Building ${metadata.name} v${metadata.version}...`); |
18 | 47 |
|
19 | | -build({ |
20 | | - entryPoints: ['src/extension.ts', 'src/prefs.ts'], |
| 48 | +const sharedOptions = { |
21 | 49 | outdir: 'dist', |
| 50 | + outbase: 'src', |
22 | 51 | bundle: true, |
| 52 | + plugins: [localExternals], |
23 | 53 | // Do not remove the functions `enable()`, `disable()` and `init()` |
24 | 54 | treeShaking: false, |
25 | 55 | // firefox60 // Since GJS 1.53.90 |
26 | 56 | // firefox68 // Since GJS 1.63.90 |
27 | 57 | // firefox78 // Since GJS 1.65.90 |
28 | 58 | // firefox91 // Since GJS 1.71.1 |
29 | 59 | // firefox102 // Since GJS 1.73.2 |
30 | | - target: 'firefox102', |
31 | | - format: 'esm', |
| 60 | + target: 'firefox102' as const, |
| 61 | + format: 'esm' as const, |
32 | 62 | alias: { |
33 | 63 | // Not exported in @girs/gnome-shell v49; map directly to runtime resource |
34 | 64 | '@girs/gnome-shell/ui/dash': 'resource:///org/gnome/shell/ui/dash.js', |
35 | 65 | }, |
36 | 66 | 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'); |
42 | 77 | const schemasSrc = resolve(currentDir, 'schemas'); |
43 | 78 | 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 | + ]; |
48 | 83 | 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 | + } |
50 | 96 |
|
51 | 97 | copyFileSync(metadataPath, metaDist); |
52 | 98 |
|
53 | 99 | 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); |
57 | 105 | } |
58 | | - styleFiles.forEach((stylePath) => { |
| 106 | + |
| 107 | + styleFiles.forEach((styleFile) => { |
| 108 | + const stylePath = resolve(distDir, styleFile); |
59 | 109 | if (existsSync(stylePath)) { |
60 | 110 | zip.addLocalFile(stylePath); |
61 | 111 | } |
|
0 commit comments