Skip to content

Commit

Permalink
feat: local JSON files (vuejs#82)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan You <yyx990803@gmail.com>
  • Loading branch information
skirtles-code and yyx990803 committed Jun 14, 2023
1 parent 3a87fa9 commit db076eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/editor/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const onChange = debounce((code: string) => {
const activeMode = computed(() => {
const { filename } = store.state.activeFile
const mode = modes[filename.split('.').pop()!]
return filename.lastIndexOf('.') !== -1 && mode
Expand Down
4 changes: 2 additions & 2 deletions src/editor/FileSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ function doneAddFile() {
if (!pending.value) return
const filename = pendingFilename.value
if (!/\.(vue|js|ts|css)$/.test(filename)) {
if (!/\.(vue|js|ts|css|json)$/.test(filename)) {
store.state.errors = [
`Playground only supports *.vue, *.js, *.ts, *.css files.`
`Playground only supports *.vue, *.js, *.ts, *.css, *.json files.`
]
return
}
Expand Down
14 changes: 14 additions & 0 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ export async function compileFile(
return
}

if (filename.endsWith('.json')) {
let parsed
try {
parsed = JSON.parse(code)
} catch (err: any) {
console.error(`Error parsing ${filename}`, err.message)
store.state.errors = [err.message]
return
}
compiled.js = compiled.ssr = `export default ${JSON.stringify(parsed)}`
store.state.errors = []
return
}

if (!filename.endsWith('.vue')) {
store.state.errors = []
return
Expand Down

0 comments on commit db076eb

Please sign in to comment.