Skip to content

Commit db076eb

Browse files
feat: local JSON files (#82)
Co-authored-by: Evan You <yyx990803@gmail.com>
1 parent 3a87fa9 commit db076eb

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/editor/Editor.vue

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const onChange = debounce((code: string) => {
3131
3232
const activeMode = computed(() => {
3333
const { filename } = store.state.activeFile
34-
3534
const mode = modes[filename.split('.').pop()!]
3635
3736
return filename.lastIndexOf('.') !== -1 && mode

src/editor/FileSelector.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ function doneAddFile() {
4848
if (!pending.value) return
4949
const filename = pendingFilename.value
5050
51-
if (!/\.(vue|js|ts|css)$/.test(filename)) {
51+
if (!/\.(vue|js|ts|css|json)$/.test(filename)) {
5252
store.state.errors = [
53-
`Playground only supports *.vue, *.js, *.ts, *.css files.`
53+
`Playground only supports *.vue, *.js, *.ts, *.css, *.json files.`
5454
]
5555
return
5656
}

src/transform.ts

+14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ export async function compileFile(
4040
return
4141
}
4242

43+
if (filename.endsWith('.json')) {
44+
let parsed
45+
try {
46+
parsed = JSON.parse(code)
47+
} catch (err: any) {
48+
console.error(`Error parsing ${filename}`, err.message)
49+
store.state.errors = [err.message]
50+
return
51+
}
52+
compiled.js = compiled.ssr = `export default ${JSON.stringify(parsed)}`
53+
store.state.errors = []
54+
return
55+
}
56+
4357
if (!filename.endsWith('.vue')) {
4458
store.state.errors = []
4559
return

0 commit comments

Comments
 (0)