Skip to content

Commit edff9d8

Browse files
JayFateyyx990803
andauthored
refactor: replace 'import-map.json' with importMapFile (#84)
Signed-off-by: JayFate <48240828+JayFate@users.noreply.github.com> Co-authored-by: Evan You <yyx990803@gmail.com>
1 parent b8caeae commit edff9d8

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/editor/FileSelector.vue

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<script setup lang="ts">
2-
import { Store } from '../store'
2+
import { Store, importMapFile } from '../store'
33
import { computed, inject, ref, VNode, Ref } from 'vue'
44
55
const store = inject('store') as Store
66
77
const pending = ref<boolean | string>(false)
88
const pendingFilename = ref('Comp.vue')
9-
const importMapFile = 'import-map.json'
109
const showImportMap = inject('import-map') as Ref<boolean>
1110
const files = computed(() =>
1211
Object.entries(store.state.files)

src/output/Preview.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import srcdoc from './srcdoc.html?raw'
1414
import { PreviewProxy } from './PreviewProxy'
1515
import { compileModulesForPreview } from './moduleCompiler'
16-
import { Store } from '../store'
16+
import { Store, importMapFile } from '../store'
1717
import { Props } from '../Repl.vue'
1818
1919
const props = defineProps<{ show: boolean; ssr: boolean }>()
@@ -36,7 +36,7 @@ onMounted(createSandbox)
3636
3737
// reset sandbox when import map changes
3838
watch(
39-
() => store.state.files['import-map.json'].code,
39+
() => store.state.files[importMapFile].code,
4040
raw => {
4141
try {
4242
const map = JSON.parse(raw)

src/store.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { OutputModes } from './output/types'
1111

1212
const defaultMainFile = 'App.vue'
1313

14+
export const importMapFile = 'import-map.json'
15+
1416
const welcomeCode = `
1517
<script setup>
1618
import { ref } from 'vue'
@@ -206,7 +208,7 @@ export class ReplStore implements Store {
206208

207209
serialize() {
208210
const files = this.getFiles()
209-
const importMap = files['import-map.json']
211+
const importMap = files[importMapFile]
210212
if (importMap) {
211213
const { imports } = JSON.parse(importMap)
212214
if (imports['vue'] === this.defaultVueRuntimeURL) {
@@ -216,9 +218,9 @@ export class ReplStore implements Store {
216218
delete imports['vue/server-renderer']
217219
}
218220
if (!Object.keys(imports).length) {
219-
delete files['import-map.json']
221+
delete files[importMapFile]
220222
} else {
221-
files['import-map.json'] = JSON.stringify({ imports }, null, 2)
223+
files[importMapFile] = JSON.stringify({ imports }, null, 2)
222224
}
223225
}
224226
return '#' + utoa(JSON.stringify(files))
@@ -255,10 +257,10 @@ export class ReplStore implements Store {
255257
}
256258

257259
private initImportMap() {
258-
const map = this.state.files['import-map.json']
260+
const map = this.state.files[importMapFile]
259261
if (!map) {
260-
this.state.files['import-map.json'] = new File(
261-
'import-map.json',
262+
this.state.files[importMapFile] = new File(
263+
importMapFile,
262264
JSON.stringify(
263265
{
264266
imports: {
@@ -292,7 +294,7 @@ export class ReplStore implements Store {
292294

293295
getImportMap() {
294296
try {
295-
return JSON.parse(this.state.files['import-map.json'].code)
297+
return JSON.parse(this.state.files[importMapFile].code)
296298
} catch (e) {
297299
this.state.errors = [
298300
`Syntax error in import-map.json: ${(e as Error).message}`
@@ -305,7 +307,7 @@ export class ReplStore implements Store {
305307
imports: Record<string, string>
306308
scopes?: Record<string, Record<string, string>>
307309
}) {
308-
this.state.files['import-map.json']!.code = JSON.stringify(map, null, 2)
310+
this.state.files[importMapFile]!.code = JSON.stringify(map, null, 2)
309311
}
310312

311313
async setVueVersion(version: string) {

0 commit comments

Comments
 (0)