Skip to content

Commit

Permalink
Support shallow builds and sourcemaps (bluesky-social#352)
Browse files Browse the repository at this point in the history
* Enable all modules to be built shallowly, externalizing their deps

* Setup shallow builds to modify package.json main

* Hoist update-main-to-dist to separate call, fix lex-cli and plc shallow builds

* Generate sourcemaps during build

* Tidy

* Hoist new build deps up to root
  • Loading branch information
devinivy authored Nov 16, 2022
1 parent e02671b commit 13aead1
Show file tree
Hide file tree
Showing 34 changed files with 445 additions and 94 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"devDependencies": {
"@babel/core": "^7.18.6",
"@babel/preset-env": "^7.18.6",
"@npmcli/package-json": "^3.0.0",
"@types/jest": "^28.1.4",
"@types/node": "^15.6.2",
"@typescript-eslint/eslint-plugin": "^5.38.1",
Expand All @@ -26,6 +27,8 @@
"babel-jest": "^28.1.2",
"dotenv": "^16.0.3",
"esbuild": "^0.14.48",
"esbuild-node-externals": "^1.5.0",
"esbuild-plugin-copy": "^1.6.0",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"jest": "^28.1.2",
Expand Down
22 changes: 22 additions & 0 deletions packages/api/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const pkgJson = require('@npmcli/package-json')
const { nodeExternalsPlugin } = require('esbuild-node-externals')

const buildShallow =
process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'

if (process.argv.includes('--update-main-to-dist')) {
return pkgJson
.load(__dirname)
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
.then((pkg) => pkg.save())
}

require('esbuild').build({
logLevel: 'info',
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
})
3 changes: 2 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"main": "src/index.ts",
"scripts": {
"codegen": "lex gen-api ./src/client ../../lexicons/com/atproto/*/* ../../lexicons/app/bsky/*/*",
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js",
"build": "node ./build.js",
"postbuild": "tsc --build tsconfig.build.json",
"update-main-to-dist": "node ./build.js --update-main-to-dist",
"test": "jest"
},
"license": "MIT",
Expand Down
22 changes: 22 additions & 0 deletions packages/auth/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const pkgJson = require('@npmcli/package-json')
const { nodeExternalsPlugin } = require('esbuild-node-externals')

const buildShallow =
process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'

if (process.argv.includes('--update-main-to-dist')) {
return pkgJson
.load(__dirname)
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
.then((pkg) => pkg.save())
}

require('esbuild').build({
logLevel: 'info',
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
})
2 changes: 1 addition & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint:fix": "yarn lint --fix",
"verify": "run-p prettier lint",
"verify:fix": "yarn prettier:fix && yarn lint:fix",
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js",
"build": "node ./build.js",
"postbuild": "tsc --build tsconfig.build.json"
},
"dependencies": {
Expand Down
22 changes: 22 additions & 0 deletions packages/aws-kms/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const pkgJson = require('@npmcli/package-json')
const { nodeExternalsPlugin } = require('esbuild-node-externals')

const buildShallow =
process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'

if (process.argv.includes('--update-main-to-dist')) {
return pkgJson
.load(__dirname)
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
.then((pkg) => pkg.save())
}

require('esbuild').build({
logLevel: 'info',
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
})
2 changes: 1 addition & 1 deletion packages/aws-kms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint:fix": "yarn lint --fix",
"verify": "run-p prettier lint",
"verify:fix": "yarn prettier:fix && yarn lint:fix",
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js",
"build": "node ./build.js",
"postbuild": "tsc --build tsconfig.build.json"
},
"dependencies": {
Expand Down
22 changes: 22 additions & 0 deletions packages/common/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const pkgJson = require('@npmcli/package-json')
const { nodeExternalsPlugin } = require('esbuild-node-externals')

const buildShallow =
process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'

if (process.argv.includes('--update-main-to-dist')) {
return pkgJson
.load(__dirname)
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
.then((pkg) => pkg.save())
}

require('esbuild').build({
logLevel: 'info',
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
})
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint:fix": "yarn lint --fix",
"verify": "run-p prettier lint",
"verify:fix": "yarn prettier:fix && yarn lint:fix",
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js",
"build": "node ./build.js",
"postbuild": "tsc --build tsconfig.build.json"
},
"dependencies": {
Expand Down
22 changes: 22 additions & 0 deletions packages/crypto/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const pkgJson = require('@npmcli/package-json')
const { nodeExternalsPlugin } = require('esbuild-node-externals')

const buildShallow =
process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'

if (process.argv.includes('--update-main-to-dist')) {
return pkgJson
.load(__dirname)
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
.then((pkg) => pkg.save())
}

require('esbuild').build({
logLevel: 'info',
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
})
2 changes: 1 addition & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint:fix": "yarn lint --fix",
"verify": "run-p prettier lint",
"verify:fix": "yarn prettier:fix && yarn lint:fix",
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js",
"build": "node ./build.js",
"postbuild": "tsc --build tsconfig.build.json"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/dev-env/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require('esbuild')
logLevel: 'info',
entryPoints: ['src/index.ts', 'src/cli.ts'],
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
external: [
Expand Down
3 changes: 1 addition & 2 deletions packages/dev-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"scripts": {
"build": "node ./build.js",
"postbuild" : "tsc --build tsconfig.build.json",
"postbuild": "tsc --build tsconfig.build.json",
"start": "node dist/cli.js",
"prettier": "prettier --check src/",
"prettier:fix": "prettier --write src/",
Expand All @@ -25,7 +25,6 @@
"get-port": "^6.1.2"
},
"devDependencies": {
"esbuild-plugin-copy": "^1.3.0",
"ts-node": "^10.8.1"
}
}
23 changes: 23 additions & 0 deletions packages/did-resolver/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const pkgJson = require('@npmcli/package-json')
const { nodeExternalsPlugin } = require('esbuild-node-externals')

const buildShallow =
process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'

if (process.argv.includes('--update-main-to-dist')) {
return pkgJson
.load(__dirname)
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
.then((pkg) => pkg.save())
}

require('esbuild').build({
logLevel: 'info',
entryPoints: ['src/index.ts'],
sourcemap: true,
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
})
2 changes: 1 addition & 1 deletion packages/did-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"lint:fix": "yarn lint --fix",
"verify": "run-p prettier lint",
"verify:fix": "yarn prettier:fix && yarn lint:fix",
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js",
"build": "node ./build.js",
"postbuild": "tsc --build tsconfig.build.json"
},
"dependencies": {
Expand Down
22 changes: 22 additions & 0 deletions packages/handle/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const pkgJson = require('@npmcli/package-json')
const { nodeExternalsPlugin } = require('esbuild-node-externals')

const buildShallow =
process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'

if (process.argv.includes('--update-main-to-dist')) {
return pkgJson
.load(__dirname)
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
.then((pkg) => pkg.save())
}

require('esbuild').build({
logLevel: 'info',
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
})
2 changes: 1 addition & 1 deletion packages/handle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint:fix": "yarn lint --fix",
"verify": "run-p prettier lint",
"verify:fix": "yarn prettier:fix && yarn lint:fix",
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js",
"build": "node ./build.js",
"postbuild": "tsc --build tsconfig.build.json"
},
"license": "MIT",
Expand Down
22 changes: 22 additions & 0 deletions packages/lex-cli/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const pkgJson = require('@npmcli/package-json')
const { nodeExternalsPlugin } = require('esbuild-node-externals')

const buildShallow =
process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'

if (process.argv.includes('--update-main-to-dist')) {
return pkgJson
.load(__dirname)
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
.then((pkg) => pkg.save())
}

require('esbuild').build({
logLevel: 'info',
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
})
2 changes: 1 addition & 1 deletion packages/lex-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint:fix": "yarn lint --fix",
"verify": "run-p prettier lint",
"verify:fix": "yarn prettier:fix && yarn lint:fix",
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js",
"build": "node ./build.js",
"postbuild": "tsc --build tsconfig.build.json"
},
"license": "MIT",
Expand Down
22 changes: 22 additions & 0 deletions packages/lexicon/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const pkgJson = require('@npmcli/package-json')
const { nodeExternalsPlugin } = require('esbuild-node-externals')

