Skip to content

Commit f5291af

Browse files
authored
fix: use ts files when necessary with --bare option (#653)
1 parent 9c9a356 commit f5291af

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

index.ts

+22-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import generateReadme from './utils/generateReadme'
1818
import getCommand from './utils/getCommand'
1919
import getLanguage from './utils/getLanguage'
2020
import renderEslint from './utils/renderEslint'
21-
import trimBoilerplate from './utils/trimBoilerplate'
21+
import { trimBoilerplate, removeCSSImport, emptyRouterConfig } from './utils/trimBoilerplate'
2222

2323
import cliPackageJson from './package.json'
2424

@@ -560,6 +560,24 @@ async function init() {
560560
},
561561
)
562562

563+
if (argv.bare) {
564+
trimBoilerplate(root)
565+
render('bare/base')
566+
// TODO: refactor the `render` utility to avoid this kind of manual mapping?
567+
if (needsTypeScript) {
568+
render('bare/typescript')
569+
}
570+
if (needsVitest) {
571+
render('bare/vitest')
572+
}
573+
if (needsCypressCT) {
574+
render('bare/cypress-ct')
575+
}
576+
if (needsNightwatchCT) {
577+
render('bare/nightwatch-ct')
578+
}
579+
}
580+
563581
// Cleanup.
564582

565583
// We try to share as many files between TypeScript and JavaScript as possible.
@@ -610,21 +628,9 @@ async function init() {
610628
}
611629

612630
if (argv.bare) {
613-
trimBoilerplate(root, { needsTypeScript, needsRouter })
614-
render('bare/base')
615-
616-
// TODO: refactor the `render` utility to avoid this kind of manual mapping?
617-
if (needsTypeScript) {
618-
render('bare/typescript')
619-
}
620-
if (needsVitest) {
621-
render('bare/vitest')
622-
}
623-
if (needsCypressCT) {
624-
render('bare/cypress-ct')
625-
}
626-
if (needsNightwatchCT) {
627-
render('bare/nightwatch-ct')
631+
removeCSSImport(root, needsTypeScript)
632+
if (needsRouter) {
633+
emptyRouterConfig(root, needsTypeScript)
628634
}
629635
}
630636

template/base/vite.config.js.data.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function getData() {
1111
id: 'vite-plugin-vue-devtools',
1212
importer: "import vueDevTools from 'vite-plugin-vue-devtools'",
1313
initializer: 'vueDevTools()',
14-
}
14+
},
1515
],
1616
}
1717
}

utils/trimBoilerplate.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ function replaceContent(filepath: string, replacer: (content: string) => string)
66
fs.writeFileSync(filepath, replacer(content))
77
}
88

9-
export default function trimBoilerplate(rootDir: string, features: Record<string, boolean>) {
10-
const isTs = features.needsTypeScript
9+
export function trimBoilerplate(rootDir: string) {
1110
const srcDir = path.resolve(rootDir, 'src')
1211

1312
for (const filename of fs.readdirSync(srcDir)) {
@@ -19,18 +18,21 @@ export default function trimBoilerplate(rootDir: string, features: Record<string
1918
const fullpath = path.resolve(srcDir, filename)
2019
fs.rmSync(fullpath, { recursive: true })
2120
}
21+
}
2222

23+
export function removeCSSImport(rootDir: string, needsTypeScript: boolean) {
2324
// Remove CSS import in the entry file
24-
const entryPath = path.resolve(rootDir, isTs ? 'src/main.ts' : 'src/main.js')
25+
const entryPath = path.resolve(rootDir, needsTypeScript ? 'src/main.ts' : 'src/main.js')
2526
replaceContent(entryPath, (content) => content.replace("import './assets/main.css'\n\n", ''))
27+
}
2628

29+
export function emptyRouterConfig(rootDir: string, needsTypeScript: boolean) {
30+
const srcDir = path.resolve(rootDir, 'src')
2731
// If `router` feature is selected, use an empty router configuration
28-
if (features.needsRouter) {
29-
const routerEntry = path.resolve(srcDir, isTs ? 'router/index.ts' : 'router/index.js')
30-
replaceContent(routerEntry, (content) =>
31-
content
32-
.replace(`import HomeView from '../views/HomeView.vue'\n`, '')
33-
.replace(/routes:\s*\[[\s\S]*?\],/, 'routes: [],'),
34-
)
35-
}
32+
const routerEntry = path.resolve(srcDir, needsTypeScript ? 'router/index.ts' : 'router/index.js')
33+
replaceContent(routerEntry, (content) =>
34+
content
35+
.replace(`import HomeView from '../views/HomeView.vue'\n`, '')
36+
.replace(/routes:\s*\[[\s\S]*?\],/, 'routes: [],'),
37+
)
3638
}

0 commit comments

Comments
 (0)