Skip to content

Commit

Permalink
feat: expose programmatic api (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Apr 14, 2023
1 parent 7232536 commit c030929
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ npx esbuild-jest [...optional-jest-arguments]

You can pass any Jest arguments just like you're running Jest itself.

### Advanced Usage

You can also use the CLI programmatically – especially useful if you are wrapping it in a custom script:

```js
import { build } from 'esbuild-jest-cli';
import myPlugin from './my-esbuild-plugin.js';

await build({
esbuild: {
outdir: 'dist',
sourcemap: true,
plugins: [myPlugin()],
},
package: {
name: 'custom-name',
},
});
```

Configuration
-------------

Expand Down Expand Up @@ -103,6 +123,19 @@ In the `esbuild` section, you can override any `esbuild` option except for a few
Any other valid [esbuild configuration options](https://esbuild.github.io/api/#options) are supported as expected.

In `package` section, you can override fields in the generated `package.json`, such as `name`, `scripts`, etc.
It is also possible to pass a function to modify its contents, e.g.:

```js
module.exports = {
esbuild: {
// ...
},
package: (p) => ({
...p,
customField: 'customValue',
}),
};
```

Troubleshooting
---------------
Expand Down
7 changes: 5 additions & 2 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env node

import { cosmiconfig } from 'cosmiconfig';
import { build } from './index.mjs';

try {
await build();
const explorer = cosmiconfig('esbuild-jest');
const esbuildJestBaseConfig = await explorer.search();
await build(esbuildJestBaseConfig.config);
} catch (e) {
console.error(`${e}`);
console.error(`${e.stack || e}`);
process.exit(1);
}
19 changes: 12 additions & 7 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cosmiconfig } from 'cosmiconfig';
import merge from 'lodash.merge';
import { build as esbuild } from 'esbuild';

import esbuildJest from './plugin.mjs';
Expand All @@ -7,13 +7,10 @@ import {convertPathToImport} from "./utils/resolve-module.mjs";
import {importViaChain} from "./utils/resolve-via-chain.mjs";
import {JEST_DEPENDENCIES} from "./utils/jestDependencies.mjs";

const explorer = cosmiconfig('esbuild-jest');

export async function build() {
export async function build(esbuildJestConfig = {}) {
const rootDir = process.cwd();

const esbuildJestBaseConfig = await explorer.search(rootDir);
const esbuildBaseConfig = esbuildJestBaseConfig ? esbuildJestBaseConfig.config.esbuild : {};
const esbuildBaseConfig = { ...esbuildJestConfig.esbuild };
const externalModules = [
...JEST_DEPENDENCIES,
...(esbuildBaseConfig.external || []),
Expand Down Expand Up @@ -68,11 +65,19 @@ export async function build() {
globalConfig,
projectConfig,
tests: tests.map(t => t.path),
package: esbuildJestBaseConfig && esbuildJestBaseConfig.config.package,
package: wrapPackageMiddleware(esbuildJestConfig.package),
}),
...(esbuildBaseConfig.plugins || []),
],
});

return buildResult;
}

function wrapPackageMiddleware(config) {
return typeof config === 'function' ? config : createPackageMerger(config)
}

function createPackageMerger(override) {
return (pkg) => merge(pkg, override)
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"homepage": "https://github.com/wix-incubator/esbuild-jest-cli#readme",
"dependencies": {
"cosmiconfig": "^8.1.3",
"lodash.merge": "^4.6.2",
"esbuild": "^0.17.4",
"import-from": "^4.0.0",
"resolve-from": "^5.0.0"
Expand Down

0 comments on commit c030929

Please sign in to comment.