Skip to content

Commit

Permalink
build: apply esbuild-plugin-entry-chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Feb 1, 2024
1 parent 689d87b commit 61951ef
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 42 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
],
"scripts": {
"build": "concurrently 'npm:build:*'",
"build:esm": "node ./src/scripts/build.cjs",
"build:cjs": "node ./src/scripts/build.cjs --cjs",
"build:js": "node ./src/scripts/build.mjs",
"build:dts": "tsc --emitDeclarationOnly --skipLibCheck --outDir target/dts",
"build:docs": "typedoc --options src/main/typedoc",
"build:stamp": "npm_config_yes=true npx buildstamp",
Expand All @@ -47,9 +46,11 @@
"concurrently": "^8.2.2",
"esbuild": "^0.20.0",
"esbuild-node-externals": "^1.12.0",
"esbuild-plugin-entry-chunks": "^0.1.5",
"eslint": "^8.56.0",
"eslint-config-qiwi": "^2.1.3",
"fast-glob": "^3.3.2",
"minimist": "^1.2.8",
"ts-node": "^10.9.2",
"typedoc": "^0.25.7",
"typescript": "^5.3.3"
Expand Down
40 changes: 0 additions & 40 deletions src/scripts/build.cjs

This file was deleted.

99 changes: 99 additions & 0 deletions src/scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env node

import esbuild from 'esbuild'
import { nodeExternalsPlugin } from 'esbuild-node-externals'
import { entryChunksPlugin } from 'esbuild-plugin-entry-chunks'
import minimist from 'minimist'
import glob from 'fast-glob'

const argv = minimist(process.argv.slice(2), {
default: {
entry: './src/main/ts/index.ts',
external: 'node:*',
bundle: 'src', // 'all' | 'none'
license: 'eof',
minify: false,
sourcemap: false,
format: 'cjs,esm',
cwd: process.cwd()
},
boolean: ['minify', 'sourcemap', 'banner'],
string: ['entry', 'external', 'bundle', 'license', 'format', 'map', 'cwd']
})
const { entry, external, bundle, minify, sourcemap, license, format, cwd: _cwd } = argv

const plugins = []
const cwd = Array.isArray(_cwd) ? _cwd[_cwd.length - 1] : _cwd
const entryPoints = entry.includes('*')
? await glob(entry.split(':'), { absolute: false, onlyFiles: true, cwd })
: entry.split(':')

const _bundle = bundle !== 'none' && !process.argv.includes('--no-bundle')
const _external = _bundle
? external.split(',')
: undefined // https://github.com/evanw/esbuild/issues/1466

if (_bundle && entryPoints.length > 1) {
plugins.push(entryChunksPlugin())
}

if (bundle === 'src') {
// https://github.com/evanw/esbuild/issues/619
// https://github.com/pradel/esbuild-node-externals/pull/52
plugins.push(nodeExternalsPlugin())
}

const formats = format.split(',')
const banner = argv.banner && bundle === 'all'
? {
js: `
const require = (await import("node:module")).createRequire(import.meta.url);
const __filename = (await import("node:url")).fileURLToPath(import.meta.url);
const __dirname = (await import("node:path")).dirname(__filename);
`
}
: {}


const esmConfig = {
absWorkingDir: cwd,
entryPoints,
outdir: './target/esm',
bundle: _bundle,
external: _external,
minify,
sourcemap,
sourcesContent: false,
platform: 'node',
target: 'esnext',
format: 'esm',
outExtension: {
'.js': '.mjs'
},
plugins,
legalComments: license,
tsconfig: './tsconfig.json',
//https://github.com/evanw/esbuild/issues/1921
banner
}

const cjsConfig = {
...esmConfig,
outdir: './target/cjs',
target: 'es6',
format: 'cjs',
banner: {},
outExtension: {
'.js': '.cjs'
}
}

for (const format of formats) {
const config = format === 'cjs' ? cjsConfig : esmConfig

await esbuild
.build(config)
.catch(() => process.exit(1))
}

process.exit(0)
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,11 @@ define-properties@^1.1.3, define-properties@^1.2.0, define-properties@^1.2.1:
has-property-descriptors "^1.0.0"
object-keys "^1.1.1"

[email protected]:
version "0.2.4"
resolved "https://registry.yarnpkg.com/depseek/-/depseek-0.2.4.tgz#93c857191be14f73f6fef674c44f7dd88732af4e"
integrity sha512-MneWFr//kZfwpJNiVfb4QkFLU9e3G78ZoAxEHYhd7MuCIRRBqMDVq1kkyeOcFkvybLFwGsnO+VGuEjncmedczQ==

diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
Expand Down Expand Up @@ -878,6 +883,13 @@ esbuild-node-externals@^1.12.0:
find-up "^5.0.0"
tslib "^2.4.1"

esbuild-plugin-entry-chunks@^0.1.5:
version "0.1.5"
resolved "https://registry.yarnpkg.com/esbuild-plugin-entry-chunks/-/esbuild-plugin-entry-chunks-0.1.5.tgz#138d44fcf0f05c7b7b8a6cdef7cee0476515ffd3"
integrity sha512-i4kdnefSWxkriFOc5s45QwstkZRpqQ3xv96FqjLhi8/ZE11ekuaoU+VvHkvoG2p6RHqijY18WgXOskXnYs+Yaw==
dependencies:
depseek "0.2.4"

esbuild@^0.20.0:
version "0.20.0"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.20.0.tgz#a7170b63447286cd2ff1f01579f09970e6965da4"
Expand Down Expand Up @@ -1789,6 +1801,11 @@ minimatch@^9.0.3:
dependencies:
brace-expansion "^2.0.1"

minimist@^1.2.8:
version "1.2.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

[email protected]:
version "2.1.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
Expand Down

0 comments on commit 61951ef

Please sign in to comment.