|
| 1 | +import * as esbuild from 'esbuild'; |
| 2 | +import lodash from 'lodash'; |
| 3 | + |
| 4 | +import { Messages } from '#constants'; |
| 5 | +import { debug } from '#utils'; |
| 6 | + |
| 7 | +import BaseBundlers from '../base.bunlders.js'; |
| 8 | +import AzionEsbuildConfig from './esbuild.config.js'; |
| 9 | +import ESBuildNodeModulePlugin from './plugins/node-polyfills/index.js'; |
| 10 | +import ESBuildAzionModulePlugin from './plugins/azion-polyfills/index.js'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class representing an ESBuild bundler, extending BaseBundlers. |
| 14 | + */ |
| 15 | +class Esbuild extends BaseBundlers { |
| 16 | + /** |
| 17 | + * Asynchronous method to run the ESBuild bundler. |
| 18 | + */ |
| 19 | + async run() { |
| 20 | + let config = lodash.cloneDeep(AzionEsbuildConfig); |
| 21 | + config.entryPoints = [this.builderConfig.entry]; |
| 22 | + |
| 23 | + if (!config.plugins) config.plugins = []; |
| 24 | + |
| 25 | + // merge config common |
| 26 | + config = super.mergeConfig(config); |
| 27 | + config = this.applyConfig(config); |
| 28 | + |
| 29 | + try { |
| 30 | + await esbuild.build(config); |
| 31 | + } catch (error) { |
| 32 | + debug.error(error); |
| 33 | + throw Error(Messages.build.error.vulcan_build_failed); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Applies specific configurations to the ESBuild config. |
| 39 | + * @param {object} config - ESBuild configuration object. |
| 40 | + * @returns {object} - Updated ESBuild configuration object. |
| 41 | + */ |
| 42 | + applyConfig(config) { |
| 43 | + const updatedConfig = { ...config }; |
| 44 | + // use polyfill with useNodePolyfills and preset mode compute |
| 45 | + const useNodePolyfills = |
| 46 | + (this.builderConfig?.useNodePolyfills || |
| 47 | + this.customConfigPreset?.useNodePolyfills || |
| 48 | + this.customConfigLocal?.useNodePolyfills) && |
| 49 | + this.presetMode === 'compute'; |
| 50 | + |
| 51 | + if (!updatedConfig.plugins) updatedConfig.plugins = []; |
| 52 | + if (useNodePolyfills) { |
| 53 | + updatedConfig.plugins.push(ESBuildNodeModulePlugin(globalThis.buildProd)); |
| 54 | + } |
| 55 | + |
| 56 | + // plugin resolve azion: |
| 57 | + updatedConfig.plugins.push(ESBuildAzionModulePlugin(globalThis.buildProd)); |
| 58 | + |
| 59 | + // inject content in worker initial code. |
| 60 | + if (this.builderConfig.contentToInject) { |
| 61 | + const workerInitContent = this.builderConfig.contentToInject; |
| 62 | + |
| 63 | + if (updatedConfig.banner?.js) { |
| 64 | + updatedConfig.banner.js = `${updatedConfig.banner.js} ${workerInitContent}`; |
| 65 | + } else { |
| 66 | + updatedConfig.banner = { js: workerInitContent }; |
| 67 | + } |
| 68 | + } |
| 69 | + return updatedConfig; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export default Esbuild; |
0 commit comments