mirror of
https://github.com/tusen-ai/naive-ui.git
synced 2025-01-18 12:34:25 +08:00
build: extract loaders to build/, compress cssr.js files
This commit is contained in:
parent
672eee1be8
commit
c7e283560c
@ -3,3 +3,4 @@ lib
|
||||
test/unit/coverage
|
||||
src/_deprecated/icons
|
||||
dist
|
||||
es
|
||||
|
@ -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')
|
@ -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) {
|
@ -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) {
|
@ -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) {
|
@ -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')
|
||||
|
9
build/rollup-plugin-css-render.js
Normal file
9
build/rollup-plugin-css-render.js
Normal 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)
|
||||
}
|
||||
}
|
||||
})
|
9
build/rollup-plugin-demo.js
Normal file
9
build/rollup-plugin-demo.js
Normal file
@ -0,0 +1,9 @@
|
||||
const getDemoByPath = require('./utils/get-demo-by-path')
|
||||
|
||||
module.exports = function () {
|
||||
return {
|
||||
async load (id) {
|
||||
return getDemoByPath(id)
|
||||
}
|
||||
}
|
||||
}
|
3
build/utils/clean-cssr.js
Normal file
3
build/utils/clean-cssr.js
Normal file
@ -0,0 +1,3 @@
|
||||
module.exports = function cleanCssr (code) {
|
||||
return code.replace(/\n\s+/g, '\n')
|
||||
}
|
21
build/utils/get-demo-by-path.js
Normal file
21
build/utils/get-demo-by-path.js
Normal 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)
|
||||
}
|
||||
}
|
17
build/vite-plugin-css-render.js
Normal file
17
build/vite-plugin-css-render.js
Normal 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
32
build/vite-plugin-demo.js
Normal 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
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
@ -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"
|
||||
|
@ -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,
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user