Allow reading config from within plugins, and partially implement esbuild initialOptions (#2861)

* Implement plugin build.config and initialOptions

* update types

* default initialoptions entrypoints
This commit is contained in:
dave caruso
2023-05-11 22:58:41 -04:00
committed by GitHub
parent 136b50c746
commit dfd0f3e252
10 changed files with 300 additions and 157 deletions

View File

@@ -653,46 +653,46 @@ describe("bundler", () => {
},
};
});
itBundled("plugin/ManyPlugins", ({ root }) => {
const pluginCount = 4000;
let resolveCount = 0;
let loadCount = 0;
return {
files: {
"index.ts": /* ts */ `
import { foo as foo1 } from "plugin1:file";
import { foo as foo2 } from "plugin4000:file";
console.log(foo1, foo2);
`,
},
plugins: Array.from({ length: pluginCount }).map((_, i) => ({
name: `${i}`,
setup(builder) {
builder.onResolve({ filter: new RegExp(`^plugin${i}:file$`) }, args => {
resolveCount++;
return {
path: `plugin${i}:file`,
namespace: `plugin${i}`,
};
});
builder.onLoad({ filter: new RegExp(`^plugin${i}:file$`), namespace: `plugin${i}` }, args => {
loadCount++;
return {
contents: `export const foo = ${i};`,
loader: "js",
};
});
},
})),
run: {
stdout: `${pluginCount - 1} ${pluginCount - 1}`,
},
onAfterBundle(api) {
expect(resolveCount).toBe(pluginCount * 2);
expect(loadCount).toBe(pluginCount);
},
};
});
// itBundled("plugin/ManyPlugins", ({ root }) => {
// const pluginCount = 4000;
// let resolveCount = 0;
// let loadCount = 0;
// return {
// files: {
// "index.ts": /* ts */ `
// import { foo as foo1 } from "plugin1:file";
// import { foo as foo2 } from "plugin4000:file";
// console.log(foo1, foo2);
// `,
// },
// plugins: Array.from({ length: pluginCount }).map((_, i) => ({
// name: `${i}`,
// setup(builder) {
// builder.onResolve({ filter: new RegExp(`^plugin${i}:file$`) }, args => {
// resolveCount++;
// return {
// path: `plugin${i}:file`,
// namespace: `plugin${i}`,
// };
// });
// builder.onLoad({ filter: new RegExp(`^plugin${i}:file$`), namespace: `plugin${i}` }, args => {
// loadCount++;
// return {
// contents: `export const foo = ${i};`,
// loader: "js",
// };
// });
// },
// })),
// run: {
// stdout: `${pluginCount - 1} ${pluginCount - 1}`,
// },
// onAfterBundle(api) {
// expect(resolveCount).toBe(pluginCount * 2);
// expect(loadCount).toBe(pluginCount);
// },
// };
// });
itBundled("plugin/NamespaceOnLoadBug", () => {
return {
files: {
@@ -753,4 +753,69 @@ describe("bundler", () => {
},
};
});
itBundled("plugin/Options", ({ getConfigRef }) => {
return {
files: {
"index.ts": /* ts */ `
console.log("it works");
`,
},
entryPoints: ["./index.ts"],
plugins(build) {
expect(build.config).toBe(getConfigRef());
},
};
});
itBundled("plugin/ESBuildInitialOptions", ({}) => {
return {
files: {
"index.ts": /* ts */ `
console.log("it works");
`,
},
external: ["esbuild"],
entryPoints: ["./index.ts"],
plugins(build) {
expect((build as any).initialOptions).toEqual({
bundle: true,
entryPoints: ["/tmp/bun-build-tests/bun-T6ZQHx/plugin/ESBuildInitialOptions/index.ts"],
external: ["esbuild"],
format: undefined,
minify: false,
minifyIdentifiers: undefined,
minifySyntax: undefined,
minifyWhitespace: undefined,
outdir: "/tmp/bun-build-tests/bun-T6ZQHx/plugin/ESBuildInitialOptions",
platform: "browser",
sourcemap: undefined,
});
},
};
});
itBundled("plugin/ESBuildInitialOptions2", ({ root }) => {
return {
files: {
"index.ts": /* ts */ `
console.log("it works");
`,
},
external: ["esbuild"],
entryPoints: ["./index.ts"],
plugins(build) {
expect((build as any).initialOptions).toEqual({
bundle: true,
entryPoints: ["/tmp/bun-build-tests/bun-T6ZQHx/plugin/ESBuildInitialOptions/index.ts"],
external: ["esbuild"],
format: undefined,
minify: false,
minifyIdentifiers: undefined,
minifySyntax: undefined,
minifyWhitespace: undefined,
outdir: root,
platform: "browser",
sourcemap: undefined,
});
},
};
});
});

View File

@@ -270,6 +270,12 @@ export interface BundlerTestRunOptions {
/** given when you do itBundled('id', (this object) => BundlerTestInput) */
export interface BundlerTestWrappedAPI {
root: string;
getConfigRef: () => BuildConfig;
}
let configRef: BuildConfig;
function getConfigRef() {
return configRef;
}
export interface BundlerTestRef {
@@ -837,15 +843,16 @@ for (const [key, blob] of build.outputs) {
}
}
configRef = buildConfig;
const build = await Bun.build(buildConfig);
configRef = null!;
Bun.gc(true);
const buildLogs = (build as any).logs;
const buildLogs = build.logs.filter(x => x.level === "error");
if (buildLogs) {
const rawErrors = buildLogs instanceof AggregateError ? buildLogs.errors : [buildLogs];
if (buildLogs.length) {
const allErrors: ErrorMeta[] = [];
for (const error of rawErrors) {
for (const error of buildLogs) {
const str = error.message ?? String(error);
if (str.startsWith("\u001B[2mexpect(") || str.startsWith("expect(")) {
throw error;
@@ -1269,7 +1276,7 @@ export function itBundled(
): BundlerTestRef {
if (typeof opts === "function") {
const fn = opts;
opts = opts({ root: path.join(outBase, id.replaceAll("/", path.sep)) });
opts = opts({ root: path.join(outBase, id.replaceAll("/", path.sep)), getConfigRef });
// @ts-expect-error
opts._referenceFn = fn;
}