UNPKG

@fleek-platform/functions-esbuild-config

Version:
65 lines (64 loc) 2.27 kB
import path from 'node:path'; import { moduleChecker } from './plugins/moduleChecker.js'; import { nodeProtocolImportSpecifier } from './plugins/nodeProtocolImportSpecifier.js'; export const defaultOptions = { bundle: true, logLevel: 'silent', platform: 'browser', format: 'esm', target: 'esnext', treeShaking: true, mainFields: ['browser', 'module', 'main'], minify: true, }; export const createFleekBuildConfig = (options) => { const { filePath, bundle, env, outFile = 'function.js', tempDir = '.fleek', onError = console.error } = options; const unsupportedModulesUsed = new Set(); const filePathWorkDir = path.dirname(filePath); const nodeModulesPath = path.join(filePathWorkDir, 'node_modules'); const plugins = [moduleChecker({ unsupportedModulesUsed })]; if (bundle) { plugins.push(nodeProtocolImportSpecifier({ onError })); } // Translate envs so we can use them in the define options const envs = Object.entries(env).reduce((acc, [key, value]) => ({ ...acc, [`process.env.${key}`]: `"${value}"` }), {}); const buildOptions = { ...defaultOptions, entryPoints: [filePath], bundle, outfile: path.join(tempDir, outFile), plugins, nodePaths: [nodeModulesPath], define: { ...envs, }, }; buildOptions.banner = { js: ` import { Buffer as _globalFleekBuffer } from "node:buffer"; import { AsyncLocalStorage as _globalFleekAsyncLocalStorage } from "node:async_hooks"; import { PerformanceObserver as _globalFleekPerformanceObserver, performance as _globalFleekPerformance } from 'node:perf_hooks'; globalThis.Buffer = _globalFleekBuffer; globalThis.AsyncLocalStorage = _globalFleekAsyncLocalStorage; globalThis.performance = _globalFleekPerformance; globalThis.PerformanceObserver = _globalFleekPerformanceObserver; globalThis.process = { ...globalThis.process, env: { ...globalThis.process.env, ${Object.entries(env) .map(([key, value]) => `${key}: "${value}"`) .join(',')} } }; globalThis.fleek = { env: { ${Object.entries(env) .map(([key, value]) => `${key}: "${value}"`) .join(',')} } }; `, }; return buildOptions; };