Skip to content

Commit

Permalink
Expose TS types & add instructions for writing declaration file
Browse files Browse the repository at this point in the history
  • Loading branch information
Smona committed Jun 22, 2021
1 parent ca5ce00 commit 877de88
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ browsers that don't support AudioWorklets, as well as interoperate seamlessly wi
Because of this, you have to use an AudioContext from `standardized-audio-context` when creating Faust nodes. If you want
to use this loader with a vanilla AudioContext, please submit an issue or PR!

### With Typescript

Typescript definitions are available for the imported modules. To automatically get the correct types when you
import a `.dsp` file, add a file ending in `.d.ts` to your project containing the following:

```ts
// faust-modules.d.ts

declare module "*.dsp" {
import { ProcessorLoader } from "faust-loader";
const loader: ProcessorLoader;
export = loader;
}
```

You may need to update your `tsconfig.json` to ensure declaration files in your
project are included by the Typescript compiler.

### With Tone.js

```ts
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "faust-loader",
"version": "1.0.0",
"version": "1.1.0",
"main": "dist/faustLoader.js",
"types": "dist/faustLoader.d.ts",
"scripts": {
"prepare": "install-peers",
"build": "tsc",
Expand Down
16 changes: 15 additions & 1 deletion src/faustLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { getOptions, interpolateName } from "loader-utils";
import { LoaderDefinitionFunction } from "webpack";
import util from "util";
import path from "path";
import os from "os";
import { exec as execCallback } from "child_process";
const exec = util.promisify(execCallback);
import fs from "fs-extra";
Expand All @@ -13,6 +12,21 @@ interface Options {
publicPath?: string;
}

export interface FaustAudioProcessorNode extends AudioWorkletNode {
getNumInputs(): number;
getNumOutputs(): number;
getParam(address: string): number;
getParams(): string[];
setParam(address: string, value: number): void;
getJson(): string;
getState(): Promise<Record<string, number>>;
destroy(): void;
}

export type ProcessorLoader = (
context: any
) => Promise<FaustAudioProcessorNode | null | undefined>;

const faustLoader: LoaderDefinitionFunction<Options> = async function (
content
) {
Expand Down

0 comments on commit 877de88

Please sign in to comment.