mirror of
https://github.com/zhigang1992/react.git
synced 2026-03-26 06:55:07 +08:00
feat: initial
This commit is contained in:
49
scripts/collect-meta.js
Normal file
49
scripts/collect-meta.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const extractMetadata = require('extract-mdx-metadata')
|
||||
const pagePrefix = path.join(__dirname, '../pages')
|
||||
const docsDir = path.join(__dirname, '../pages/docs')
|
||||
const targetPath = path.join(__dirname, '../lib/data/metadata.json')
|
||||
const weights = {
|
||||
'getting-started': 1,
|
||||
'customization': 2,
|
||||
'components': 3,
|
||||
}
|
||||
|
||||
const getMetadata = async (files, parentPath) => {
|
||||
return Promise.all(
|
||||
files
|
||||
.filter(name => name.endsWith('.mdx') || !name.includes('.'))
|
||||
.map(async file => {
|
||||
const filePath = path.join(parentPath, file)
|
||||
const isDirectory = fs.statSync(filePath).isDirectory()
|
||||
if (isDirectory) {
|
||||
const children = await fs.readdir(filePath)
|
||||
const childrenMetadata = await getMetadata(children, filePath)
|
||||
const sorted = childrenMetadata.sort((a, b) => a.index - b.index)
|
||||
return { name: file, children: sorted }
|
||||
}
|
||||
const content = await fs.readFile(filePath, 'utf-8')
|
||||
const meta = await extractMetadata(content)
|
||||
const url = filePath
|
||||
.replace(pagePrefix, '')
|
||||
.replace('.mdx', '')
|
||||
return { name: meta.title || file, url, index: meta.index || 100, meta }
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
;(async () => {
|
||||
try {
|
||||
const files = await fs.readdir(docsDir)
|
||||
const data = await getMetadata(files, docsDir)
|
||||
const sorted = data.sort((a, b) => {
|
||||
return weights[a.name] - weights[b.name]
|
||||
})
|
||||
await fs.ensureFile(targetPath)
|
||||
await fs.writeJson(targetPath, sorted)
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
process.exit(1)
|
||||
}
|
||||
})()
|
||||
36
scripts/tsconfig.json
Normal file
36
scripts/tsconfig.json
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": "../",
|
||||
"outDir": "../dist",
|
||||
"declaration": true,
|
||||
"emitDeclarationOnly": true,
|
||||
"strictNullChecks": true,
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"experimentalDecorators": true,
|
||||
"jsx": "preserve",
|
||||
"noUnusedParameters": true,
|
||||
"noUnusedLocals": true,
|
||||
"noImplicitAny": true,
|
||||
"target": "es6",
|
||||
"lib": [
|
||||
"dom",
|
||||
"es2017"
|
||||
],
|
||||
"skipLibCheck": true,
|
||||
"allowJs": true,
|
||||
"strict": false,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"module": "esnext",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
},
|
||||
"files": [
|
||||
"../components/index.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"lib",
|
||||
"es"
|
||||
]
|
||||
}
|
||||
78
scripts/webpack.config.js
Normal file
78
scripts/webpack.config.js
Normal file
@@ -0,0 +1,78 @@
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const componentsPath = path.join(__dirname, '../components')
|
||||
|
||||
module.exports = async () => {
|
||||
const files = await fs.readdir(componentsPath)
|
||||
|
||||
const components = await Promise.all(files.map(async name => {
|
||||
const comPath = path.join(componentsPath, name)
|
||||
const entry = path.join(comPath, 'index.ts')
|
||||
|
||||
const stat = await fs.stat(comPath)
|
||||
if (!stat.isDirectory()) return null
|
||||
|
||||
const hasFile = await fs.pathExists(entry)
|
||||
if (!hasFile) return null
|
||||
|
||||
return { name, url: entry }
|
||||
}))
|
||||
|
||||
const componentsEntries = components
|
||||
.filter(r => r)
|
||||
.reduce((pre, current) => {
|
||||
return Object.assign({}, pre, { [current.name]: current.url })
|
||||
}, { index: path.join(componentsPath, 'index.ts') })
|
||||
|
||||
console.log(`\n${Object.keys(componentsEntries).length} Components in total have been collected.`)
|
||||
console.log('Bundle now...')
|
||||
|
||||
return {
|
||||
mode: 'production',
|
||||
|
||||
entry: componentsEntries,
|
||||
|
||||
output: {
|
||||
filename: '[name].js',
|
||||
path: path.resolve(__dirname, '../dist'),
|
||||
library: '',
|
||||
libraryTarget: 'commonjs',
|
||||
},
|
||||
|
||||
resolve: {
|
||||
extensions: ['.ts', '.tsx', '.js'],
|
||||
alias: {
|
||||
components: componentsPath,
|
||||
},
|
||||
},
|
||||
|
||||
externals: {
|
||||
react: {
|
||||
root: 'React',
|
||||
commonjs2: 'react',
|
||||
commonjs: 'react',
|
||||
amd: 'react'
|
||||
},
|
||||
'react-dom': {
|
||||
root: 'ReactDOM',
|
||||
commonjs2: 'react-dom',
|
||||
commonjs: 'react-dom',
|
||||
amd: 'react-dom'
|
||||
}
|
||||
},
|
||||
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.tsx?$/,
|
||||
exclude: /(node_modules)/,
|
||||
loader: 'babel-loader',
|
||||
options: {
|
||||
presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'],
|
||||
plugins: ['styled-jsx/babel'],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user