-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: apply esbuild-plugin-entry-chunks
- Loading branch information
1 parent
689d87b
commit 61951ef
Showing
4 changed files
with
119 additions
and
42 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
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 |
---|---|---|
@@ -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) |
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 |
---|---|---|
|
@@ -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" | ||
|
@@ -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== | ||
|
||
[email protected]: | ||
version "2.1.2" | ||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" | ||
|