Skip to content

Commit

Permalink
feat(ssr): experimental ssr, simple static demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
07akioni committed Jun 2, 2021
1 parent 787f624 commit bd5fc97
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 53 deletions.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"scripts": {
"start": "npm run dev",
"dev": "npm run clean && npm run gen-version && cross-env NODE_ENV=development vite",
"build:js": "npm run gen-version && npm run clean && tsc -b --force tsconfig.esm.json && tsc -b --force tsconfig.cjs.json && node scripts/post-build",
"build:package": "npm run gen-version && npm run clean && tsc -b --force tsconfig.esm.json && tsc -b --force tsconfig.cjs.json && node scripts/post-build",
"build:package": "npm run gen-version && npm run clean && tsc -b --force tsconfig.esm.json && node scripts/pre-build/pre-cjs-build.js && tsc -b --force tsconfig.cjs.json && node scripts/post-build",
"build:site": "./scripts/pre-build-site/pre-build-site.sh && cross-env NODE_ENV=production NODE_OPTIONS=--max-old-space-size=4096 vite build && ./scripts/post-build-site/post-build-site.sh",
"clean": "rm -rf site lib es node_modules/naive-ui themes/**/es themes/**/lib",
"release:package": "npm run build:package && npm publish --tag next",
Expand Down Expand Up @@ -93,30 +92,31 @@
"eslint-plugin-promise": "^4.3.1",
"eslint-plugin-standard": "^4.1.0",
"eslint-plugin-vue": "^7.6.0",
"express": "^4.17.1",
"fs-extra": "^9.0.1",
"husky": "^4.3.5",
"jest": "^26.6.2",
"lint-staged": "^10.5.3",
"lodash": "^4.17.20",
"marked": "^2.0.1",
"prettier": "^2.2.1",
"typescript": "^4.2.2",
"typescript": "^4.3.2",
"vite": "^2.1.3"
},
"dependencies": {
"@css-render/plugin-bem": "^0.15.1",
"@css-render/vue3-ssr": "^0.15.1",
"@css-render/plugin-bem": "^0.15.2",
"@css-render/vue3-ssr": "^0.15.2",
"async-validator": "^3.5.1",
"css-render": "^0.15.1",
"css-render": "^0.15.2",
"date-fns": "^2.19.0",
"evtd": "^0.2.0",
"evtd": "^0.2.2",
"highlight.js": "^10.7.1",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"seemly": "^0.3.1",
"treemate": "^0.2.10",
"vdirs": "^0.1.2",
"vdirs": "^0.1.4",
"vfonts": "^0.1.0",
"vooks": "^0.2.3",
"vooks": "^0.2.4",
"vue": "^3.0.10",
"vue-router": "^4.0.5",
"vueuc": "^0.4.5"
Expand Down
28 changes: 28 additions & 0 deletions playground/ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const express = require('express')
const { h, createSSRApp } = require('vue')
const { renderToString } = require('@vue/server-renderer')
const { setup } = require('@css-render/vue3-ssr')
const { NButton } = require('../lib')
const { NSsrProvider } = require('../lib/ssr')

const app = express()
const vueAppOptions = {
render () {
return h(NSsrProvider, null, {
default: () => h(NButton, null, { default: () => 'btn' })
})
}
}

app.use((req, res) => {
const ssrApp = createSSRApp(vueAppOptions)
const { collect } = setup(ssrApp)
res.write('<!doctype html><html><head><title>SSR Test</title></head><body>')
renderToString(ssrApp).then((html) => {
res.write(collect() + html)
res.write('</body></html>')
res.end()
})
})

app.listen(8086)
9 changes: 7 additions & 2 deletions scripts/post-build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
const { terseCssr } = require('./terse-cssr')

// replace __DEV__
const { replaceDefine } = require('./replace-define')
const { replaceDefine, outDirs, srcDir } = require('../utils')

;(async () => {
await terseCssr()
await replaceDefine()
await replaceDefine(outDirs, {
__DEV__: "process.env.NODE_ENV !== 'production'"
})
await replaceDefine([srcDir], {
"'lodash'": "'lodash-es'"
})
})()
8 changes: 8 additions & 0 deletions scripts/pre-build/pre-cjs-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// replace lodash-es
const { replaceDefine, srcDir } = require('../utils')

;(async () => {
await replaceDefine([srcDir], {
"'lodash\\-es'": "'lodash'"
})
})()
3 changes: 3 additions & 0 deletions scripts/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ exports.walk = async function * walk (dir) {
}

exports.outDirs = ['es', 'lib'].map((d) => path.resolve(__dirname, '../..', d))
exports.srcDir = path.resolve(__dirname, '../../src')

exports.replaceDefine = require('./replace-define').replaceDefine
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
const fs = require('fs').promises
const { walk, outDirs } = require('../utils')
const { walk } = require('.')

const define = {
__DEV__: "process.env.NODE_ENV !== 'production'"
}

exports.replaceDefine = async () => {
const defineKeys = Object.keys(define)
exports.replaceDefine = async (dirs, defines) => {
const defineKeys = Object.keys(defines)
const patterns = {}
defineKeys.forEach((key) => {
patterns[key] = new RegExp(key, 'g')
})
for (const dir of outDirs) {
for (const dir of dirs) {
for await (const p of walk(dir)) {
const code = await fs.readFile(p, 'utf-8')
for (const key of defineKeys) {
const pattern = patterns[key]
if (pattern.test(code)) {
const outCode = code.replace(pattern, define[key])
const outCode = code.replace(pattern, defines[key])
await fs.writeFile(p, outCode)
}
}
}
}
}

if (require.main === module) {
exports.replaceDefine()
}
31 changes: 22 additions & 9 deletions src/_mixins/use-style.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import { CNode } from 'css-render'
import { Ref, onBeforeMount } from 'vue'
import { Ref, onBeforeMount, inject } from 'vue'
import globalStyle from '../_styles/global/index.cssr'

globalStyle.mount({
id: 'naive-ui-global',
head: true
})
import { ssrInjectionKey } from '../ssr/context'
import { throwError } from '../_utils'

export default function useStyle (
mountId: string,
style: CNode,
clsPrefixRef?: Ref<string | undefined>
): void {
onBeforeMount(() => {
if (!style) {
if (__DEV__) throwError('use-style', 'No style is specified.')
return
}
const ssrAdapter = inject(ssrInjectionKey, undefined)
const mountStyle = (): void => {
const clsPrefix = clsPrefixRef?.value
style.mount({
id: clsPrefix === undefined ? mountId : clsPrefix + mountId,
head: true,
props: {
bPrefix: clsPrefix ? `.${clsPrefix}-` : undefined
}
},
ssr: ssrAdapter
})
globalStyle.mount({
id: 'naive-ui/global',
head: true,
ssr: ssrAdapter
})
})
}
if (ssrAdapter) {
mountStyle()
} else {
onBeforeMount(mountStyle)
}
}
10 changes: 5 additions & 5 deletions src/_mixins/use-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import { configProviderInjectionKey } from '../config-provider/src/ConfigProvide
import type { ThemeCommonVars } from '../_styles/common'
import { ssrInjectionKey } from '../ssr/context'

globalStyle.mount({
id: 'naive-ui-global',
head: true
})

export interface Theme<N, T = {}, R = any> {
name: N
common?: ThemeCommonVars
Expand Down Expand Up @@ -108,6 +103,11 @@ function useTheme<N, T, R> (
},
ssr: ssrAdapter
})
globalStyle.mount({
id: 'naive-ui/global',
head: true,
ssr: ssrAdapter
})
}
if (ssrAdapter) {
mountStyle()
Expand Down
8 changes: 5 additions & 3 deletions src/button/src/styles/button.cssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ export default c([
animationName: 'button-wave-spread, button-wave-opacity'
})
]),
'MozBoxSizing' in document.body.style ? c('&::moz-focus-inner', {
border: 0
}) : null,
(typeof window !== 'undefined' && 'MozBoxSizing' in document.body.style)
? c('&::moz-focus-inner', {
border: 0
})
: null,
cE('border, state-border', `
position: absolute;
left: 0;
Expand Down
13 changes: 5 additions & 8 deletions src/tree/src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,8 @@ export default defineComponent({
return
}
const { virtualScroll } = props
const viewportHeight = (virtualScroll
? virtualListInstRef.value!.listElRef
: selfElRef.value!
const viewportHeight = (
virtualScroll ? virtualListInstRef.value!.listElRef : selfElRef.value!
).offsetHeight
const viewportItemCount = Math.ceil(viewportHeight / ITEM_SIZE) + 1
if (addedKey !== null) {
Expand Down Expand Up @@ -596,7 +595,7 @@ export default defineComponent({
function handleDragStart ({ event, node }: InternalDragInfo): void {
if (!props.draggable || props.disabled || node.disabled) return
// Most of time, the image will block user's view
event.dataTransfer?.setDragImage(emptyImage, 0, 0)
emptyImage && event.dataTransfer?.setDragImage(emptyImage, 0, 0)
dragStartX = event.clientX
draggingNodeRef.value = node
doDragStart({ event, node: node.rawNode })
Expand All @@ -612,10 +611,8 @@ export default defineComponent({
if (emit) doDragOver({ event, node: node.rawNode })
// Update dropping node
const el = event.currentTarget as HTMLElement
const {
height: elOffsetHeight,
top: elClientTop
} = el.getBoundingClientRect()
const { height: elOffsetHeight, top: elClientTop } =
el.getBoundingClientRect()
const eventOffsetY = event.clientY - elClientTop
let mousePosition: DropPosition

Expand Down
10 changes: 7 additions & 3 deletions src/tree/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ export function keysWithFilter (
return [Array.from(keys), Array.from(highlightKeys)]
}

const emptyImage = new Image()
emptyImage.src =
'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
const emptyImage: HTMLImageElement | null = null
if (typeof window !== 'undefined') {
const emptyImage = new Image()
emptyImage.src =
'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
}

export { emptyImage }

0 comments on commit bd5fc97

Please sign in to comment.