Skip to content

fix: use ts files when necessary with --bare option #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import generateReadme from './utils/generateReadme'
import getCommand from './utils/getCommand'
import getLanguage from './utils/getLanguage'
import renderEslint from './utils/renderEslint'
import trimBoilerplate from './utils/trimBoilerplate'
import { trimBoilerplate, removeCSSImport, emptyRouterConfig } from './utils/trimBoilerplate'

import cliPackageJson from './package.json'

Expand Down Expand Up @@ -560,6 +560,24 @@ async function init() {
},
)

if (argv.bare) {
trimBoilerplate(root)
render('bare/base')
// TODO: refactor the `render` utility to avoid this kind of manual mapping?
if (needsTypeScript) {
render('bare/typescript')
}
if (needsVitest) {
render('bare/vitest')
}
if (needsCypressCT) {
render('bare/cypress-ct')
}
if (needsNightwatchCT) {
render('bare/nightwatch-ct')
}
}

// Cleanup.

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

if (argv.bare) {
trimBoilerplate(root, { needsTypeScript, needsRouter })
render('bare/base')

// TODO: refactor the `render` utility to avoid this kind of manual mapping?
if (needsTypeScript) {
render('bare/typescript')
}
if (needsVitest) {
render('bare/vitest')
}
if (needsCypressCT) {
render('bare/cypress-ct')
}
if (needsNightwatchCT) {
render('bare/nightwatch-ct')
removeCSSImport(root, needsTypeScript)
if (needsRouter) {
emptyRouterConfig(root, needsTypeScript)
}
}

Expand Down
2 changes: 1 addition & 1 deletion template/base/vite.config.js.data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function getData() {
id: 'vite-plugin-vue-devtools',
importer: "import vueDevTools from 'vite-plugin-vue-devtools'",
initializer: 'vueDevTools()',
}
},
],
}
}
24 changes: 13 additions & 11 deletions utils/trimBoilerplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ function replaceContent(filepath: string, replacer: (content: string) => string)
fs.writeFileSync(filepath, replacer(content))
}

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

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

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

export function emptyRouterConfig(rootDir: string, needsTypeScript: boolean) {
const srcDir = path.resolve(rootDir, 'src')
// 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: [],'),
)
}
const routerEntry = path.resolve(srcDir, needsTypeScript ? 'router/index.ts' : 'router/index.js')
replaceContent(routerEntry, (content) =>
content
.replace(`import HomeView from '../views/HomeView.vue'\n`, '')
.replace(/routes:\s*\[[\s\S]*?\],/, 'routes: [],'),
)
}
Loading