Skip to content

Commit

Permalink
feat: export sveld for programmatic usage (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym authored Jan 22, 2022
1 parent 34d9d5c commit 6782da6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ yarn add -D sveld
npm i -D sveld
```

### Set-up with Rollup
### Rollup

Import and add `sveld` as a plugin to your `rollup.config.js`.

Expand Down Expand Up @@ -194,9 +194,36 @@ Append `--json` or `--markdown` flags to generate documentation in JSON/Markdown
npx sveld --json --markdown
```

### Node.js

You can also use `sveld` programmatically in Node.js.

If no `input` is specified, `sveld` will infer the entry point based on the `package.json#svelte` field.

```js
const { sveld } = require("sveld");
const pkg = require("./package.json");

sveld({
input: "./src/index.js",
glob: true,
markdown: true,
markdownOptions: {
onAppend: (type, document, components) => {
if (type === "h1")
document.append("quote", `${components.size} components exported from ${pkg.name}@${pkg.version}.`);
},
},
json: true,
jsonOptions: {
outFile: "docs/src/COMPONENT_API.json",
},
});
```

### Publishing to NPM

Specify the entry point for the TypeScript definitions in your `package.json`.
TypeScript definitions are outputted to the `types` folder by default. Don't forget to include the folder in your `package.json` when publishing the package to NPM.

```diff
{
Expand Down
19 changes: 15 additions & 4 deletions src/get-svelte-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@ import * as path from "path";
export type SvelteEntryPoint = string;

/**
* Get the file path entrypoint for uncompiled Svelte source code
* Get the file path entry point for uncompiled Svelte source code
* Expects a "svelte" field in the consumer's `package.json`
*/
export function getSvelteEntry(): SvelteEntryPoint | null {
export function getSvelteEntry(entryPoint?: SvelteEntryPoint): SvelteEntryPoint | null {
if (entryPoint) {
const entry_path = path.join(process.cwd(), entryPoint);

if (fs.existsSync(entry_path)) {
return entry_path;
} else {
process.stdout.write(`Invalid entry point: ${entry_path}.\n`);
return null;
}
}

const pkg_path = path.join(process.cwd(), "package.json");

if (fs.existsSync(pkg_path)) {
const pkg: { svelte?: SvelteEntryPoint } = JSON.parse(fs.readFileSync(pkg_path, "utf-8"));

if (pkg.svelte !== undefined) return pkg.svelte;

process.stdout.write("Could not determine an entrypoint.\n");
process.stdout.write('Specify an entrypoint to your Svelte code in the "svelte" field of your package.json.\n');
process.stdout.write("Could not determine an entry point.\n");
process.stdout.write('Specify an entry point to your Svelte code in the "svelte" field of your package.json.\n');
return null;
} else {
process.stdout.write("Could not locate a package.json file.\n");
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default } from "./rollup-plugin";
export { default as ComponentParser } from "./ComponentParser";
export { cli } from "./cli";
export { sveld } from "./sveld";
18 changes: 18 additions & 0 deletions src/sveld.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getSvelteEntry } from "./get-svelte-entry";
import { PluginSveldOptions, generateBundle, writeOutput } from "./rollup-plugin";

interface SveldOptions extends PluginSveldOptions {
/**
* Specify the input to the uncompiled Svelte source.
* If no value is provided, `sveld` will attempt to infer
* the entry point from the `package.json#svelte` field.
*/
input?: string;
}

export async function sveld(opts?: SveldOptions) {
const input = getSvelteEntry(opts?.input);
if (input === null) return;
const result = await generateBundle(input, opts?.glob === true);
writeOutput(result, opts || {}, input);
}

0 comments on commit 6782da6

Please sign in to comment.