|
| 1 | +import esbuild from 'esbuild'; |
| 2 | +import copy from 'esbuild-plugin-copy'; |
| 3 | +import { wasmLoader } from 'esbuild-plugin-wasm'; |
| 4 | +import path from 'path'; |
| 5 | +import fs from 'fs'; |
| 6 | +import { fileURLToPath } from 'url'; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | + |
| 11 | +const isProduction = process.env.NODE_ENV === 'production'; |
| 12 | + |
| 13 | +function generateHtmlPlugin(options) { |
| 14 | + return { |
| 15 | + name: 'firefox-profiler-generate-html', |
| 16 | + setup(build) { |
| 17 | + const { outdir, publicPath } = build.initialOptions; |
| 18 | + build.initialOptions.metafile = true; |
| 19 | + build.onEnd(async (result) => { |
| 20 | + await generateHTML(result.metafile, { ...options, outdir, publicPath }); |
| 21 | + }); |
| 22 | + }, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +const baseConfig = { |
| 27 | + bundle: true, |
| 28 | + absWorkingDir: __dirname, |
| 29 | + loader: { |
| 30 | + '.png': 'file', |
| 31 | + '.jpg': 'file', |
| 32 | + '.svg': 'file', |
| 33 | + '.worker.js': 'file', |
| 34 | + }, |
| 35 | + alias: { |
| 36 | + 'firefox-profiler': './src', |
| 37 | + 'firefox-profiler-res': './res', |
| 38 | + }, |
| 39 | + plugins: [wasmLoader()], |
| 40 | +}; |
| 41 | + |
| 42 | +const templateHTML = fs.readFileSync( |
| 43 | + path.join(__dirname, 'res/index.html'), |
| 44 | + 'utf8' |
| 45 | +); |
| 46 | + |
| 47 | +export const mainBundleConfig = { |
| 48 | + ...baseConfig, |
| 49 | + format: 'esm', |
| 50 | + platform: 'browser', |
| 51 | + target: 'es2022', |
| 52 | + sourcemap: true, |
| 53 | + minify: isProduction, |
| 54 | + splitting: true, |
| 55 | + entryPoints: ['src/index.tsx'], |
| 56 | + outdir: 'dist', |
| 57 | + metafile: true, |
| 58 | + publicPath: '/', |
| 59 | + entryNames: '[name]-[hash]', |
| 60 | + define: { |
| 61 | + 'process.env.L10N': process.env.L10N |
| 62 | + ? JSON.stringify(process.env.L10N) |
| 63 | + : 'undefined', |
| 64 | + AVAILABLE_STAGING_LOCALES: process.env.L10N |
| 65 | + ? JSON.stringify(fs.readdirSync('./locales')) |
| 66 | + : 'undefined', |
| 67 | + // no need to define NODE_ENV: |
| 68 | + // esbuild automatically defines NODE_ENV based on the value for "minify" |
| 69 | + }, |
| 70 | + external: ['zlib'], |
| 71 | + plugins: [ |
| 72 | + wasmLoader(), |
| 73 | + copy({ |
| 74 | + resolveFrom: __dirname, |
| 75 | + assets: [ |
| 76 | + { from: ['res/_headers'], to: ['dist'] }, |
| 77 | + { from: ['res/_redirects'], to: ['dist'] }, |
| 78 | + { from: ['res/contribute.json'], to: ['dist'] }, |
| 79 | + { from: ['res/robots.txt'], to: ['dist'] }, |
| 80 | + { from: ['res/service-worker-compat.js'], to: ['dist'] }, |
| 81 | + { from: ['res/img/favicon.png'], to: ['dist/res/img'] }, |
| 82 | + { from: ['docs-user/**/*'], to: ['dist/docs'] }, |
| 83 | + { from: ['locales/**/*'], to: ['dist/locales'] }, |
| 84 | + ], |
| 85 | + }), |
| 86 | + generateHtmlPlugin({ |
| 87 | + filename: 'index.html', |
| 88 | + entryPoint: 'src/index.tsx', |
| 89 | + templateHTML, |
| 90 | + }), |
| 91 | + ], |
| 92 | +}; |
| 93 | + |
| 94 | +// Common build configuration for node-based tools |
| 95 | +export const nodeBaseConfig = { |
| 96 | + ...baseConfig, |
| 97 | + platform: 'node', |
| 98 | + target: 'node16', |
| 99 | + format: 'cjs', |
| 100 | + external: ['fs', 'path', 'crypto', 'zlib'], |
| 101 | +}; |
| 102 | + |
| 103 | +async function buildAll() { |
| 104 | + // Clean dist directory |
| 105 | + if (fs.existsSync('dist')) { |
| 106 | + fs.rmSync('dist', { recursive: true }); |
| 107 | + } |
| 108 | + |
| 109 | + const builds = []; |
| 110 | + |
| 111 | + // Main app build |
| 112 | + builds.push(esbuild.build(mainBundleConfig)); |
| 113 | + |
| 114 | + // Node tools (if requested) |
| 115 | + if (process.argv.includes('--node-tools')) { |
| 116 | + // Symbolicator CLI |
| 117 | + builds.push( |
| 118 | + esbuild.build({ |
| 119 | + ...nodeBaseConfig, |
| 120 | + entryPoints: ['src/symbolicator-cli/index.ts'], |
| 121 | + outfile: 'dist/symbolicator-cli.js', |
| 122 | + }) |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + // Wait for all builds to complete |
| 127 | + const buildResults = await Promise.all(builds); |
| 128 | + |
| 129 | + // Save metafile for analysis |
| 130 | + if (buildResults[0].metafile) { |
| 131 | + fs.writeFileSync( |
| 132 | + 'dist/metafile.json', |
| 133 | + JSON.stringify(buildResults[0].metafile, null, 2) |
| 134 | + ); |
| 135 | + console.log('📊 Metafile saved to dist/metafile.json'); |
| 136 | + } |
| 137 | + |
| 138 | + console.log('✅ Build completed'); |
| 139 | +} |
| 140 | + |
| 141 | +async function generateHTML(metafileJson, options) { |
| 142 | + const { entryPoint, templateHTML, filename, outdir, publicPath } = options; |
| 143 | + |
| 144 | + const htmlOutputPath = outdir + '/' + filename; |
| 145 | + |
| 146 | + function convertPath(oldPath) { |
| 147 | + const prefixToStrip = outdir + '/'; |
| 148 | + |
| 149 | + if (!oldPath || !oldPath.startsWith || !oldPath.startsWith(prefixToStrip)) { |
| 150 | + throw new Error( |
| 151 | + `Unexpected path ${oldPath} which seems to be outside the outdir (which is set to ${outdir})` |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + const relativePath = oldPath.slice(prefixToStrip.length); |
| 156 | + if (publicPath) { |
| 157 | + // e.g. publicPath === '/' |
| 158 | + return publicPath + relativePath; |
| 159 | + } |
| 160 | + return relativePath; |
| 161 | + } |
| 162 | + |
| 163 | + if (!metafileJson || !metafileJson.outputs) { |
| 164 | + throw new Error('No outputs detected'); |
| 165 | + } |
| 166 | + |
| 167 | + const [mainBundlePath, mainBundle] = Object.entries( |
| 168 | + metafileJson.outputs |
| 169 | + ).find(([_bundlePath, bundle]) => bundle.entryPoint === entryPoint); |
| 170 | + |
| 171 | + const extraHeadTags = []; |
| 172 | + |
| 173 | + // Main JS bundle |
| 174 | + extraHeadTags.push( |
| 175 | + `<script src="${convertPath(mainBundlePath)}" type="module" async></script>` |
| 176 | + ); |
| 177 | + |
| 178 | + // Main Stylesheet |
| 179 | + if (mainBundle.cssBundle) { |
| 180 | + extraHeadTags.push( |
| 181 | + `<link rel="stylesheet" href="${convertPath(mainBundle.cssBundle)}">` |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + // Preload startup chunks |
| 186 | + const startupChunks = mainBundle.imports.filter( |
| 187 | + (imp) => imp.kind === 'import-statement' // as opposed to 'dynamic-import' |
| 188 | + ); |
| 189 | + for (const startupChunk of startupChunks) { |
| 190 | + extraHeadTags.push( |
| 191 | + `<link rel="modulepreload" href="${convertPath(startupChunk.path)}">` |
| 192 | + ); |
| 193 | + } |
| 194 | + |
| 195 | + // Insert tags before </head> |
| 196 | + const extraHeadStr = extraHeadTags.map((s) => ' ' + s).join('\n'); |
| 197 | + const html = templateHTML.replace( |
| 198 | + '</head>', |
| 199 | + '\n' + extraHeadStr + '\n </head>' |
| 200 | + ); |
| 201 | + |
| 202 | + // Write the final HTML |
| 203 | + fs.writeFileSync(htmlOutputPath, html); |
| 204 | +} |
| 205 | + |
| 206 | +// Run build if called directly |
| 207 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 208 | + buildAll().catch(console.error); |
| 209 | +} |
0 commit comments