Skip to content

Commit 7785958

Browse files
committed
feat: create-app
1 parent 4f24f24 commit 7785958

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+729
-3
lines changed

docs/guide/index.md

-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ Supported template presets include:
3434
- `vue-ts`
3535
- `react`
3636
- `react-ts`
37-
- `preact`
38-
- `preact-ts`
39-
- `reason-react`
4037

4138
See [@vitejs/create-app](https://github.com/vitejs/vite/tree/main/packages/create-app) for more details on each template.
4239

packages/create-app/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/create-app/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# @vite/create-app
2+
3+
## Scaffolding Your First Vite Project
4+
5+
> **Comaptibility Note:**
6+
> Vite requires [Node.js](https://nodejs.org/en/) version >=12.0.0.
7+
8+
With NPM:
9+
10+
```bash
11+
$ npm init @vitejs/app
12+
```
13+
14+
With Yarn:
15+
16+
```bash
17+
$ yarn create @vitejs/app
18+
```
19+
20+
Then follow the prompts!
21+
22+
You can also directly specify the project name and the template you want to use via additional command line options. For example, to scaffold a Vite + Vue project, run:
23+
24+
```bash
25+
npm init @vitejs/app my-vue-app --template vue
26+
```
27+
28+
Currently supported template presets include:
29+
30+
- `vue`
31+
- `vue-ts`
32+
- `react`
33+
- `react-ts`

packages/create-app/index.js

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env node
2+
3+
// @ts-check
4+
const fs = require('fs')
5+
const path = require('path')
6+
const argv = require('minimist')(process.argv.slice(2))
7+
const { prompt } = require('enquirer')
8+
9+
const targetDir = argv._[0] || '.'
10+
const cwd = process.cwd()
11+
const root = path.join(cwd, targetDir)
12+
const renameFiles = {
13+
_gitignore: '.gitignore'
14+
}
15+
console.log(`Scaffolding project in ${root}...`)
16+
17+
async function init() {
18+
if (!fs.existsSync(root)) {
19+
fs.mkdirSync(root, { recursive: true })
20+
} else {
21+
const existing = fs.readdirSync(root)
22+
if (existing.length) {
23+
/**
24+
* @type {{ yes: boolean }}
25+
*/
26+
const { yes } = await prompt({
27+
type: 'confirm',
28+
name: 'yes',
29+
message:
30+
`Target directory ${targetDir} is not empty.\n` +
31+
`Remove existing files and continue?`
32+
})
33+
if (yes) {
34+
emptyDir(root)
35+
} else {
36+
return
37+
}
38+
}
39+
}
40+
41+
const templateDir = path.join(
42+
__dirname,
43+
`template-${argv.t || argv.template || 'vue'}`
44+
)
45+
46+
const write = (file, content) => {
47+
const targetPath = renameFiles[file]
48+
? path.join(root, renameFiles[file])
49+
: path.join(root, file)
50+
if (content) {
51+
fs.writeFileSync(targetPath, content)
52+
} else {
53+
copy(path.join(templateDir, file), targetPath)
54+
}
55+
}
56+
57+
const files = fs.readdirSync(templateDir)
58+
for (const file of files.filter((f) => f !== 'package.json')) {
59+
write(file)
60+
}
61+
62+
const pkg = require(path.join(templateDir, `package.json`))
63+
pkg.name = path.basename(root)
64+
write('package.json', JSON.stringify(pkg, null, 2))
65+
66+
console.log(`\nDone. Now run:\n`)
67+
if (root !== cwd) {
68+
console.log(` cd ${path.relative(cwd, root)}`)
69+
}
70+
console.log(` npm install (or \`yarn\`)`)
71+
console.log(` npm run dev (or \`yarn dev\`)`)
72+
console.log()
73+
}
74+
75+
function copy(src, dest) {
76+
const stat = fs.statSync(src)
77+
if (stat.isDirectory()) {
78+
copyDir(src, dest)
79+
} else {
80+
fs.copyFileSync(src, dest)
81+
}
82+
}
83+
84+
function copyDir(srcDir, destDir) {
85+
fs.mkdirSync(destDir, { recursive: true })
86+
for (const file of fs.readdirSync(srcDir)) {
87+
const srcFile = path.resolve(srcDir, file)
88+
const destFile = path.resolve(destDir, file)
89+
copy(srcFile, destFile)
90+
}
91+
}
92+
93+
function emptyDir(dir) {
94+
if (!fs.existsSync(dir)) {
95+
return
96+
}
97+
for (const file of fs.readdirSync(dir)) {
98+
const abs = path.resolve(dir, file)
99+
// baseline is Node 12 so can't use rmSync :(
100+
if (fs.lstatSync(abs).isDirectory()) {
101+
emptyDir(abs)
102+
fs.rmdirSync(abs)
103+
} else {
104+
fs.unlinkSync(abs)
105+
}
106+
}
107+
}
108+
109+
init().catch((e) => {
110+
console.error(e)
111+
})

packages/create-app/package.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@vitejs/create-app",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"author": "Evan You",
6+
"bin": {
7+
"create-vite-app": "index.js",
8+
"cva": "index.js"
9+
},
10+
"files": [
11+
"index.js"
12+
],
13+
"main": "index.js",
14+
"scripts": {
15+
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path . --lerna-package create-app",
16+
"release": "node ../../scripts/release.js --skipBuild"
17+
},
18+
"engines": {
19+
"node": ">=12.0.0"
20+
},
21+
"repository": {
22+
"type": "git",
23+
"url": "git+https://github.com/vitejs/vite.git"
24+
},
25+
"bugs": {
26+
"url": "https://github.com/vitejs/vite/issues"
27+
},
28+
"homepage": "https://github.com/vitejs/vite/tree/main/packages/create-app#readme",
29+
"dependencies": {
30+
"enquirer": "^2.3.6",
31+
"minimist": "^1.2.5"
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.DS_Store
3+
dist
4+
dist-ssr
5+
*.local
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Vite App</title>
7+
</head>
8+
<body>
9+
<div id="root"></div>
10+
<script type="module" src="/src/main.tsx"></script>
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "vite-react-typescript-starter",
3+
"version": "0.0.0",
4+
"scripts": {
5+
"dev": "vite",
6+
"build": "tsc && vite build"
7+
},
8+
"dependencies": {
9+
"react": "^17.0.0",
10+
"react-dom": "^17.0.0"
11+
},
12+
"devDependencies": {
13+
"@types/react": "^17.0.0",
14+
"@types/react-dom": "^17.0.0",
15+
"@vitejs/plugin-react-refresh": "^1.1.0",
16+
"typescript": "^4.1.2",
17+
"vite": "^2.0.0-beta.1"
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.App {
2+
text-align: center;
3+
}
4+
5+
.App-logo {
6+
height: 40vmin;
7+
pointer-events: none;
8+
}
9+
10+
@media (prefers-reduced-motion: no-preference) {
11+
.App-logo {
12+
animation: App-logo-spin infinite 20s linear;
13+
}
14+
}
15+
16+
.App-header {
17+
background-color: #282c34;
18+
min-height: 100vh;
19+
display: flex;
20+
flex-direction: column;
21+
align-items: center;
22+
justify-content: center;
23+
font-size: calc(10px + 2vmin);
24+
color: white;
25+
}
26+
27+
.App-link {
28+
color: #61dafb;
29+
}
30+
31+
@keyframes App-logo-spin {
32+
from {
33+
transform: rotate(0deg);
34+
}
35+
to {
36+
transform: rotate(360deg);
37+
}
38+
}
39+
40+
button {
41+
font-size: calc(10px + 2vmin);
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { useState } from 'react'
2+
import logo from './logo.svg'
3+
import './App.css'
4+
5+
function App() {
6+
const [count, setCount] = useState(0)
7+
8+
return (
9+
<div className="App">
10+
<header className="App-header">
11+
<img src={logo} className="App-logo" alt="logo" />
12+
<p>Hello Vite + React!</p>
13+
<p>
14+
<button onClick={() => setCount(count => count + 1)}>count is: {count}</button>
15+
</p>
16+
<p>
17+
Edit <code>App.tsx</code> and save to test HMR updates.
18+
</p>
19+
<a
20+
className="App-link"
21+
href="https://reactjs.org"
22+
target="_blank"
23+
rel="noopener noreferrer"
24+
>
25+
Learn React
26+
</a>
27+
</header>
28+
</div>
29+
)
30+
}
31+
32+
export default App
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
body {
2+
margin: 0;
3+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
4+
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
5+
sans-serif;
6+
-webkit-font-smoothing: antialiased;
7+
-moz-osx-font-smoothing: grayscale;
8+
}
9+
10+
code {
11+
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
12+
monospace;
13+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
import ReactDOM from 'react-dom'
3+
import './index.css'
4+
import App from './App'
5+
6+
ReactDOM.render(
7+
<React.StrictMode>
8+
<App />
9+
</React.StrictMode>,
10+
document.getElementById('root')
11+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"lib": ["DOM", "DOM.Iterable", "ESNext"],
5+
"types": [],
6+
"allowJs": false,
7+
"skipLibCheck": false,
8+
"esModuleInterop": false,
9+
"allowSyntheticDefaultImports": true,
10+
"strict": true,
11+
"forceConsistentCasingInFileNames": true,
12+
"module": "ESNext",
13+
"moduleResolution": "Node",
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"noEmit": true,
17+
"jsx": "react"
18+
},
19+
"include": ["src"]
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import reactRefresh from '@vitejs/plugin-react-refresh'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
plugins: [reactRefresh()]
6+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.DS_Store
3+
dist
4+
dist-ssr
5+
*.local

0 commit comments

Comments
 (0)