feat: initial

This commit is contained in:
unix
2020-03-19 01:15:58 +08:00
parent a37c630d54
commit 1a258b23d0
181 changed files with 17112 additions and 0 deletions

49
scripts/collect-meta.js Normal file
View 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
View 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
View 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'],
},
},
],
},
}
}