Skip to content

Commit

Permalink
fixed empty/no script tag in vue component error
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdilgergeo committed Dec 13, 2022
1 parent 7747321 commit f80d97f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
33 changes: 33 additions & 0 deletions examples/example.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<template>
<div>
<span>hello world
</span></div>
</template>

<script>
// I am top level comment in this file.
// I am second line of top level comment in this file.
import threeLevelRelativePath from '../../../threeLevelRelativePath';
import sameLevelRelativePath from './sameLevelRelativePath';
import thirdParty from 'third-party';
import React from 'react';
export { random } from './random';
import oneLevelRelativePath from '../oneLevelRelativePath';
import otherthing from '@core/otherthing';
import twoLevelRelativePath from '../../twoLevelRelativePath';
import component from '@ui/hello';
export default {
title: 'hello',
};
import fourLevelRelativePath from '../../../../fourLevelRelativePath';
import something from '@server/something';
function add(a, b) {
return a + b;
}
</script>

<style>
div { color: red; }
</style>
9 changes: 6 additions & 3 deletions src/preprocessors/vue-preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor';

export function vuePreprocessor(code: string, options: PrettierOptions) {
const { parse } = require('@vue/compiler-sfc')
const { parse } = require('@vue/compiler-sfc');
const { descriptor } = parse(code);
const content =
(descriptor.script ?? descriptor.scriptSetup)?.content ?? code;

const content = (descriptor.script ?? descriptor.scriptSetup)?.content;
if (!content) {
return code;
}

return code.replace(content, `\n${preprocessor(content, options)}\n`);
}

0 comments on commit f80d97f

Please sign in to comment.