Skip to content

Commit

Permalink
feat: switch to Typescript and provide type declaration file
Browse files Browse the repository at this point in the history
Closes #36
  • Loading branch information
freddy38510 committed Sep 23, 2022
1 parent efc1086 commit b2ba9cc
Show file tree
Hide file tree
Showing 94 changed files with 2,215 additions and 857 deletions.
34 changes: 19 additions & 15 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
module.exports = {
require('@rushstack/eslint-patch/modern-module-resolution');
const { defineConfig } = require('eslint-define-config');

module.exports = defineConfig({
root: true,
extends: [
'airbnb-base',
'plugin:vue/vue3-recommended',
'plugin:prettier/recommended',
'eslint-config-vitest-globals',
],
env: {
browser: true,
node: true,
'vue/setup-compiler-macros': true,
},
globals: {
__DEV__: 'readonly',
'vitest-globals/env': true,
},
ignorePatterns: ['node_modules', 'dist'],
ignorePatterns: ['node_modules', 'dist', 'temp'],
extends: [
'plugin:vue/vue3-recommended',
'@vue/eslint-config-airbnb-with-typescript',
'plugin:vitest-globals/recommended',
'prettier',
],
rules: {
'prettier/prettier': 'error',
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
'no-param-reassign': ['error', { props: false }],
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: true,
},
],
},
};
});
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ coverage
yarn.lock
package-lock.json
.pnpm-store/
temp

/cypress/videos/
/cypress/screenshots/
Expand Down
5 changes: 3 additions & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"recommendations": [
"johnsoncodehk.volar",
"vue.volar",
"Vue.vscode-typescript-vue-plugin",
"esbenp.prettier-vscode",
"ZixuanChen.vitest-explorer"
"zixuanchen.vitest-explorer"
]
}
38 changes: 38 additions & 0 deletions api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "<projectFolder>/temp/index.d.ts",
"bundledPackages": [],
"compiler": {
"tsconfigFilePath": "<projectFolder>/tsconfig.types.json"
},
"apiReport": {
"enabled": false
},
"docModel": {
"enabled": false
},
"dtsRollup": {
"enabled": true,
"publicTrimmedFilePath": "<projectFolder>/dist/<unscopedPackageName>.d.ts"
},
"tsdocMetadata": {
"enabled": false
},
"messages": {
"compilerMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"extractorMessageReporting": {
"default": {
"logLevel": "warning"
}
},
"tsdocMessageReporting": {
"default": {
"logLevel": "warning"
}
}
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion demo/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</main>
</template>

<script>
<script lang="ts">
import { defineComponent } from 'vue';
import NavLinks from './components/NavLinks.vue';
Expand Down
23 changes: 11 additions & 12 deletions demo/collect-css-ssr.js → demo/collect-css-ssr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-bitwise */

