-
Notifications
You must be signed in to change notification settings - Fork 3
Enhancement/esbuild mode #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8cbd13b
Added esbuild mode
goransle 2e29794
Updated readme
goransle 481481a
Removed mention of yarn, used esbuild on index page
goransle b7e7ecc
Ran tsc
goransle 80c8015
Ran lint
goransle f1bad8b
Updated package-lock
goransle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // @ts-check | ||
| /** | ||
| * Esbuild-based compile-on-demand functionality. | ||
| * Uses @highcharts/highcharts-utils/lib/compile-on-demand-core.js for core compilation. | ||
| * | ||
| * @module esbuild | ||
| */ | ||
|
|
||
| const { join } = require('node:path') | ||
| const { readFile, writeFile, mkdir } = require('node:fs/promises') | ||
| const { existsSync } = require('node:fs') | ||
| const { log } = require('./utilities') | ||
|
|
||
| /** | ||
| * Cached promise for the dynamically imported compile-on-demand-core module | ||
| * @type {Promise<typeof import('@highcharts/highcharts-utils/lib/compile-on-demand-core.js')> | null} | ||
| */ | ||
| let coreModulePromise = null | ||
|
|
||
| /** | ||
| * Dynamically import the compile-on-demand-core module (ESM) | ||
| * @returns {Promise<typeof import('@highcharts/highcharts-utils/lib/compile-on-demand-core.js')>} | ||
| */ | ||
| async function getCompileCore () { | ||
| if (!coreModulePromise) { | ||
| coreModulePromise = import('@highcharts/highcharts-utils/lib/compile-on-demand-core.js') | ||
| } | ||
| return coreModulePromise | ||
| } | ||
|
|
||
| /** | ||
| * Get package version from package.json in the source directory | ||
| * @param {string} highchartsDir - Path to the Highcharts source directory | ||
| * @returns {Promise<string>} The package version | ||
| */ | ||
| async function getPackageVersion (highchartsDir) { | ||
| try { | ||
| const packageJsonPath = join(highchartsDir, 'package.json') | ||
| const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf-8')) | ||
| return packageJson.version || '0.0.0' | ||
| } catch (e) { | ||
| return '999.0.0' // Default to a high version to skip legacy plugins | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Compile a file and write it to the output directory | ||
| * @param {string} branch - The branch/commit SHA | ||
| * @param {string} requestFilename - The requested filename (e.g., 'highcharts.src.js') | ||
| * @returns {Promise<{file?: string, body?: string, status: number}>} Result with file path or error | ||
| */ | ||
| async function compileWithEsbuild (branch, requestFilename) { | ||
| const core = await getCompileCore() | ||
|
|
||
| const pathCacheDirectory = join(__dirname, '../tmp', branch) | ||
| const outputDir = join(pathCacheDirectory, 'output-esbuild') | ||
|
|
||
| // Normalize the filename to have a leading slash for the compile function | ||
| const normalizedFilename = requestFilename.startsWith('/') | ||
| ? requestFilename | ||
| : `/${requestFilename}` | ||
|
|
||
| // Output file path | ||
| const outputFilePath = join(outputDir, requestFilename) | ||
|
|
||
| // Check if already compiled | ||
| if (existsSync(outputFilePath)) { | ||
| log(0, `Serving cached esbuild file: ${outputFilePath}`) | ||
| return { file: outputFilePath, status: 200 } | ||
| } | ||
|
|
||
| // Ensure output directory exists (use try/catch to handle race conditions) | ||
| const outputFileDir = join(outputDir, ...requestFilename.split('/').slice(0, -1)) | ||
| try { | ||
| await mkdir(outputFileDir, { recursive: true }) | ||
| } catch (error) { | ||
| // EEXIST is fine; directory already exists (race condition with parallel requests) | ||
| if (error.code !== 'EEXIST') { | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| log(0, `Compiling with esbuild: ${normalizedFilename} for ${branch}`) | ||
|
|
||
| /** @type {{code: string, duration: number}} */ | ||
| let result | ||
| try { | ||
| result = await core.compile(normalizedFilename, { | ||
| highchartsDir: pathCacheDirectory, | ||
| branchName: branch, | ||
| getPackageVersion: () => getPackageVersion(pathCacheDirectory) | ||
| }) | ||
|
|
||
| // Write to output directory | ||
| await writeFile(outputFilePath, result.code, 'utf-8') | ||
|
|
||
| log(0, `esbuild compilation complete: ${normalizedFilename} (${result.duration}ms)`) | ||
|
|
||
| return { file: outputFilePath, status: 200 } | ||
| } catch (error) { | ||
| log(2, `esbuild compilation failed: ${error.message}`) | ||
| const errorJs = `console.error('esbuild compilation failed: ${error.message.replace(/'/g, "\\'")}');` | ||
| await writeFile(outputFilePath, errorJs, 'utf-8') | ||
| return { file: outputFilePath, status: 200 } | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| compileWithEsbuild, | ||
| // Re-export from core for any external usage | ||
| getCompileCore | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.