From 61951efab55aeea825c287b98cecb659f7cbd320 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Thu, 1 Feb 2024 16:52:24 +0300 Subject: [PATCH] build: apply esbuild-plugin-entry-chunks --- package.json | 5 ++- src/scripts/build.cjs | 40 ----------------- src/scripts/build.mjs | 99 +++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 17 ++++++++ 4 files changed, 119 insertions(+), 42 deletions(-) delete mode 100644 src/scripts/build.cjs create mode 100644 src/scripts/build.mjs diff --git a/package.json b/package.json index 945b129..8b9eb64 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/src/scripts/build.cjs b/src/scripts/build.cjs deleted file mode 100644 index 34d912f..0000000 --- a/src/scripts/build.cjs +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env node - -const esbuild = require('esbuild') -const { nodeExternalsPlugin } = require('esbuild-node-externals') - -const esmConfig = { - entryPoints: ['./src/main/ts/index.ts'], - outdir: './target/esm', - bundle: true, - minify: true, - sourcemap: true, - sourcesContent: false, - platform: 'node', - target: 'ES2020', - format: 'esm', - outExtension: { - '.js': '.mjs' - }, - external: ['node:*'], // https://github.com/evanw/esbuild/issues/1466 - plugins: [nodeExternalsPlugin()], // https://github.com/evanw/esbuild/issues/619 - tsconfig: './tsconfig.json' -} - -const cjsConfig = { - ...esmConfig, - outdir: './target/cjs', - target: 'es6', - format: 'cjs', - outExtension: { - '.js': '.cjs' - } -} - -const config = process.argv.includes('--cjs') - ? cjsConfig - : esmConfig - -esbuild - .build(config) - .catch(() => process.exit(1)) diff --git a/src/scripts/build.mjs b/src/scripts/build.mjs new file mode 100644 index 0000000..cc719a5 --- /dev/null +++ b/src/scripts/build.mjs @@ -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) \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 20c532f..b85ddbc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" +depseek@0.2.4: + 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" @@ -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" @@ -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== + ms@2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"