Skip to content

Commit 80b7bd7

Browse files
authored
feat(CTA): add initial vite support with vue and vue-ts (#1467)
1 parent 86f62d3 commit 80b7bd7

13 files changed

Lines changed: 278 additions & 10 deletions

File tree

.changes/cta-vite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-tauri-app": patch
3+
---
4+
5+
Add initial `vite` support starting with `vue` and `vue-ts`

tooling/create-tauri-app/bin/create-tauri-app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function printUsage() {
8989
--window-title, -W Window title of your Tauri application
9090
--dist-dir, -D Web assets location, relative to <project-dir>/src-tauri
9191
--dev-path, -P Url of your dev server
92-
--recipe, -r Add UI framework recipe. None by default.
92+
--recipe, -r Add UI framework recipe. None by default.
9393
Supported recipes: [${recipeShortNames.join("|")}]
9494
`);
9595
}
@@ -130,7 +130,7 @@ const getOptionsInteractive = (argv) => {
130130
);
131131
runInit();
132132
} else {
133-
// Something else when wrong
133+
// Something else went wrong
134134
console.error("An unknown error occurred:", error);
135135
}
136136
});

tooling/create-tauri-app/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { map, find } from "lodash";
66
import { TauriBuildConfig } from "./types/config";
77
import { reactjs, reactts } from "./recipes/react";
88
import { vanillajs } from "./recipes/vanilla";
9+
import { vite } from "./recipes/vite";
910

1011
export { shell } from "./shell";
1112
export { install, checkPackageManager } from "./dependency-manager";
@@ -43,7 +44,7 @@ export interface Recipe {
4344
}) => Promise<void>;
4445
}
4546

46-
export const allRecipes: Recipe[] = [vanillajs, reactjs, reactts];
47+
export const allRecipes: Recipe[] = [vanillajs, reactjs, reactts, vite];
4748

4849
export const recipeNames: Array<[string, string]> = map(
4950
allRecipes,

tooling/create-tauri-app/src/recipes/react.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ const afterCra = async (cwd: string, appName: string, version: string) => {
2424
const reactjs: Recipe = {
2525
descriptiveName: "React.js",
2626
shortName: "reactjs",
27-
configUpdate: ({ cfg }) => ({
27+
configUpdate: ({ cfg, packageManager }) => ({
2828
...cfg,
2929
distDir: `../build`,
3030
devPath: "http://localhost:3000",
31-
beforeDevCommand: `npm start`,
32-
beforeBuildCommand: `npm build`,
31+
beforeDevCommand: `${packageManager === "yarn" ? "yarn" : "npm run"} start`,
32+
beforeBuildCommand: `${
33+
packageManager === "yarn" ? "yarn" : "npm run"
34+
} build`,
3335
}),
3436
extraNpmDevDependencies: [],
3537
extraNpmDependencies: [],
@@ -57,7 +59,7 @@ const reactjs: Recipe = {
5759
postInit: async ({ packageManager }) => {
5860
console.log(`
5961
Your installation completed.
60-
To start, run ${packageManager} tauri dev
62+
To start, run ${packageManager === "yarn" ? "yarn" : "npm run"} tauri dev
6163
`);
6264
},
6365
};
@@ -98,7 +100,7 @@ const reactts: Recipe = {
98100
postInit: async ({ packageManager }) => {
99101
console.log(`
100102
Your installation completed.
101-
To start, run ${packageManager} tauri dev
103+
To start, run ${packageManager === "yarn" ? "yarn" : "npm run"} tauri dev
102104
`);
103105
},
104106
};

tooling/create-tauri-app/src/recipes/vanilla.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ install dependencies:
4141
$ ${packageManager} install
4242
4343
run the app:
44-
$ ${packageManager} tauri ${packageManager === "npm" ? "-- " : ""}dev
44+
$ ${packageManager === "yarn" ? "yarn" : "npm run"} tauri ${
45+
packageManager === "npm" ? "-- " : ""
46+
}dev
4547
`);
4648
},
4749
};
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { Recipe } from "..";
2+
import { join } from "path";
3+
import { readdirSync } from "fs";
4+
//@ts-ignore
5+
import scaffe from "scaffe";
6+
import { shell } from "../shell";
7+
import inquirer from "inquirer";
8+
9+
const afterViteCA = async (
10+
cwd: string,
11+
appName: string,
12+
version: string,
13+
template: string
14+
) => {
15+
const templateDir = join(__dirname, `../src/templates/vite/${template}`);
16+
const variables = {
17+
name: appName,
18+
tauri_version: version,
19+
};
20+
21+
try {
22+
await scaffe.generate(templateDir, join(cwd, appName), {
23+
overwrite: true,
24+
variables,
25+
});
26+
} catch (err) {
27+
console.log(err);
28+
}
29+
};
30+
31+
const vite: Recipe = {
32+
descriptiveName: "Vite backed recipe",
33+
shortName: "vite",
34+
configUpdate: ({ cfg, packageManager }) => ({
35+
...cfg,
36+
distDir: `../dist`,
37+
devPath: "http://localhost:3000",
38+
beforeDevCommand: `${packageManager === "yarn" ? "yarn" : "npm run"} start`,
39+
beforeBuildCommand: `${
40+
packageManager === "yarn" ? "yarn" : "npm run"
41+
} build`,
42+
}),
43+
extraNpmDevDependencies: [],
44+
extraNpmDependencies: [],
45+
preInit: async ({ cwd, cfg, packageManager }) => {
46+
try {
47+
const { template } = await inquirer.prompt([
48+
{
49+
type: "list",
50+
name: "template",
51+
message: "Which vite template would you like to use?",
52+
choices: readdirSync(join(__dirname, "../src/templates/vite")),
53+
default: "vue",
54+
},
55+
]);
56+
57+
// Vite creates the folder for you
58+
if (packageManager === "yarn") {
59+
await shell(
60+
"yarn",
61+
[
62+
"create",
63+
"@vitejs/app",
64+
`${cfg.appName}`,
65+
"--template",
66+
`${template}`,
67+
],
68+
{
69+
cwd,
70+
}
71+
);
72+
} else {
73+
await shell(
74+
"npm",
75+
[
76+
"init",
77+
"@vitejs/app",
78+
`${cfg.appName}`,
79+
"--",
80+
"--template",
81+
`${template}`,
82+
],
83+
{
84+
cwd,
85+
}
86+
);
87+
}
88+
89+
const version = await shell("npm", ["view", "tauri", "version"], {
90+
stdio: "pipe",
91+
});
92+
const versionNumber = version.stdout.trim();
93+
await afterViteCA(cwd, cfg.appName, versionNumber, template);
94+
} catch (error) {
95+
if (error.isTtyError) {
96+
// Prompt couldn't be rendered in the current environment
97+
console.log(
98+
"It appears your terminal does not support interactive prompts. Using default values."
99+
);
100+
} else {
101+
// Something else went wrong
102+
console.error("An unknown error occurred:", error);
103+
}
104+
}
105+
},
106+
postInit: async ({ packageManager }) => {
107+
console.log(`
108+
Your installation completed.
109+
To start, run ${packageManager === "yarn" ? "yarn" : "npm run"} tauri ${
110+
packageManager === "npm" ? "--" : ""
111+
} dev
112+
`);
113+
},
114+
};
115+
116+
export { vite };
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<template>
2+
<div>
3+
<img alt="Vue logo" src="./assets/logo.png" />
4+
<img alt="Tauri logo" height="200" src="./assets/tauri.svg" />
5+
</div>
6+
<HelloWorld msg="Hello Vue 3 + TypeScript + Vite + Tauri" />
7+
<a
8+
style="color: #42b983;"
9+
href="https://tauri.studio"
10+
target="_blank"
11+
rel="noopener noreferrer"
12+
>Learn Tauri</a>
13+
</template>
14+
15+
<script lang="ts">
16+
import { defineComponent } from 'vue'
17+
import HelloWorld from './components/HelloWorld.vue'
18+
19+
export default defineComponent({
20+
name: 'App',
21+
components: {
22+
HelloWorld
23+
}
24+
})
25+
</script>
26+
27+
<style>
28+
#app {
29+
font-family: Avenir, Helvetica, Arial, sans-serif;
30+
-webkit-font-smoothing: antialiased;
31+
-moz-osx-font-smoothing: grayscale;
32+
text-align: center;
33+
color: #2c3e50;
34+
margin-top: 60px;
35+
}
36+
</style>
Lines changed: 21 additions & 0 deletions
Loading
Lines changed: 16 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)