-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
build: generate js and dts bundles #722
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
aa5d31c
ci: enable autotests for all pushes
antongolub 2705246
Merge branch 'google:main' into main
antongolub 235170f
Merge branch 'google:main' into main
antongolub ee1e3a9
build: bundle with esbuild
antongolub 793f6ef
test: ignore vendor.js coverage
antongolub 9c17cd4
fix: use vendor chunk for repl
antongolub f8c80eb
build(dts): rm redundant triple slashes
antongolub 187185f
build: move external typings to optional deps
antongolub c5f54ac
chore(core): relax Options type
antongolub c275fba
perf: replace node-fetch with node-fetch-native
antongolub ed7a529
build: turn off minify
antongolub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😁