1. knitwork
Utilities to generate JavaScript code.
knitwork
Package: knitwork
Created by: unjs
Last modified: Sat, 30 Mar 2024 12:51:50 GMT
Version: 1.1.0
License: MIT
Downloads: 1,880,387
Repository: https://github.com/unjs/knitwork

Install

npm install knitwork
yarn add knitwork

🧶 knitwork

npm version
npm downloads
codecov

Utilities to generate JavaScript code.

Install

 # ✨ Auto-detect
npx nypm install knitwork

# npm
npm install knitwork

# yarn
yarn add knitwork

# pnpm
pnpm install knitwork

# bun
bun install knitwork

ESM (Node.js, Bun)

 import {} from "knitwork";

CommonJS (Legacy Node.js)

 const {} = require("knitwork");

CDN (Deno, Bun and Browsers)

 import {} from "https://esm.sh/knitwork";

ESM

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.

Example:

 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.

Serialization

genArrayFromRaw(array, indent, options)

Serialize an array to a string.

Example:

 genArrayFromRaw([1, 2, 3])
// ~> `[1, 2, 3]`

genObjectFromRaw(object, indent, options)

Serialize an object to a string.

Example:

 genObjectFromValues({ foo: "bar", test: '() => import("pkg")' })
// ~> `{ foo: bar, test: () => import("pkg") }`

genObjectFromRawEntries(array, indent, options)

Serialize an array of key-value pairs to a string.

genObjectFromValues(obj, indent, options)

Serialize an object to a string.

Example:

 genObjectFromValues({ foo: "bar" })
// ~> `{ foo: "bar" }`

String

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.

Typescript

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.

Contribution

Local development
  • Clone this repository
  • Install the latest LTS version of Node.js
  • Enable Corepack using corepack enable
  • Install dependencies using bun install
  • Run tests using bun dev

License

Published under the MIT license.
Made by @pi0, @danielroe and community 💛






🤖 auto updated with automd

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin