Skip to content

Commit 3ea95e9

Browse files
authored
refactor: add internal flag for verifying vue sfcs (#21)
1 parent decd630 commit 3ea95e9

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/mkdist.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { SFCBlock, SFCTemplateBlock } from 'vue/compiler-sfc'
22
import type { InputFile, Loader, LoaderContext, LoaderResult, OutputFile } from './types/mkdist'
3+
import process from 'node:process'
34
import { transform } from 'esbuild'
45
import { preTranspileScriptSetup, transpileVueTemplate } from './index'
56

@@ -132,6 +133,23 @@ function defineVueLoader(options?: DefineVueLoaderOptions): Loader {
132133
})
133134
.filter(item => !!item)
134135
.join('\n')
136+
137+
// @ts-expect-error internal flag for testing
138+
if (context.options._verify || process.env.VERIFY_VUE_FILES) {
139+
// verify the output
140+
const { parse } = await import('vue/compiler-sfc')
141+
const { errors } = parse(contents, {
142+
filename: input.srcPath,
143+
ignoreEmpty: true,
144+
})
145+
if (errors.length > 0) {
146+
for (const error of errors) {
147+
console.error(error)
148+
}
149+
return
150+
}
151+
}
152+
135153
addOutput({
136154
path: input.path,
137155
srcPath: input.srcPath,

test/mkdist.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,12 @@ describe('transform typescript script setup', () => {
247247
await rm(tmpDir, { force: true, recursive: true })
248248
await mkdir(join(tmpDir, 'src'), { recursive: true })
249249
await writeFile(join(tmpDir, 'src/index.vue'), src)
250-
await mkdist({ loaders: ['js', vueLoader], rootDir: tmpDir })
250+
await mkdist({
251+
loaders: ['js', vueLoader],
252+
rootDir: tmpDir,
253+
// @ts-expect-error internal flag for testing
254+
_verify: true,
255+
})
251256
return await readFile(join(tmpDir, 'dist/index.vue'), 'utf-8')
252257
}
253258

0 commit comments

Comments
 (0)