Skip to content

Commit

Permalink
chore: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
shenjingnan committed Dec 28, 2023
1 parent 1f75086 commit 9dae425
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 76 deletions.
12 changes: 6 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createApp } from 'vue'
import '@unocss/reset/tailwind.css'
import '@vue/repl/style.css'
import './vars.scss'
import App from './App.vue'
import { createApp } from 'vue';
import '@unocss/reset/tailwind.css';
import '@vue/repl/style.css';
import './vars.scss';
import App from './App.vue';

createApp(App).mount('#app')
createApp(App).mount('#app');
78 changes: 39 additions & 39 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
// custom repl's store
import { File, ReplStore, type StoreOptions, type StoreState } from "@vue/repl";
import { utoa, genCdnLink } from "./utils";
import { File, ReplStore, type StoreOptions, type StoreState } from '@vue/repl';
import { utoa, genCdnLink } from './utils';

import welcomeCode2 from "./template/App.vue?raw";
import welcomeCode from "./template/App.vue?raw";
import mainCode from "./template/main.vue?raw";
import vantCode from "./template/vant.ts?raw";
import tsconfigCode from "./template/tsconfig.json?raw";
import { computed, reactive, shallowRef } from "vue";
import welcomeCode2 from './template/App.vue?raw';
import welcomeCode from './template/App.vue?raw';
import mainCode from './template/main.vue?raw';
import vantCode from './template/vant.ts?raw';
import tsconfigCode from './template/tsconfig.json?raw';
import { computed, reactive, shallowRef } from 'vue';

interface Dependency {
pkg?: string;
version?: string;
path: string;
}

const MAIN_FILE = "src/PlaygroundMain.vue";
const WELCOME_FILE = "src/App.vue";
const VANT_FILE = "src/vant.ts";
const TSCONFIG = "tsconfig.json";
const IMPORT_MAP_FILE = "import-map.json";
const MAIN_FILE = 'src/PlaygroundMain.vue';
const WELCOME_FILE = 'src/App.vue';
const VANT_FILE = 'src/vant.ts';
const TSCONFIG = 'tsconfig.json';
const IMPORT_MAP_FILE = 'import-map.json';

const genVantCode = () => {
return vantCode.replace("#STYLE#", genCdnLink("vant", "", "/lib/index.css"));
return vantCode.replace('#STYLE#', genCdnLink('vant', '', '/lib/index.css'));
};

