Skip to content

Commit 613a9ba

Browse files
committed
Make Vitest inclusion optional
1 parent feb568c commit 613a9ba

File tree

9 files changed

+58
-16
lines changed

9 files changed

+58
-16
lines changed

packages/create-vue-lib/src/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type Config = {
7979
includeExamples: boolean
8080
includeEsLint: boolean
8181
includeEsLintStylistic: boolean
82+
includeVitest: boolean
8283
includeAtAliases: boolean
8384
includeTestVariable: boolean
8485
}
@@ -252,6 +253,7 @@ async function init() {
252253

253254
const includeEsLint = await togglePrompt('Include ESLint?', true)
254255
const includeEsLintStylistic = await togglePromptIf(includeEsLint, 'Include ESLint Stylistic for formatting?', includeEsLint)
256+
const includeVitest = await togglePromptIf(extended, 'Include Vitest for testing?', true)
255257
const includeDocs = await togglePrompt('Include VitePress for documentation?', true)
256258
const includeGithubPages = includeDocs && await togglePrompt('Include GitHub Pages config for documentation?')
257259
const includePlayground = await togglePrompt('Include playground application for development?', true)
@@ -309,6 +311,7 @@ async function init() {
309311
includeExamples,
310312
includeEsLint,
311313
includeEsLintStylistic,
314+
includeVitest,
312315
includeAtAliases,
313316
includeTestVariable
314317
}
@@ -331,6 +334,10 @@ async function init() {
331334
copyTemplate('eslint', config)
332335
}
333336

337+
if (config.includeVitest) {
338+
copyTemplate('vitest', config)
339+
}
340+
334341
console.log()
335342
console.log('Project created')
336343
console.log()
@@ -399,9 +406,15 @@ function copyFiles(templateFile: string, config: Config) {
399406
const target = targetPath.replace(/\.ejs$/, '')
400407
let content = ejs.render(template, { config })
401408

402-
if (target.endsWith('.json')) {
403-
// Remove trailing commas
404-
content = content.replace(/,(\s*)([}\]])/g, '$1$2')
409+
if (/\.(json|m?[jt]s)$/.test(target)) {
410+
// Trim spaces from the ends of lines
411+
content = content.replace(/ +\n/g, '\n')
412+
413+
// Remove consecutive blank lines
414+
content = content.replace(/\n\n+/g, '\n\n')
415+
416+
// Remove trailing commas and any blank newlines that follow them
417+
content = content.replace(/, *(?:\n+(\n\s*)|(\s*))([}\])])/g, '$1$2$3')
405418
}
406419

407420
fs.writeFileSync(target, content)

