Skip to content

Commit

Permalink
chore(esbuild): dedupe esbuild config (#9875)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtoar committed Jan 24, 2024
1 parent e94dbf5 commit f1cdb71
Show file tree
Hide file tree
Showing 43 changed files with 161 additions and 563 deletions.
73 changes: 73 additions & 0 deletions buildDefaults.mjs
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,
})
}
1 change: 1 addition & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"!{projectRoot}/**/*.test.{js,jsx,ts,tsx}",
"{workspaceRoot}/babel.config.js",
"{workspaceRoot}/tsconfig.json",
"{workspaceRoot}/buildDefaults.mjs",
{
"runtime": "node -v"
},
Expand Down
26 changes: 2 additions & 24 deletions packages/babel-config/build.mjs
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()
1 change: 0 additions & 1 deletion packages/babel-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"@types/babel__core": "7.20.4",
"@types/node": "20.10.4",
"babel-plugin-tester": "11.0.4",
"esbuild": "0.19.9",
"jest": "29.7.0"
},
"gitHead": "3905ed045508b861b495f8d5630d76c7a157d8f1"
Expand Down
76 changes: 23 additions & 53 deletions packages/cli-packages/dataMigrate/build.mjs
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))
2 changes: 0 additions & 2 deletions packages/cli-packages/dataMigrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
"@prisma/client": "5.7.0",
"@types/fs-extra": "11.0.4",
"@types/yargs": "17.0.32",
"esbuild": "0.19.9",
"fast-glob": "3.3.2",
"jest": "29.7.0",
"memfs": "4.6.0",
"typescript": "5.3.3"
Expand Down
26 changes: 2 additions & 24 deletions packages/cli-packages/storybook/build.mjs
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()
8 changes: 0 additions & 8 deletions packages/cli-packages/storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
"prepublishOnly": "NODE_ENV=production yarn build"
},
"jest": {
"testPathIgnorePatterns": [
"/dist/"
]
},
"dependencies": {
"@redwoodjs/cli-helpers": "6.0.7",
"@redwoodjs/project-config": "6.0.7",
Expand All @@ -40,9 +35,6 @@
},
"devDependencies": {
"@types/yargs": "17.0.32",
"esbuild": "0.19.9",
"fast-glob": "3.3.2",
"jest": "29.7.0",
"typescript": "5.3.3"
},
"gitHead": "3905ed045508b861b495f8d5630d76c7a157d8f1"
Expand Down
1 change: 0 additions & 1 deletion packages/context/.babelrc.js

This file was deleted.

28 changes: 2 additions & 26 deletions packages/context/build.mjs
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()
8 changes: 0 additions & 8 deletions packages/context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@
"build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
"prepublishOnly": "NODE_ENV=production yarn build"
},
"jest": {
"testPathIgnorePatterns": [
"/dist/"
]
},
"devDependencies": {
"esbuild": "0.19.9",
"fast-glob": "3.3.2",
"jest": "29.7.0",
"typescript": "5.3.3"
},
"gitHead": "3905ed045508b861b495f8d5630d76c7a157d8f1"
Expand Down
1 change: 0 additions & 1 deletion packages/create-redwood-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"check-node-version": "4.2.1",
"ci-info": "4.0.0",
"envinfo": "7.11.0",
"esbuild": "0.19.9",
"execa": "5.1.1",
"fs-extra": "11.2.0",
"jest": "29.7.0",
Expand Down
34 changes: 11 additions & 23 deletions packages/create-redwood-app/scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/* eslint-env node */

import * as esbuild from 'esbuild'
import fs from 'fs-extra'
import { build, defaultBuildOptions } from '../../../buildDefaults.mjs'

const jsBanner = `\
#!/usr/bin/env node
Expand All @@ -11,24 +8,15 @@ const __filename = (await import("node:url")).fileURLToPath(import.meta.url);
const __dirname = (await import("node:path")).dirname(__filename);
`

const result = await esbuild.build({
entryPoints: ['src/create-redwood-app.js'],
outdir: 'dist',

platform: 'node',
target: ['node20'],
format: 'esm',
bundle: true,
banner: {
js: jsBanner,
await build({
buildOptions: {
...defaultBuildOptions,
banner: {
js: jsBanner,
},
bundle: true,
entryPoints: ['src/create-redwood-app.js'],
format: 'esm',
minify: true,
},

minify: true,

logLevel: 'info',
metafile: true,
})

await fs.writeJSON(new URL('./meta.json', import.meta.url), result.metafile, {
spaces: 2,
})
24 changes: 2 additions & 22 deletions packages/eslint-plugin/build.mjs
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()
2 changes: 0 additions & 2 deletions packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
"@types/eslint": "8",
"@types/estree": "1.0.5",
"@typescript-eslint/parser": "5.62.0",
"esbuild": "0.19.9",
"fast-glob": "3.3.2",
"glob": "10.3.10",
"tsx": "4.6.2",
"typescript": "5.3.3"
Expand Down
Loading

0 comments on commit f1cdb71

Please sign in to comment.