@statewalker/webrun-bundler 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +178 -2
- package/dist/index.d.mts +225 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +571 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +63 -3
- package/src/bundle.test.ts +388 -0
- package/src/bundle.ts +245 -0
- package/src/index.ts +17 -0
- package/src/input-maps.ts +79 -0
- package/src/packages.ts +126 -0
- package/src/paths.ts +62 -0
- package/src/resolve.ts +116 -0
- package/src/types.ts +154 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import type { FilesApi } from "@statewalker/webrun-files";
|
|
2
|
+
import type { BuildOptions, build, Message, Metafile, transform } from "esbuild-wasm";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The esbuild API the bundler calls: `esbuild-wasm` (initialized, in browsers and
|
|
6
|
+
* workers) or native `esbuild` on Node. Only `build` and `transform` are used, and
|
|
7
|
+
* every file reaches esbuild through the bundler's plugin: esbuild never touches a
|
|
8
|
+
* file system itself.
|
|
9
|
+
*/
|
|
10
|
+
export interface Esbuild {
|
|
11
|
+
build: typeof build;
|
|
12
|
+
transform: typeof transform;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The fields of a `package.json` the resolver reads. */
|
|
16
|
+
export interface PackageManifest {
|
|
17
|
+
name?: string;
|
|
18
|
+
version?: string;
|
|
19
|
+
exports?: unknown;
|
|
20
|
+
imports?: unknown;
|
|
21
|
+
main?: string;
|
|
22
|
+
module?: string;
|
|
23
|
+
browser?: unknown;
|
|
24
|
+
dependencies?: Record<string, string>;
|
|
25
|
+
peerDependencies?: Record<string, string>;
|
|
26
|
+
optionalDependencies?: Record<string, string>;
|
|
27
|
+
devDependencies?: Record<string, string>;
|
|
28
|
+
[key: string]: unknown;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A tree of files the bundler reads: the project, or one npm package. Its files get
|
|
33
|
+
* ids `<prefix><path>` (`~/src/app.ts`, `npm:react@18.3.1/index.js`); the ids are what
|
|
34
|
+
* the source maps name, so every bundled line traces back to the user's file or to the
|
|
35
|
+
* package it came from.
|
|
36
|
+
*/
|
|
37
|
+
export interface Mount {
|
|
38
|
+
/** `~` for the project, `npm:<name>@<version>` for a package. */
|
|
39
|
+
prefix: string;
|
|
40
|
+
files: FilesApi;
|
|
41
|
+
/** The tree's directory in `files` (`/` for a package of its own, `/node_modules/x`). */
|
|
42
|
+
root: string;
|
|
43
|
+
/** Its `package.json`, when it has one. */
|
|
44
|
+
manifest?: PackageManifest;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Where an import comes from: a file of a mount. */
|
|
48
|
+
export interface Importer {
|
|
49
|
+
mount: Mount;
|
|
50
|
+
/** The importing file's path in `mount.files` (root included). */
|
|
51
|
+
path: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Finds the npm packages bare specifiers name: in `node_modules` folders of a FilesApi
|
|
56
|
+
* (`nodeModulesPackages`), fetched from a registry on demand (`npmPackages`), or both
|
|
57
|
+
* (`chainPackages`).
|
|
58
|
+
*/
|
|
59
|
+
export interface PackageStore {
|
|
60
|
+
/** The package `name` as `importer` sees it; `undefined` when it has none. */
|
|
61
|
+
get(name: string, importer: Importer): Promise<Mount | undefined>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A specifier left to the runtime (`cloudflare:workers`, `node:fs`): a string ending in `*` is a prefix. */
|
|
65
|
+
export type ExternalPattern = string | RegExp;
|
|
66
|
+
|
|
67
|
+
export interface BundleOptions {
|
|
68
|
+
/** `esbuild-wasm` (or native `esbuild` on Node). */
|
|
69
|
+
esbuild: Esbuild;
|
|
70
|
+
/** The project's files. */
|
|
71
|
+
files: FilesApi;
|
|
72
|
+
/** The project's directory in `files` (default `/`). */
|
|
73
|
+
root?: string;
|
|
74
|
+
/** The entry module, a path in the project (`/src/index.ts`). */
|
|
75
|
+
entry: string;
|
|
76
|
+
/** Where bare specifiers go (default: `node_modules` folders of `files`). */
|
|
77
|
+
packages?: PackageStore;
|
|
78
|
+
/** Specifiers kept as imports for the runtime. `http(s):` and `data:` URLs always are. */
|
|
79
|
+
external?: ExternalPattern[];
|
|
80
|
+
/**
|
|
81
|
+
* Modules the host supplies, by specifier (`cloudflare:workers`, `node:buffer`): their
|
|
82
|
+
* JavaScript source, or `undefined` to resolve the specifier as usual. They come before
|
|
83
|
+
* `external`. Their own imports resolve from the project's root; they name themselves
|
|
84
|
+
* `virtual:<specifier>` in the source map.
|
|
85
|
+
*/
|
|
86
|
+
modules?: Record<string, string> | ((specifier: string) => string | undefined);
|
|
87
|
+
/**
|
|
88
|
+
* Modules whose exports replace the globals of the same name in every bundled file
|
|
89
|
+
* (esbuild `inject`): specifiers or project paths, resolved like imports (`modules` too).
|
|
90
|
+
*/
|
|
91
|
+
inject?: string[];
|
|
92
|
+
/**
|
|
93
|
+
* `package.json` `exports` conditions besides `import`/`require` and `default`
|
|
94
|
+
* (default `["browser"]`; a workerd target takes `["workerd", "worker", "browser"]`).
|
|
95
|
+
*/
|
|
96
|
+
conditions?: string[];
|
|
97
|
+
/** Output format (default `esm`). */
|
|
98
|
+
format?: "esm" | "iife" | "cjs";
|
|
99
|
+
/** esbuild platform (default `browser`): sets `process.env.NODE_ENV` handling and the like. */
|
|
100
|
+
platform?: "browser" | "node" | "neutral";
|
|
101
|
+
/** Language target (default `es2022`). */
|
|
102
|
+
target?: string | string[];
|
|
103
|
+
/**
|
|
104
|
+
* Lower `async`/`await`, async generators and `for await` to generators, so that each
|
|
105
|
+
* `await` resumes through `Promise.prototype.then` (an AsyncLocalStorage polyfill can then
|
|
106
|
+
* carry its context through it). Source maps follow the lowered code.
|
|
107
|
+
*/
|
|
108
|
+
lowerAwait?: boolean;
|
|
109
|
+
minify?: boolean;
|
|
110
|
+
define?: Record<string, string>;
|
|
111
|
+
/** Name of the output file: the map is linked as `<outfile>.map` (default `bundle.js`). */
|
|
112
|
+
outfile?: string;
|
|
113
|
+
/** `external` (default): `code` links `<outfile>.map`, which `map` holds; `inline`: `code` carries it too. */
|
|
114
|
+
sourcemap?: "external" | "inline";
|
|
115
|
+
/** Other esbuild options, applied last (the bundler's plugin and source maps stay). */
|
|
116
|
+
esbuildOptions?: Partial<BuildOptions>;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A source map (version 3). */
|
|
120
|
+
export interface SourceMap {
|
|
121
|
+
version: 3;
|
|
122
|
+
file?: string;
|
|
123
|
+
sources: string[];
|
|
124
|
+
sourcesContent?: (string | null)[];
|
|
125
|
+
names: string[];
|
|
126
|
+
mappings: string;
|
|
127
|
+
sourceRoot?: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface BundleResult {
|
|
131
|
+
code: string;
|
|
132
|
+
/** The bundle's source map: sources are file ids (`~/src/app.ts`, `npm:pkg@1.0.0/src/x.ts`). */
|
|
133
|
+
map: SourceMap;
|
|
134
|
+
metafile: Metafile;
|
|
135
|
+
warnings: Message[];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface TranspileOptions {
|
|
139
|
+
esbuild: Esbuild;
|
|
140
|
+
/** The file's id or path: names it in the source map and picks the loader by extension. */
|
|
141
|
+
path: string;
|
|
142
|
+
format?: "esm" | "iife" | "cjs";
|
|
143
|
+
target?: string | string[];
|
|
144
|
+
lowerAwait?: boolean;
|
|
145
|
+
minify?: boolean;
|
|
146
|
+
define?: Record<string, string>;
|
|
147
|
+
sourcemap?: "external" | "inline";
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface TranspileResult {
|
|
151
|
+
code: string;
|
|
152
|
+
map: SourceMap;
|
|
153
|
+
warnings: Message[];
|
|
154
|
+
}
|