build: extract loaders to build/, compress cssr.js files

This commit is contained in:
07akioni 2020-11-12 13:30:04 +08:00
parent 672eee1be8
commit c7e283560c
27 changed files with 111 additions and 87 deletions

View File

@ -3,3 +3,4 @@ lib
test/unit/coverage
src/_deprecated/icons
dist
es

View File

@ -1,7 +1,7 @@
const marked = require('marked')
const fs = require('fs')
const path = require('path')
const createRenderer = require('./mdRenderer')
const createRenderer = require('./md-renderer')
const mdRenderer = createRenderer()
const codeRenderer = createRenderer(false)
// const prettier = require('prettier')

View File

@ -1,7 +1,7 @@
const marked = require('marked')
const camelCase = require('lodash/camelCase')
const mdLoader = require('./NaiveUIMdLoader')
const createRenderer = require('./mdRenderer')
const mdLoader = require('./naive-ui-md-loader')
const createRenderer = require('./md-renderer')
const mdRenderer = createRenderer()
function template (demos, demosLiteral, isSingleColumn = false) {

View File

@ -1,4 +1,4 @@
const convertMd2Demo = require('./convertMd2Demo')
const convertMd2Demo = require('./convert-md-to-demo')
const projectPath = require('./project-path')
module.exports = function (content, path) {

View File

@ -1,4 +1,4 @@
const convertMd2Doc = require('./convertMd2Doc')
const convertMd2Doc = require('./convert-md-to-doc')
const projectPath = require('./project-path')
module.exports = function (content, path) {

View File

@ -1,5 +1,5 @@
const marked = require('marked')
const createRenderer = require('./mdRenderer')
const createRenderer = require('./md-renderer')
const renderer = createRenderer()
const projectPath = require('./project-path')

View File

@ -0,0 +1,9 @@
const cleanCssr = require('./utils/clean-cssr')
module.exports = () => ({
async transform (code, id) {
if (id.endsWith('.cssr.js')) {
return cleanCssr(code)
}
}
})

View File

@ -0,0 +1,9 @@
const getDemoByPath = require('./utils/get-demo-by-path')
module.exports = function () {
return {
async load (id) {
return getDemoByPath(id)
}
}
}

View File

@ -0,0 +1,3 @@
module.exports = function cleanCssr (code) {
return code.replace(/\n\s+/g, '\n')
}

View File

@ -0,0 +1,21 @@
const fs = require('fs').promises
const demoLoader = require('../loaders/naive-ui-demo-loader')
const docEntryLoader = require('../loaders/naive-ui-doc-entry-loader')
const docLoader = require('../loaders/naive-ui-doc-loader')
const mdLoader = require('../loaders/naive-ui-md-loader')
module.exports = async function getDemoByPath (path) {
if (path.endsWith('.demo.md')) {
const code = await fs.readFile(path, 'utf-8')
return demoLoader(code, path)
} else if (path.endsWith('.entry')) {
return docEntryLoader(path)
} else if (path.endsWith('.demo-entry.md')) {
const code = await fs.readFile(path, 'utf-8')
return docLoader(code, path)
} else if (path.endsWith('.md')) {
const code = await fs.readFile(path, 'utf-8')
return mdLoader(code, path)
}
}

View File

@ -0,0 +1,17 @@
const cleanCssr = require('./utils/clean-cssr')
module.exports = () => {
return {
configureServer: [
async ({ app }) => {
app.use(async (ctx, next) => {
await next()
// if not ctx.body, it should be a http 304
if (ctx.path.endsWith('.cssr.js') && ctx.body) {
ctx.body = cleanCssr(ctx.body.toString())
}
})
}
]
}
}

32
build/vite-plugin-demo.js Normal file
View File

@ -0,0 +1,32 @@
const getDemoByPath = require('./utils/get-demo-by-path')
module.exports = function () {
const configureServer = [
async ({ app, resolver, watcher }) => {
// check and send hmr message
watcher.on('change', async (file) => {
if (file.endsWith('.md')) {
const content = await getDemoByPath(file)
watcher.handleVueReload(file, Date.now(), content)
}
})
app.use(async (ctx, next) => {
if (/\/@modules\/naive-ui\/?/.test(ctx.path)) {
ctx.path = ctx.path.replace(/\/@modules\/naive-ui\/?/, '/@naive-ui/index.js')
}
if (/\.md$/.test(ctx.path) || /\.entry$/.test(ctx.path)) {
const publicPath = ctx.path
const filePath = resolver.requestToFile(publicPath)
const content = await getDemoByPath(filePath)
// make it Treat as vue
ctx.vue = true
ctx.body = content
}
await next()
})
}
]
return {
configureServer
}
}

View File

@ -1,74 +0,0 @@
const { readFileSync } = require('fs')
const NaiveUIDemoLoader = require('../loaders/NaiveUIDemoLoader')
const NaiveUIDocEntryLoader = require('../loaders/NaiveUIDocEntryLoader')
const NaiveUIDocLoader = require('../loaders/NaiveUIDocLoader')
const NaiveUIMdLoader = require('../loaders/NaiveUIMdLoader')
module.exports = {
viteMdPlugin: function () {
const configureServer = [
async ({ app, resolver, watcher }) => {
// check and send hmr message
watcher.on('change', (file) => {
if (file.endsWith('.md')) {
const content = mdTransform2VueTemplateHandlers(file)
watcher.handleVueReload(file, Date.now(), content)
}
})
app.use(async (ctx, next) => {
if (/\/@modules\/naive-ui\/?/.test(ctx.path)) {
ctx.path = ctx.path.replace(/\/@modules\/naive-ui\/?/, '/@naive-ui/index.js')
}
if (/.md$/.test(ctx.path) || ctx.path.endsWith('.entry')) {
const publicPath = ctx.path
const filePath = resolver.requestToFile(publicPath)
const content = mdTransform2VueTemplateHandlers(filePath)
// make it Treat as vue
ctx.vue = true
ctx.body = content
await next()
} else {
await next()
}
})
}
]
return {
configureServer
}
},
rollupMdPlugin: function () {
return {
async load (id) {
if (id.endsWith('.demo.md')) {
const code = readFileSync(id).toString()
return NaiveUIDemoLoader(code, id)
} else if (id.endsWith('.entry')) {
return NaiveUIDocEntryLoader(id)
} else if (id.endsWith('.demo-entry.md')) {
const code = readFileSync(id).toString()
return NaiveUIDocLoader(code, id)
} else if (id.endsWith('.md')) {
const code = readFileSync(id).toString()
return NaiveUIMdLoader(code, id)
}
}
}
}
}
function mdTransform2VueTemplateHandlers (path) {
let content = null
const mdContent = readFileSync(path, 'utf-8')
if (/\.demo\.md$/.test(path)) {
content = NaiveUIDemoLoader(mdContent, path)
} else if (path.endsWith('.entry')) {
content = NaiveUIDocEntryLoader(path)
} else if (/\.demo-entry\.md$/.test(path)) {
content = NaiveUIDocLoader(mdContent, path)
} else {
content = NaiveUIMdLoader(mdContent, path)
}
return content
}

View File

@ -83,13 +83,14 @@
"@vicons/fluent": "^0.0.1",
"@vicons/ionicons-v4": "^0.0.1",
"@vicons/ionicons-v5": "^0.0.1",
"async-validator": "^1.11.5",
"async-validator": "^3.4.0",
"css-render": "^0.11.1",
"date-fns": "^2.9.0",
"evtd": "^0.0.1-alpha.1",
"highlight.js": "^9.18.1",
"lodash-es": "^4.17.15",
"treemate": "^0.1.9",
"vfonts": "^0.0.1",
"vooks": "^0.0.1",
"vue": "^3.0.2",
"vueuc": "0.1.0-alpha.9"

View File

@ -1,4 +1,5 @@
const vuePlugin = require('rollup-plugin-vue')
const cssRenderPlugin = require('./build/rollup-plugin-css-render')
const { nodeResolve } = require('@rollup/plugin-node-resolve')
const { terser } = require('rollup-plugin-terser')
const replace = require('@rollup/plugin-replace')
@ -29,6 +30,7 @@ module.exports = {
nodeResolve({
extensions: ['.js', '.json', '.vue']
}),
cssRenderPlugin(),
vuePlugin(),
terser({
mangle: false,

View File

@ -1,12 +1,15 @@
const path = require('path')
const alias = require('@rollup/plugin-alias')
const { viteMdPlugin, rollupMdPlugin } = require('./demo/vite-plugins/mdPlugin')
const rollupCssRenderPlugin = require('./build/rollup-plugin-css-render')
const rollupDemoPlugin = require('./build/rollup-plugin-demo')
const viteCssRenderPlugin = require('./build/vite-plugin-css-render')
const viteDemoPlugin = require('./build/vite-plugin-demo')
module.exports = {
root: __dirname,
plugins: [
viteMdPlugin()
viteDemoPlugin(),
viteCssRenderPlugin()
],
outDir: 'site',
optimizeDeps: {
@ -39,7 +42,8 @@ module.exports = {
}
]
}),
rollupMdPlugin()
rollupDemoPlugin(),
rollupCssRenderPlugin()
]
},
indexHtmlTransforms: [
@ -47,7 +51,6 @@ module.exports = {
apply: 'pre',
transform ({ code }) {
const isTusimple = !!process.env.TUSIMPLE
console.log('/demo/index.' + isTusimple ? 'ts-dev.js' : 'dev.js')
switch (process.env.NODE_ENV) {
case 'production':
return code.replace(

View File

@ -323,7 +323,7 @@
- [x] use-global-style ? 感觉效果不会特别好而且还是很无谓的性能开销
这个问题可以之后再思考,现阶段 vue 的写法还是全局 install
- [x] dynamic-input, no value
- [ ] button, input 的变量名需要修改一下
- [ ] button, input 的 css 变量名需要修改一下
- [x] test rollup treeshaking
- [x] test webpack treeshaking