forked from vuejs/create-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use a
--bare
flag to generate a template without too much boi…
…lerplate
- Loading branch information
1 parent
fce4361
commit c4289cd
Showing
2 changed files
with
58 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as fs from 'node:fs' | ||
import * as path from 'path' | ||
|
||
function getBareBoneAppContent(isTs: boolean) { | ||
return `<script setup${isTs ? ' lang="ts"' : ''}> | ||
</script> | ||
<template> | ||
<h1>Hello World</h1> | ||
</template> | ||
<style scoped> | ||
</style> | ||
` | ||
} | ||
|
||
function replaceContent(filepath: string, replacer: (content: string) => string) { | ||
const content = fs.readFileSync(filepath, 'utf8') | ||
fs.writeFileSync(filepath, replacer(content)) | ||
} | ||
|
||
export default function trimBoilerplate(rootDir: string, features: Record<string, boolean>) { | ||
const isTs = features.needsTypeScript | ||
const srcDir = path.resolve(rootDir, 'src') | ||
|
||
for (const filename of fs.readdirSync(srcDir)) { | ||
// Keep `App.vue`, `main.js/ts`, `router`, and `stores` directories | ||
if (['App.vue', 'main.js', 'main.ts', 'router', 'stores'].includes(filename)) { | ||
console.log('continued') | ||
continue | ||
} | ||
const fullpath = path.resolve(srcDir, filename) | ||
fs.rmSync(fullpath, { recursive: true }) | ||
} | ||
|
||
// Replace the content in `src/App.vue` with a barebone template | ||
replaceContent(path.resolve(rootDir, 'src/App.vue'), () => getBareBoneAppContent(isTs)) | ||
|
||
// Remove CSS import in the entry file | ||
const entryPath = path.resolve(rootDir, isTs ? 'src/main.ts' : 'src/main.js') | ||
replaceContent(entryPath, (content) => content.replace("import './assets/main.css'\n\n", '')) | ||
|
||
// If `router` feature is selected, use an empty router configuration | ||
if (features.needsRouter) { | ||
const routerEntry = path.resolve(srcDir, isTs ? 'router/index.ts' : 'router/index.js') | ||
replaceContent(routerEntry, (content) => | ||
content | ||
.replace(`import HomeView from '../views/HomeView.vue'\n`, '') | ||
.replace(/routes:\s*\[[\s\S]*?\],/, 'routes: [],'), | ||
) | ||
} | ||
} |