Skip to content
/ treetype Public

Generate TS module definitions based on a tree structure

License

Notifications You must be signed in to change notification settings

uwu/treetype

Repository files navigation

TreeType

TreeType generates module definitions for object types.

Install

npm    i -D @uwu/treetype
yarn add -D @uwu/treetype
pnpm add -D @uwu/treetype

Usage

treetype <definition file> [output file]

If the output file is not specified, standard output is used.

The definition file specifies which modules to generate, the format is as follows:

## These directives control some global options
# `source` specifies which file should be used as the source of all source types
\source ./types.ts
# `import` specifies how `source` should be imported in the generated file
\import ./types

# All top-level modules must have a source type, this source type must be exported
# All properties on the source type are declared exports on the generated module
@module from SourceType {
  # You may further specify a property inside a module to generate nested modules
  test
  hello {
    world
    foo
  }
  # You may override the source type for a portion of the module tree, this property should not exist on the parent source type
  bar from AnotherType
  baz from EvenMoreTypes {
    hello
    world
  }
}

Should any path or tree node need to contain whitespace, you can enclose it in quotes (").

Bundler usage

A bundler plugin will be provided in the future, until then, here are examples of how to use a bundler to make TreeType modules usable

Rollup

export default {
	output: {
		globals(id) {
			if (id.startsWith("@module")) return id.substring(1).replace(/\//g, ".");
		},
	},
};

esbuild

export default {
	name: "treetype",
	setup(build) {
		const namespace = "treetype";
		build.onResolve({ filter: /^@module\b/ }, (args) => ({
			path: args.path,
			namespace,
		}));
		build.onLoad({ filter: /.*/, namespace }, (args) => {
			return {
				contents: `module.exports = ${args.path.slice(1).replaceAll("/", ".")};`,
				loader: "js",
			};
		});
	},
}

About

Generate TS module definitions based on a tree structure

Resources

License

Stars

Watchers

Forks

Packages

No packages published