const getImportMap = () => {
const deps: Record<string, Dependency> = {
vue: {
pkg: "@vue/runtime-dom",
version: "",
path: "/dist/runtime-dom.esm-browser.js",
pkg: '@vue/runtime-dom',
version: '',
path: '/dist/runtime-dom.esm-browser.js',
},
"vue/server-renderer": {
pkg: "@vue/server-renderer",
version: "",
path: "/dist/server-renderer.esm-browser.js",
'vue/server-renderer': {
pkg: '@vue/server-renderer',
version: '',
path: '/dist/server-renderer.esm-browser.js',
},
"@vue/shared": {
version: "",
path: "/dist/shared.esm-bundler.js",
'@vue/shared': {
version: '',
path: '/dist/shared.esm-bundler.js',
},
"vant/lib/index.css": {
version: "",
path: "",
'vant/lib/index.css': {
version: '',
path: '',
},
vant: {
version: "",
path: "/es/index.mjs",
version: '',
path: '/es/index.mjs',
},
"@vant/use": {
version: "",
path: "/dist/index.esm.mjs",
'@vant/use': {
version: '',
path: '/dist/index.esm.mjs',
},
"@vant/popperjs": {
version: "",
path: "/dist/index.esm.mjs",
'@vant/popperjs': {
version: '',
path: '/dist/index.esm.mjs',
},
};

Expand All @@ -64,7 +64,7 @@ const getImportMap = () => {
Object.entries(deps).map(([key, dep]) => [
key,
genCdnLink(dep.pkg ?? key, dep.version, dep.path),
])
]),
),
};
return res;
Expand All @@ -76,16 +76,16 @@ const _files = {
[TSCONFIG]: tsconfigCode,
};

const userFiles = location.hash.slice(1)
const userFiles = location.hash.slice(1);
// const userFiles = "";

class VantReplStore extends ReplStore {
constructor(storeOptions: StoreOptions = {}) {
super(storeOptions);
this.state.mainFile = MAIN_FILE
this.state.mainFile = MAIN_FILE;
this.addFile(new File(MAIN_FILE, mainCode, true));
this.addFile(new File(VANT_FILE, genVantCode(), true));
this.setActive(WELCOME_FILE)
this.setActive(WELCOME_FILE);
}
}
const store = new VantReplStore({
Expand Down
30 changes: 15 additions & 15 deletions src/template/vant.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { getCurrentInstance } from 'vue'
import Vant from 'vant'
import { getCurrentInstance } from 'vue';
import Vant from 'vant';

let installed = false
await loadStyle()
let installed = false;
await loadStyle();

export function setupVant() {
if (installed) return
const instance = getCurrentInstance()
instance.appContext.app.use(Vant)
installed = true
if (installed) return;
const instance = getCurrentInstance();
instance.appContext.app.use(Vant);
installed = true;
}

function loadStyle() {
return new Promise((resolve, reject) => {
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = '#STYLE#'
link.addEventListener('load', resolve)
link.addEventListener('error', reject)
document.body.append(link)
})
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '#STYLE#';
link.addEventListener('load', resolve);
link.addEventListener('error', reject);
document.body.append(link);
});
}
16 changes: 10 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { zlibSync, unzlibSync, strToU8, strFromU8 } from "fflate";
import { zlibSync, unzlibSync, strToU8, strFromU8 } from 'fflate';

export function debounce(fn: Function, n = 100) {
let handle: any;
Expand All @@ -10,10 +10,14 @@ export function debounce(fn: Function, n = 100) {
};
}

export const genCdnLink = (pkg: string, version: string | undefined, path: string) => {
version = version ? `@${version}` : "";
switch ("jsdelivr") {
case "jsdelivr":
export const genCdnLink = (
pkg: string,
version: string | undefined,
path: string,
) => {
version = version ? `@${version}` : '';
switch ('jsdelivr') {
case 'jsdelivr':
return `https://cdn.jsdelivr.net/npm/${pkg}${version}${path}`;
// case 'jsdelivr-fastly':
// return `https://fastly.jsdelivr.net/npm/${pkg}${version}${path}`
Expand All @@ -33,7 +37,7 @@ export function atou(base64: string): string {
const binary = atob(base64);

// zlib header (x78), level 9 (xDA)
if (binary.startsWith("\x78\xDA")) {
if (binary.startsWith('\x78\xDA')) {
const buffer = strToU8(binary, true);
const unzipped = unzlibSync(buffer);
return strFromU8(unzipped);
Expand Down
20 changes: 10 additions & 10 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import fs from "node:fs";
import path from "node:path";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import Inspect from 'vite-plugin-inspect'
const pathSrc = path.resolve(__dirname, "src");
import fs from 'node:fs';
import path from 'node:path';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import Inspect from 'vite-plugin-inspect';
const pathSrc = path.resolve(__dirname, 'src');

// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
"@": pathSrc,
'@': pathSrc,
},
},
build: {
rollupOptions: {
external: ["typescript"],
external: ['typescript'],
},
},
server: {
Expand All @@ -28,13 +28,13 @@ export default defineConfig({
propsDestructure: true,
fs: {
fileExists: fs.existsSync,
readFile: (file) => fs.readFileSync(file, "utf-8"),
readFile: (file) => fs.readFileSync(file, 'utf-8'),
},
},
}),
Inspect(),
],
optimizeDeps: {
exclude: ["@vue/repl"],
exclude: ['@vue/repl'],
},
});

0 comments on commit 9dae425

Please sign in to comment.