Utilities to generate JavaScript code.
# ✨ Auto-detect
npx nypm install knitwork
# npm
npm install knitwork
# yarn
yarn add knitwork
# pnpm
pnpm install knitwork
# bun
bun install knitwork
# deno
deno install knitwork
undefinedESM (Node.js, Bun, Deno)
import {} from "knitwork";
undefinedCommonJS (Legacy Node.js)
const {} = require("knitwork");
undefinedCDN (Deno, Bun and Browsers)
import {} from "https://esm.sh/knitwork";
genDynamicImport(specifier, options)Generate an ESM dynamic import() statement.
genExport(specifier, exports?, options)Generate an ESM export statement.
genImport(specifier, imports?, options)Generate an ESM import statement.
undefinedExample:undefined
genImport("pkg", "foo");
// ~> `import foo from "pkg";`
genImport("pkg", ["foo"]);
// ~> `import { foo } from "pkg";`
genImport("pkg", ["a", "b"]);
// ~> `import { a, b } from "pkg`;
genImport("pkg", [{ name: "default", as: "bar" }]);
// ~> `import { default as bar } from "pkg`;
genImport("pkg", [{ name: "foo", as: "bar" }]);
// ~> `import { foo as bar } from "pkg`;
genImport("pkg", "foo", { attributes: { type: "json" } });
// ~> `import foo from "pkg" with { type: "json" };
genExport("pkg", "foo");
// ~> `export foo from "pkg";`
genExport("pkg", ["a", "b"]);
// ~> `export { a, b } from "pkg";`
// export * as bar from "pkg"
genExport("pkg", { name: "*", as: "bar" });
// ~> `export * as bar from "pkg";`
genTypeImport(specifier, imports, options)Generate an ESM import type statement.
genArrayFromRaw(array, indent, options)Serialize an array to a string.
Values are not escaped or quoted.
undefinedExample:undefined
genArrayFromRaw([1, 2, 3])
// ~> `[1, 2, 3]`
genObjectFromRaw(object, indent, options)Serialize an object to a string.
Values are not escaped or quoted.
undefinedExample:undefined
genObjectFromRaw({ foo: "bar", test: '() => import("pkg")' })
// ~> `{ foo: bar, test: () => import("pkg") }`
genObjectFromRawEntries(array, indent, options)Serialize an array of key-value pairs to a string.
Values are not escaped or quoted.
genObjectFromValues(obj, indent, options)Serialize an object to a string.
Values are escaped and quoted if necessary.
undefinedExample:undefined
genObjectFromValues({ foo: "bar" })
// ~> `{ foo: "bar" }`
escapeString(id)Escape a string for use in a javascript string.
genSafeVariableName(name)Generate a safe javascript variable name.
genString(input, options)Generate a string with double or single quotes and handle escapes.
genAugmentation(specifier)Generate typescript declare module augmentation.
genInlineTypeImport(specifier, name, options)Generate an typescript typeof import() statement for default import.
genInterface(name, contents?, options, indent)Generate typescript interface.
genTypeExport(specifier, imports, options)Generate a typescript export type statement.
genTypeObject(object, indent)Generate typescript object type.
genObjectKey(key)Generate a safe javascript variable name for an object key.
wrapInDelimiters(lines, indent, delimiters, withComma)Wrap an array of strings in delimiters.
Published under the MIT license.
Made by @pi0, @danielroe and community 💛
🤖 auto updated with automd
We use cookies
We use cookies to analyze traffic and improve your experience. You can accept or reject analytics cookies.