const hashCode = (moduleId) => {
import type { ModuleNode } from 'vite';

const hashCode = (moduleId: string) => {
let hash = 0;
let i;
let chr;
Expand All @@ -14,31 +16,28 @@ const hashCode = (moduleId) => {
return hash;
};

const moduleIsStyle = (mod) => {
return (
(mod.file?.endsWith('.scss') ||
mod.file?.endsWith('.css') ||
mod.id?.includes('vue&type=style')) &&
mod.ssrModule
);
};
const moduleIsStyle = (mod: ModuleNode) =>
(mod?.file?.endsWith('.scss') ||
mod?.file?.endsWith('.css') ||
mod?.id?.includes('vue&type=style')) &&
mod?.ssrModule;

/**
* Collect SSR CSS for Vite
*/
export const collectCss = (
mods,
mods: ModuleNode[] | Set<ModuleNode>,
styles = new Map(),
checkedMods = new Set()
) => {
let result = '';

mods.forEach((mod) => {
if (moduleIsStyle(mod)) {
styles.set(mod.url, mod.ssrModule.default);
styles.set(mod.url, mod.ssrModule?.default);
}

if (mod.importedModules?.size > 0 && !checkedMods.has(mod.id)) {
if (mod?.importedModules?.size > 0 && !checkedMods.has(mod.id)) {
checkedMods.add(mod.id);

collectCss(mod.importedModules, styles, checkedMods);
Expand Down
4 changes: 2 additions & 2 deletions demo/components/CounterComp.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script setup>
<script setup lang="ts">
import { ref } from 'vue';
const count = ref(0);
Expand All @@ -10,7 +10,7 @@ function onClick() {
<template>
<div>
<p>
<button @click="onClick">Increment Counter</button>
<button type="button" @click="onClick">Increment Counter</button>
</p>
<p>
Counter: <code>{{ count }}</code>
Expand Down
14 changes: 7 additions & 7 deletions demo/components/HydrationState.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script setup>
<script setup lang="ts">
import { getCurrentInstance, onMounted, ref, toRef } from 'vue';
const props = defineProps({
Expand All @@ -7,13 +7,13 @@ const props = defineProps({
},
});
const hasInitialVnode = getCurrentInstance().vnode.el !== null;
const hasInitialVnode = getCurrentInstance()?.vnode.el !== null;
const showReload = ref(undefined);
const willPerformHydration = ref(undefined);
const showReload = ref();
const willPerformHydration = ref();
const isHydrated = toRef(props, 'isHydrated');
function stateValueClass(value) {
function stateValueClass(value: boolean) {
return value === true ? 'text-green' : 'text-red';
}
Expand All @@ -32,8 +32,8 @@ onMounted(() => {
<template>
<div class="box">
<p v-if="showReload" class="text-red">
Please <button @click="reload">Reload</button> to see lazy hydration
effect.
Please <button type="button" @click="reload">Reload</button> to see lazy
hydration effect.
</p>
<ul>
<li>
Expand Down
6 changes: 4 additions & 2 deletions demo/components/InputComp.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script setup>
<script setup lang="ts">
defineProps({
modelValue: {
type: String,
Expand All @@ -12,7 +12,9 @@ defineEmits(['update:modelValue']);
<p>
<input
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
@input="
$emit('update:modelValue', ($event.target as HTMLInputElement).value)
"
/>
</p>
<p>
Expand Down
6 changes: 4 additions & 2 deletions demo/components/InputHydratedOnInteraction.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script setup>
<script setup lang="ts">
import { useLazyHydration, useHydrateOnInteraction } from '../../src';
defineProps({
Expand All @@ -21,7 +21,9 @@ result.onHydrated(() => emit('hydrated'));
<p>
<input
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
@input="
$emit('update:modelValue', ($event.target as HTMLInputElement).value)
"
/>
</p>
<p>
Expand Down
4 changes: 2 additions & 2 deletions demo/components/LazilyHydratedCounter.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script setup>
<script setup lang="ts">
import { ref, toRef } from 'vue';
import {
useLazyHydration,
Expand Down Expand Up @@ -50,7 +50,7 @@ result.onHydrated(() => emit('hydrated'));
<template>
<div>
<p>
<button @click="onClick">Increment Counter</button>
<button type="button" @click="onClick">Increment Counter</button>
</p>
<p>
Counter: <code>{{ count }}</code>
Expand Down
2 changes: 1 addition & 1 deletion demo/components/NavLinks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<hr />
</template>

<script setup>
<script setup lang="ts">
const links = [
{
label: 'Composables',
Expand Down
3 changes: 2 additions & 1 deletion demo/entry-client.js → demo/entry-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ removeCssHotReloaded();
const { app, router } = createApp();

// wait until router is ready before mounting to ensure hydration match
router.isReady().then(() => {
// eslint-disable-next-line no-void
void router.isReady().then(() => {
app.mount('#app');
});
29 changes: 21 additions & 8 deletions demo/entry-server.js → demo/entry-server.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
import path from 'node:path';
import { renderToString } from 'vue/server-renderer';
import type { ModuleNode, ViteDevServer } from 'vite';
import { renderToString, type SSRContext } from 'vue/server-renderer';
import { collectCss } from './collect-css-ssr';
import createApp from './main';

export default async function render(url, { moduleGraph }) {
export default async function render(
url: string,
{ moduleGraph }: ViteDevServer
) {
const { app, router } = createApp();

// set the router to the desired URL before rendering
router.push(url);
// eslint-disable-next-line no-void
void router.push(url);
await router.isReady();

// passing SSR context object which will be available via useSSRContext()
// @vitejs/plugin-vue injects code into a component's setup() that registers
// itself on ctx.modules. After the render, ctx.modules would contain all the
// components that have been instantiated during this render call.
const ctx = {};
const ctx = {} as SSRContext;
const appHtml = await renderToString(app, ctx);

const mods = new Set();
const mods: Set<ModuleNode> = new Set();

// add main module for direct imported styles
mods.add(moduleGraph.getModuleById(path.resolve('./demo/main.js')));
let mod = moduleGraph.getModuleById(path.resolve('./demo/main.ts'));

if (mod) {
mods.add(mod);
}

// add modules from rendered Vue components
ctx.modules.forEach((componentPath) => {
mods.add(moduleGraph.getModuleById(path.resolve(componentPath)));
(ctx.modules as Set<string>).forEach((componentPath) => {
mod = moduleGraph.getModuleById(path.resolve(componentPath));

if (mod) {
mods.add(mod);
}
});

return { appHtml, css: collectCss(mods) };
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit b2ba9cc

Please sign in to comment.