Skip to content
当前有符合你浏览器所设置语言的版本,是否前往zh-CN版本的网站?
There is a version suitable for your browser's language settings. Would you like to go to the zh-CN language of the site?
HomeDocument

VS Code Template

H1

The VS Code extension from Milkio comes with a built-in template feature.

Unlike code snippets, templates can create multiple files at once. For example, we often need to write CRUD (Create, Read, Update, Delete) for certain features. With templates, we can create all these files at once.

The template feature is not only effective for Milkio projects; as long as the VS Code extension is installed, it can be used in all JavaScript projects. Are you ready to say goodbye to the days of manually typing <script lang="ts" setup>?

Installation

Milkio projects have it installed by default. For non-Milkio projects (like your Vue/React projects), you need to install it to use the templates.

Terminal window
bun add --dev milkio-template

Creating Templates

Create a file with any name in your /.templates directory.

/.templates/foo-template.ts
import { createTemplate } from "milkio-template";
import { join } from "path";
await createTemplate(async (tools) => {
return {
path: join(tools.directory(), `${tools.hyphen(tools.name())}.ts`),
content: `
console.log("Hello ${tools.name()}!")
`.trim(),
};
});

Tools

In createTemplate, you can access the toolkit through the tools object. The following methods are available.

NameResult TypeDescription
tools.namestringThe name entered when creating the template
tools.directorystringThe directory selected when creating the template
tools.srcstringThe relative path from the directory to the src directory, usually used for import paths
tools.hyphenstringConvert the name to a hyphenated name (foo-bar)
tools.humpstringConvert the name to PascalCase (FooBar)
tools.camelstringConvert the name to camelCase (fooBar)

Built-in Templates

Milkio does not provide built-in templates because each team’s technology stack may be different. If Milkio provides a template biased towards one technology stack, it will disappoint users who use a different stack. Moreover, built-in templates are not editable, which means users cannot adjust some details themselves to make the template more user-friendly.