Knitwork JavaScript Code Generation Utilities
Knitwork is a utility library designed for programmatic generation of JavaScript and TypeScript code. It provides functions to construct various language constructs, including ESM `import` and `export` statements, dynamic imports, and TypeScript-specific elements like type imports, interfaces, and module augmentations. The library also offers robust serialization utilities for converting JavaScript objects and arrays into string representations, with options for raw or escaped values, and string escaping functions. Currently at stable version 1.3.0, Knitwork is actively maintained within the unjs ecosystem, receiving regular updates with a focus on modern JavaScript practices (ESM-first) and TypeScript integration. Its key differentiator lies in offering granular control over code generation for tasks like bundler plugins, build-time optimizations, and code transformation, providing a lower-level API compared to full AST manipulation libraries.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` to import Knitwork in an ES module environment.fixChange your import statements to use ES module syntax: `import { genImport } from 'knitwork';` -
TypeError: (0, knitwork.genImport) is not a function
cause This error typically indicates an incorrect named import or a CommonJS context trying to access an ES module export incorrectly.fixEnsure you are using `import { functionName } from 'knitwork';` for named exports, and that your environment supports ES Modules or is correctly configured for transpilation if using CJS. -
Error: Cannot find module 'knitwork' or its corresponding type declarations.
cause The `knitwork` package is not installed or incorrectly resolved by your module system.fixRun `npm install knitwork`, `yarn add knitwork`, or `pnpm add knitwork` to install the package. If the error persists, check your module resolution configuration.
Warnings
- gotcha Knitwork is an ESM-first library. Direct `require()` statements for importing Knitwork utilities are not recommended and may lead to issues or require specific CommonJS transpilation setups. Always prefer ES module `import` syntax.
- gotcha When serializing objects, `genObjectFromRaw` does not escape or quote values, assuming they are raw code snippets. This can lead to invalid JavaScript if string values are passed directly. Use `genObjectFromValues` for general object serialization where values should be properly escaped and quoted.
- breaking Versions prior to v1.1.0 might not correctly respect the `singleQuotes` option in `genString`, potentially always outputting double quotes regardless of the setting. This was fixed in v1.1.0.
- gotcha Module augmentations generated with `genAugmentation` in versions prior to v1.3.0 might have included unnecessary commas, leading to syntax errors in some TypeScript environments. This was fixed in v1.3.0.
Install
-
npm install knitwork -
yarn add knitwork -
pnpm add knitwork
Imports
- genImport
const { genImport } = require('knitwork')import { genImport } from 'knitwork' - genObjectFromValues
import genObjectFromValues from 'knitwork'
import { genObjectFromValues } from 'knitwork' - genAugmentation
const genAugmentation = require('knitwork').genAugmentationimport { genAugmentation } from 'knitwork'
Quickstart
import { genImport, genObjectFromValues, genInterface, genExport, genString } from 'knitwork';
// Generate ESM imports
const importStatement = genImport('my-package', ['foo', { name: 'default', as: 'MyDefault' }]);
// ~> import { foo, default as MyDefault } from "my-package";
// Generate an object with escaped values
const dataObject = genObjectFromValues({
name: 'Knitwork Demo',
version: 1.0,
isActive: true,
config: {
url: 'https://example.com/api',
timeout: 5000
}
});
// ~> { name: "Knitwork Demo", version: 1, isActive: true, config: { url: "https://example.com/api", timeout: 5000 } }
// Generate a TypeScript interface
const myInterface = genInterface('MyOptions', `{
id: string;
value: number;
}`);
// ~> interface MyOptions {
// id: string;
// value: number;
// }
// Generate an export statement
const exportStatement = genExport('./types', { name: '*', as: 'MyTypes' });
// ~> export * as MyTypes from "./types";
console.log(importStatement + '\n\n' + dataObject + '\n\n' + myInterface + '\n\n' + exportStatement);