|
| 1 | +import type { BuildOptions as ESBuildOptions } from 'esbuild'; |
| 2 | +import fs from 'fs-extra'; |
| 3 | +import { join } from 'path'; |
| 4 | + |
| 5 | +import { BuildOptions } from '../utils/options'; |
| 6 | +import { writePkgJson } from '../utils/write-pkg-json'; |
| 7 | +import { getBaseEsbuildOptions } from './utils'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Get an object containing ESbuild options to build the internal app globals |
| 11 | + * file. This function also performs relevant side-effects, like writing a |
| 12 | + * `package.json` file to disk. |
| 13 | + * |
| 14 | + * @param opts build options |
| 15 | + * @returns a Promise wrapping an array of ESbuild option objects |
| 16 | + */ |
| 17 | +export async function getInternalAppGlobalsBundles(opts: BuildOptions): Promise<ESBuildOptions[]> { |
| 18 | + const appGlobalsBuildDir = join(opts.buildDir, 'app-globals'); |
| 19 | + const appGlobalsSrcDir = join(opts.srcDir, 'app-globals'); |
| 20 | + const outputInternalAppDataDir = join(opts.output.internalDir, 'app-globals'); |
| 21 | + |
| 22 | + await fs.emptyDir(outputInternalAppDataDir); |
| 23 | + |
| 24 | + // copy @stencil/core/internal/app-globals/index.d.ts |
| 25 | + await fs.copyFile(join(appGlobalsBuildDir, 'index.d.ts'), join(outputInternalAppDataDir, 'index.d.ts')); |
| 26 | + |
| 27 | + // write @stencil/core/internal/app-globals/package.json |
| 28 | + writePkgJson(opts, outputInternalAppDataDir, { |
| 29 | + name: '@stencil/core/internal/app-globals', |
| 30 | + description: 'Used for default app globals.', |
| 31 | + main: 'index.js', |
| 32 | + module: 'index.js', |
| 33 | + sideEffects: false, |
| 34 | + }); |
| 35 | + |
| 36 | + const appGlobalsBaseOptions: ESBuildOptions = { |
| 37 | + ...getBaseEsbuildOptions(), |
| 38 | + entryPoints: [join(appGlobalsSrcDir, 'index.ts')], |
| 39 | + platform: 'node', |
| 40 | + }; |
| 41 | + |
| 42 | + const appGlobalsESM: ESBuildOptions = { |
| 43 | + ...appGlobalsBaseOptions, |
| 44 | + format: 'esm', |
| 45 | + outfile: join(outputInternalAppDataDir, 'index.js'), |
| 46 | + }; |
| 47 | + |
| 48 | + return [appGlobalsESM]; |
| 49 | +} |
0 commit comments