-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(esbuild): dedupe esbuild config (#9875)
- Loading branch information
Showing
43 changed files
with
161 additions
and
563 deletions.
There are no files selected for viewing
This file contains 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,73 @@ | ||
import path from 'node:path' | ||
|
||
import * as esbuild from 'esbuild' | ||
import fg from 'fast-glob' | ||
import fs from 'fs-extra' | ||
|
||
export const defaultBuildOptions = { | ||
outdir: 'dist', | ||
|
||
platform: 'node', | ||
target: ['node20'], | ||
|
||
format: 'cjs', | ||
|
||
logLevel: 'info', | ||
|
||
// For visualizing dist. See: | ||
// - https://esbuild.github.io/api/#metafile | ||
// - https://esbuild.github.io/analyze/ | ||
metafile: true, | ||
} | ||
|
||
export const defaultPatterns = ['./src/**/*.{ts,js}'] | ||
export const defaultIgnorePatterns = ['**/__tests__', '**/*.test.{ts,js}'] | ||
|
||
/** | ||
* @typedef {{ | ||
* cwd?: string | ||
* buildOptions?: import('esbuild').BuildOptions | ||
* entryPointOptions?: { | ||
* patterns?: string[] | ||
* ignore?: string[] | ||
* } | ||
* metafileName?: string | ||
* }} BuildOptions | ||
* | ||
* @param {BuildOptions} options | ||
*/ | ||
export async function build({ | ||
cwd, | ||
buildOptions, | ||
entryPointOptions, | ||
metafileName, | ||
} = {}) { | ||
// Yarn and Nx both set this to the package's root dir path | ||
cwd ??= process.cwd() | ||
|
||
buildOptions ??= defaultBuildOptions | ||
metafileName ??= 'meta.json' | ||
|
||
// If the user didn't explicitly provide entryPoints, | ||
// then we'll use fg to find all the files in `${cwd}/src` | ||
let entryPoints = buildOptions.entryPoints | ||
|
||
if (!entryPoints) { | ||
const patterns = entryPointOptions?.patterns ?? defaultPatterns | ||
const ignore = entryPointOptions?.ignore ?? defaultIgnorePatterns | ||
|
||
entryPoints = await fg(patterns, { | ||
cwd, | ||
ignore, | ||
}) | ||
} | ||
|
||
const result = await esbuild.build({ | ||
entryPoints, | ||
...buildOptions, | ||
}) | ||
|
||
await fs.writeJSON(path.join(cwd, metafileName), result.metafile, { | ||
spaces: 2, | ||
}) | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,25 +1,3 @@ | ||
import fs from 'node:fs/promises' | ||
import { build } from '../../buildDefaults.mjs' | ||
|
||
import * as esbuild from 'esbuild' | ||
import fg from 'fast-glob' | ||
|
||
const sourceFiles = await fg.glob(['./src/**/*.ts'], { | ||
ignore: ['./src/**/__tests__'], | ||
}) | ||
|
||
const result = await esbuild.build({ | ||
entryPoints: sourceFiles, | ||
outdir: 'dist', | ||
|
||
format: 'cjs', | ||
platform: 'node', | ||
target: ['node20'], | ||
|
||
logLevel: 'info', | ||
|
||
// For visualizing the bundle. | ||
// See https://esbuild.github.io/api/#metafile and https://esbuild.github.io/analyze/. | ||
metafile: true, | ||
}) | ||
|
||
await fs.writeFile('meta.json', JSON.stringify(result.metafile, null, 2)) | ||
await build() |
This file contains 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 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 |
---|---|---|
@@ -1,57 +1,27 @@ | ||
import fs from 'node:fs/promises' | ||
|
||
import * as esbuild from 'esbuild' | ||
import fg from 'fast-glob' | ||
|
||
// ─── Package ───────────────────────────────────────────────────────────────── | ||
// | ||
// Types don't need to be transformed by esbuild, and the bin is bundled later. | ||
|
||
const sourceFiles = await fg.glob(['./src/**/*.ts'], { | ||
ignore: ['./src/__tests__', './src/types.ts', './src/bin.ts'], | ||
}) | ||
|
||
let result = await esbuild.build({ | ||
entryPoints: sourceFiles, | ||
outdir: 'dist', | ||
|
||
format: 'cjs', | ||
platform: 'node', | ||
target: ['node20'], | ||
|
||
logLevel: 'info', | ||
|
||
// For visualizing the bundle. | ||
// See https://esbuild.github.io/api/#metafile and https://esbuild.github.io/analyze/. | ||
metafile: true, | ||
import { | ||
build, | ||
defaultBuildOptions, | ||
defaultIgnorePatterns, | ||
} from '../../../buildDefaults.mjs' | ||
|
||
// Build the package. | ||
await build({ | ||
entryPointOptions: { | ||
ignore: [...defaultIgnorePatterns, './src/types.ts', './src/bin.ts'], | ||
}, | ||
}) | ||
|
||
await fs.writeFile('meta.json', JSON.stringify(result.metafile, null, 2)) | ||
|
||
// ─── Bin ───────────────────────────────────────────────────────────────────── | ||
// | ||
// We build the bin differently because it doesn't have to asynchronously import the handler. | ||
|
||
result = await esbuild.build({ | ||
entryPoints: ['./src/bin.ts'], | ||
outdir: 'dist', | ||
|
||
banner: { | ||
js: '#!/usr/bin/env node', | ||
// Build the bin. | ||
await build({ | ||
buildOptions: { | ||
...defaultBuildOptions, | ||
banner: { | ||
js: '#!/usr/bin/env node', | ||
}, | ||
bundle: true, | ||
entryPoints: ['./src/bin.ts'], | ||
minify: true, | ||
packages: 'external', | ||
}, | ||
|
||
bundle: true, | ||
minify: true, | ||
|
||
platform: 'node', | ||
target: ['node20'], | ||
packages: 'external', | ||
|
||
logLevel: 'info', | ||
|
||
// For visualizing the bundle. | ||
// See https://esbuild.github.io/api/#metafile and https://esbuild.github.io/analyze/. | ||
metafile: true, | ||
metafileName: 'meta.bin.json', | ||
}) | ||
|
||
await fs.writeFile('meta.bins.json', JSON.stringify(result.metafile, null, 2)) |
This file contains 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 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 |
---|---|---|
@@ -1,25 +1,3 @@ | ||
import fs from 'node:fs' | ||
import { build } from '../../../buildDefaults.mjs' | ||
|
||
import * as esbuild from 'esbuild' | ||
import fg from 'fast-glob' | ||
|
||
// Get source files | ||
const sourceFiles = fg.sync(['./src/**/*.ts']) | ||
|
||
// Build general source files | ||
const result = await esbuild.build({ | ||
entryPoints: sourceFiles, | ||
outdir: 'dist', | ||
|
||
format: 'cjs', | ||
platform: 'node', | ||
target: ['node20'], | ||
|
||
logLevel: 'info', | ||
|
||
// For visualizing dist. | ||
// See https://esbuild.github.io/api/#metafile and https://esbuild.github.io/analyze/. | ||
metafile: true, | ||
}) | ||
|
||
fs.writeFileSync('meta.json', JSON.stringify(result.metafile, null, 2)) | ||
await build() |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,27 +1,3 @@ | ||
import fs from 'node:fs' | ||
import { build } from '../../buildDefaults.mjs' | ||
|
||
import * as esbuild from 'esbuild' | ||
import fg from 'fast-glob' | ||
|
||
// Get source files | ||
const sourceFiles = fg.sync(['./src/**/*.ts'], { | ||
ignore: ['**/*.test.ts'], | ||
}) | ||
|
||
// Build general source files | ||
const result = await esbuild.build({ | ||
entryPoints: sourceFiles, | ||
outdir: 'dist', | ||
|
||
format: 'cjs', | ||
platform: 'node', | ||
target: ['node18'], | ||
|
||
logLevel: 'info', | ||
|
||
// For visualizing dist. | ||
// See https://esbuild.github.io/api/#metafile and https://esbuild.github.io/analyze/. | ||
metafile: true, | ||
}) | ||
|
||
fs.writeFileSync('meta.json', JSON.stringify(result.metafile, null, 2)) | ||
await build() |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,23 +1,3 @@ | ||
import fs from 'node:fs' | ||
import { build } from '../../buildDefaults.mjs' | ||
|
||
import * as esbuild from 'esbuild' | ||
import fg from 'fast-glob' | ||
|
||
const sourceFiles = fg.sync(['./src/**/*.ts'], { ignore: ['./src/__tests__'] }) | ||
|
||
const result = await esbuild.build({ | ||
entryPoints: sourceFiles, | ||
outdir: 'dist', | ||
|
||
format: 'cjs', | ||
platform: 'node', | ||
target: ['node20'], | ||
|
||
logLevel: 'info', | ||
|
||
// For visualizing dist. | ||
// See https://esbuild.github.io/api/#metafile and https://esbuild.github.io/analyze/. | ||
metafile: true, | ||
}) | ||
|
||
fs.writeFileSync('meta.json', JSON.stringify(result.metafile, null, 2)) | ||
await build() |
This file contains 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.