packages/create-vue-lib/src/template/base/config/.vscode/extensions.json.ejs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"recommendations": [
33
"Vue.volar",
4+
<%_ if (config.includeVitest) { _%>
45
"vitest.explorer",
6+
<%_ } _%>
57
<%_ if (config.includeEsLint) { _%>
68
"dbaeumer.vscode-eslint",
79
<%_ } _%>

packages/create-vue-lib/src/template/base/config/package.json.ejs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
"docs:dev": "pnpm run --filter ./packages/docs -r dev",
1515
"docs:build": "pnpm run --filter ./packages/docs -r build",
1616
<%_ } _%>
17+
<%_ if (config.includeVitest) { _%>
1718
"test:unit": "pnpm run --filter ./packages/<%- config.mainPackageDirName %> -r test:unit",
1819
"coverage": "pnpm run --filter ./packages/<%- config.mainPackageDirName %> -r coverage",
20+
<%_ } _%>
1921
<%_ if (config.includeEsLint) { _%>
2022
"type-check": "run-p type-check:*",
2123
"type-check:packages": "pnpm run -r type-check",
@@ -51,7 +53,9 @@
5153
"@tsconfig/node22": "^22.0.0",
5254
"@types/node": "^22.13.4",
5355
"@typescript-eslint/parser": "^8.23.0",
56+
<%_ if (config.includeVitest) { _%>
5457
"@vitest/eslint-plugin": "1.1.31",
58+
<%_ } _%>
5559
"@vue/eslint-config-typescript": "^14.4.0",
5660
"eslint": "^9.20.1",
5761
"eslint-plugin-vue": "^9.32.0",

packages/create-vue-lib/src/template/base/config/packages/@projectName@/package.json renamed to packages/create-vue-lib/src/template/base/config/packages/@projectName@/package.json.ejs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
{
22
"private": true,
3-
"name": "@scopedPackageName@",
3+
"name": "<%- config.scopedPackageName %>",
44
"version": "0.0.0",
55
"author": "",
66
"license": "",
77
"description": "",
88
"keywords": [],
9-
"homepage": "@homepageUrl@",
10-
"bugs": "@githubIssues@",
9+
"homepage": "<%- config.homepageUrl %>",
10+
"bugs": "<%- config.githubIssues %>",
1111
"repository": {
1212
"type": "git",
13-
"url": "@githubRepository@"
13+
"url": "<%- config.githubRepository %>"
1414
},
1515
"sideEffects": false,
16-
"main": "dist/@unscopedPackageName@.cjs",
17-
"module": "dist/@unscopedPackageName@.esm-bundler.prod.mjs",
18-
"unpkg": "dist/@unscopedPackageName@.global.dev.js",
19-
"jsdelivr": "dist/@unscopedPackageName@.global.dev.js",
20-
"types": "dist/@unscopedPackageName@.d.ts",
16+
"main": "dist/<%- config.unscopedPackageName %>.cjs",
17+
"module": "dist/<%- config.unscopedPackageName %>.esm-bundler.prod.mjs",
18+
"unpkg": "dist/<%- config.unscopedPackageName %>.global.dev.js",
19+
"jsdelivr": "dist/<%- config.unscopedPackageName %>.global.dev.js",
20+
"types": "dist/<%- config.unscopedPackageName %>.d.ts",
2121
"exports": {
2222
".": {
23-
"types": "./dist/@unscopedPackageName@.d.ts",
23+
"types": "./dist/<%- config.unscopedPackageName %>.d.ts",
2424
"import": {
25-
"development": "./dist/@unscopedPackageName@.esm.dev.js",
26-
"default": "./dist/@unscopedPackageName@.esm-bundler.prod.mjs"
25+
"development": "./dist/<%- config.unscopedPackageName %>.esm.dev.js",
26+
"default": "./dist/<%- config.unscopedPackageName %>.esm-bundler.prod.mjs"
2727
},
28-
"require": "./dist/@unscopedPackageName@.cjs"
28+
"require": "./dist/<%- config.unscopedPackageName %>.cjs"
2929
},
3030
"./dist/*": "./dist/*",
3131
"./package.json": "./package.json"
@@ -42,8 +42,10 @@
4242
"@types/jsdom": "^21.1.7",
4343
"@types/node": "^22.13.4",
4444
"@vitejs/plugin-vue": "^5.2.1",
45+
<%_ if (config.includeVitest) { _%>
4546
"@vitest/coverage-v8": "^3.0.5",
4647
"@vue/test-utils": "^2.4.6",
48+
<%_ } _%>
4749
"@vue/tsconfig": "^0.7.0",
4850
"copyfiles": "^2.4.1",
4951
"cross-env": "^7.0.3",
@@ -53,15 +55,19 @@
5355
"typescript": "~5.7.3",
5456
"vite": "^6.1.0",
5557
"vite-plugin-dts": "^4.5.3",
58+
<%_ if (config.includeVitest) { _%>
5659
"vitest": "^3.0.5",
60+
<%_ } _%>
5761
"vue": "^3.5.13",
5862
"vue-tsc": "^2.2.2"
5963
},
6064
"scripts": {
6165
"clean:dist": "rimraf dist",
6266
"clean": "rimraf dist coverage LICENSE README.md",
67+
<%_ if (config.includeVitest) { _%>
6368
"test:unit": "vitest --environment jsdom",
6469
"coverage": "vitest run --coverage --environment jsdom",
70+
<%_ } _%>
6571
"type-check": "vue-tsc --build",
6672
"build:copy": "copyfiles -f ../../LICENSE ../../README.md .",
6773
"build:dev": "cross-env NODE_ENV=development vite build --mode development",

packages/create-vue-lib/src/template/base/config/packages/@projectName@/tsconfig.json renamed to packages/create-vue-lib/src/template/base/config/packages/@projectName@/tsconfig.json.ejs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
{
88
"path": "./tsconfig.app.json"
99
},
10+
<%_ if (config.includeVitest) { _%>
1011
{
1112
"path": "./tsconfig.vitest.json"
1213
}
14+
<%_ } _%>
1315
]
1416
}

packages/create-vue-lib/src/template/eslint/config/eslint.config.mts.ejs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import pluginVue from 'eslint-plugin-vue'
77
import stylistic from '@stylistic/eslint-plugin'
88
<%_ } _%>
99
import { defineConfigWithVueTs, vueTsConfigs } from '@vue/eslint-config-typescript'
10+
<%_ if (config.includeVitest) { _%>
1011
import pluginVitest from '@vitest/eslint-plugin'
12+
<%_ } _%>
1113

1214
export default defineConfigWithVueTs(
1315
{
@@ -26,8 +28,10 @@ export default defineConfigWithVueTs(
2628
}),
2729
<%_ } _%>
2830

31+
<%_ if (config.includeVitest) { _%>
2932
{
3033
...pluginVitest.configs.recommended,
3134
files: ['src/**/__tests__/*']
3235
}
36+
<%_ } _%>
3337
)

packages/docs/src/questions.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<span class="check">✔</span> <a href="#github-path">GitHub path (optional) … skirtles-code/test-project</a>
4646
<span class="check">✔</span> <a href="#include-eslint">Include ESLint? … No / Yes</a>
4747
<span class="check">✔</span> <a href="#include-eslint-stylistic">Include ESLint Stylistic for formatting? … No / Yes</a>
48+
<span class="check">✔</span> <a href="#include-vitest">Include Vitest for testing? … No / Yes</a>
4849
<span class="check">✔</span> <a href="#include-vitepress">Include VitePress for documentation? … No / Yes</a>
4950
<span class="check">✔</span> <a href="#include-github-pages">Include GitHub Pages config for documentation? … No / Yes</a>
5051
<span class="check">✔</span> <a href="#include-playground">Include playground application for development? … No / Yes</a>
@@ -132,6 +133,16 @@ Include configuration for ESLint. This will be very similar to the default confi
132133

133134
Currently, this is the only formatter that is supported by this CLI tool, but you can configure others yourself after the project is created.
134135

136+
## Include Vitest for testing?{#include-vitest}
137+
138+
:::info NOTE
139+
This question is only asked when using the `--extended` flag.
140+
:::
141+
142+
[Vitest](https://vitest.dev/) is a testing framework for Vite.
143+
144+
Testing is particularly important for libraries, so Vitest is included by default, but you can opt out if you want to implement an alternative testing strategy.
145+
135146
## Include VitePress for documentation?{#include-vitepress}
136147

137148
[VitePress](https://vitepress.dev/) has become the standard tool for library documentation in the Vue community. If you intend to publish your package publicly then you probably want to use VitePress for your documentation.

0 commit comments

Comments
 (0)