Skip to content

Commit c61c56a

Browse files
committed
fix: do not generate empty file
The recent refactoring in vuejs#4330 introduced a regression where a `Home.vue` file was generated if using the TS plugin even without the router plugin. This happened because the result from the ejs rendering was `undefined`. This fixes the issue by not generating a file if the content is `undefined`.
1 parent a8df1df commit c61c56a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Diff for: packages/@vue/cli-plugin-typescript/__tests__/tsGenerator.spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ test('generate files', async () => {
1717
expect(files['src/main.ts']).toBeTruthy()
1818
expect(files['src/main.js']).toBeFalsy()
1919
expect(files['src/App.vue']).toMatch('<script lang="ts">')
20+
// checks that the Home.vue file has not be created, even empty
21+
expect(files.hasOwnProperty('src/views/Home.vue')).toBeFalsy()
2022
})
2123

2224
test('classComponent', async () => {
@@ -62,6 +64,22 @@ test('use with Babel', async () => {
6264
expect(files['tsconfig.json']).toMatch(`"target": "esnext"`)
6365
})
6466

67+
test('use with router', async () => {
68+
const { files } = await generateWithPlugin([
69+
{
70+
id: '@vue/cli-plugin-router',
71+
apply: require('@vue/cli-plugin-router/generator'),
72+
options: {}
73+
},
74+
{
75+
id: 'ts',
76+
apply: require('../generator'),
77+
options: {}
78+
}
79+
])
80+
expect(files['src/views/Home.vue']).toMatch('<div class=\"home\">')
81+
})
82+
6583
test('lint', async () => {
6684
const { pkg, files } = await generateWithPlugin([
6785
{

Diff for: packages/@vue/cli/lib/GeneratorAPI.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class GeneratorAPI {
244244
const sourcePath = path.resolve(source, rawPath)
245245
const content = renderFile(sourcePath, data, ejsOptions)
246246
// only set file if it's not all whitespace, or is a Buffer (binary files)
247-
if (Buffer.isBuffer(content) || /[^\s]/.test(content)) {
247+
if (Buffer.isBuffer(content) || content && /[^\s]/.test(content)) {
248248
files[targetPath] = content
249249
}
250250
}
@@ -255,7 +255,7 @@ class GeneratorAPI {
255255
for (const targetPath in source) {
256256
const sourcePath = path.resolve(baseDir, source[targetPath])
257257
const content = renderFile(sourcePath, data, ejsOptions)
258-
if (Buffer.isBuffer(content) || content.trim()) {
258+
if (Buffer.isBuffer(content) || content && content.trim()) {
259259
files[targetPath] = content
260260
}
261261
}

0 commit comments

Comments
 (0)