const buildShallow =
process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'

if (process.argv.includes('--update-main-to-dist')) {
return pkgJson
.load(__dirname)
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
.then((pkg) => pkg.save())
}

require('esbuild').build({
logLevel: 'info',
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
})
4 changes: 2 additions & 2 deletions packages/lexicon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"lint:fix": "yarn lint --fix",
"verify": "run-p prettier lint",
"verify:fix": "yarn prettier:fix && yarn lint:fix",
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js",
"postbuild" : "tsc --build tsconfig.build.json"
"build": "node ./build.js",
"postbuild": "tsc --build tsconfig.build.json"
},
"license": "MIT",
"dependencies": {
Expand Down
22 changes: 22 additions & 0 deletions packages/nsid/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const pkgJson = require('@npmcli/package-json')
const { nodeExternalsPlugin } = require('esbuild-node-externals')

const buildShallow =
process.argv.includes('--shallow') || process.env.ATP_BUILD_SHALLOW === 'true'

if (process.argv.includes('--update-main-to-dist')) {
return pkgJson
.load(__dirname)
.then((pkg) => pkg.update({ main: 'dist/index.js' }))
.then((pkg) => pkg.save())
}

require('esbuild').build({
logLevel: 'info',
entryPoints: ['src/index.ts'],
bundle: true,
sourcemap: true,
outdir: 'dist',
platform: 'node',
plugins: buildShallow ? [nodeExternalsPlugin()] : [],
})
7 changes: 3 additions & 4 deletions packages/nsid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
"lint:fix": "yarn lint --fix",
"verify": "run-p prettier lint",
"verify:fix": "yarn prettier:fix && yarn lint:fix",
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js",
"postbuild" : "tsc --build tsconfig.build.json"
"build": "node ./build.js",
"postbuild": "tsc --build tsconfig.build.json"
},
"license": "MIT",
"dependencies": {
}
"dependencies": {}
}
Loading

0 comments on commit 13aead1

Please sign in to comment.