-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: generate js and dts bundles (#722)
* ci: enable autotests for all pushes * build: bundle with esbuild * test: ignore vendor.js coverage * fix: use vendor chunk for repl * build(dts): rm redundant triple slashes * build: move external typings to optional deps relates #712 * chore(core): relax Options type * perf: replace node-fetch with node-fetch-native * build: turn off minify
- Loading branch information
1 parent
906786a
commit 2acb0f2
Showing
13 changed files
with
1,609 additions
and
212 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,86 @@ | ||
#!/usr/bin/env node | ||
|
||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import fs from 'fs/promises' | ||
import { generateDtsBundle } from 'dts-bundle-generator' | ||
import glob from 'fast-glob' | ||
|
||
const entry = { | ||
filePath: './src/vendor.ts', | ||
outFile: './build/vendor.d.ts', | ||
libraries: { | ||
allowedTypesLibraries: ['node'], // args['external-types'], | ||
inlinedLibraries: [ | ||
'@nodelib/fs.stat', | ||
'@nodelib/fs.scandir', | ||
'@nodelib/fs.walk', | ||
'fast-glob', | ||
'@types/jsonfile', | ||
'node-fetch-native', | ||
'chalk', | ||
'globby', | ||
'webpod', | ||
'@types/fs-extra', | ||
'@types/minimist', | ||
'@types/ps-tree', | ||
'@types/which', | ||
], // args['external-inlines'], | ||
}, | ||
output: { | ||
inlineDeclareExternals: true, | ||
inlineDeclareGlobals: true, | ||
sortNodes: false, | ||
exportReferencedTypes: false, //args['export-referenced-types'], | ||
}, | ||
} | ||
|
||
const compilationOptions = { | ||
preferredConfigPath: './tsconfig.prod.json', // args.project, | ||
followSymlinks: true, | ||
} | ||
|
||
let [result] = generateDtsBundle([entry], compilationOptions) | ||
|
||
// generateDtsBundle cannot handle the circular refs on types inlining, so we need to help it manually: | ||
/* | ||
build/vendor.d.ts(163,7): error TS2456: Type alias 'Options' circularly references itself. | ||
build/vendor.d.ts(164,7): error TS2456: Type alias 'Entry' circularly references itself. | ||
build/vendor.d.ts(165,7): error TS2456: Type alias 'Task' circularly references itself. | ||
build/vendor.d.ts(166,7): error TS2456: Type alias 'Pattern' circularly references itself. | ||
build/vendor.d.ts(167,7): error TS2456: Type alias 'FileSystemAdapter' circularly references itself. | ||
build/vendor.d.ts(197,48): error TS2694: Namespace 'FastGlob' has no exported member 'FastGlobOptions | ||
*/ | ||
|
||
result = result | ||
.replace('type Options = Options;', 'export {Options};') | ||
.replace('type Task = Task;', 'export {Task};') | ||
.replace('type Pattern = Pattern;', 'export {Pattern};') | ||
.replace('FastGlob.FastGlobOptions', 'FastGlob.Options') | ||
.replace('type Entry =', 'export type Entry =') | ||
|
||
await fs.writeFile(entry.outFile, result, 'utf8') | ||
|
||
// Replaces redundant triple-slash directives | ||
for (const dts of await glob(['build/**/*.d.ts', '!build/vendor.d.ts'])) { | ||
const contents = (await fs.readFile(dts, 'utf8')) | ||
.split('\n') | ||
.filter((line) => !line.startsWith('/// <reference types')) | ||
.join('\n') | ||
|
||
await fs.writeFile(dts, contents, 'utf8') | ||
} | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#!/usr/bin/env node | ||
|
||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import path from 'node:path' | ||
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/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 entries = entry.split(/,\s?/) | ||
const entryPoints = entry.includes('*') | ||
? await glob(entries, { absolute: false, onlyFiles: true, cwd, root: cwd }) | ||
: entries.map((p) => path.relative(cwd, path.resolve(cwd, p))) | ||
|
||
console.log('cwd=', cwd) | ||
console.log('entryPoints=', entryPoints) | ||
|
||
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: './build', | ||
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: './build', | ||
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
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 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 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
Oops, something went wrong.