@sanpjs/core
Version:
@sanpjs/core
392 lines • 11.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schema_utils_1 = require("schema-utils");
const getInspectSchema = (value) => {
return {
anyOf: [
{
type: 'string',
enum: ['off', 'info']
},
{
type: 'array',
items: [
{
type: 'string',
enum: ['off', 'info', 'warn', 'error']
},
value
],
additionalItems: false
}
]
};
};
const getPageSchema = (requireName = false) => {
const tagSchema = {
type: 'object',
properties: {
attributes: {
type: 'object'
},
tags: {
type: 'array',
items: {
type: 'object',
properties: {
tagName: {
type: 'string'
},
attributes: {
type: 'object'
},
innerHTML: {
type: 'string'
}
}
}
}
},
additionalProperties: false
};
const schema = {
type: 'object',
properties: {
name: {
type: 'string'
},
filename: {
type: 'string'
},
layout: {
type: 'string'
},
title: {
type: 'string'
},
html: {
type: 'object',
properties: {
attributes: {
type: 'object'
}
},
additionalProperties: false
},
head: tagSchema,
body: tagSchema
}
};
if (requireName) {
schema.required = ['name'];
}
return schema;
};
const getOptionalSchema = (hasParse) => {
let parseOpt = hasParse ? { parseOptions: { type: 'object' } } : {};
return {
anyOf: [
{
type: 'string',
enum: ['off', 'info', 'warn', 'error']
},
{
type: 'array',
items: [
{
type: 'string',
enum: ['off', 'info', 'warn', 'error']
},
{
type: 'object',
properties: {
includes: {
// 参数可以是具体包名也可以是正则
type: 'array',
items: {
anyOf: [{ type: 'string' }, { instanceof: 'RegExp' }]
}
},
excludes: {
type: 'array',
items: {
anyOf: [{ type: 'string' }, { instanceof: 'RegExp' }]
}
},
...parseOpt
},
additionalProperties: false
}
],
additionalItems: false
}
]
};
};
const boolOrString = {
anyOf: [{ type: 'string' }, { type: 'boolean' }]
};
const boolOrObject = {
anyOf: [{ type: 'boolean' }, { type: 'object' }]
};
const schema = {
type: 'object',
properties: {
// 项目根目录
root: {
type: 'string'
},
pagesDir: {
type: 'string'
},
sanpDir: {
type: 'string'
},
attachedElement: {
type: 'string'
},
// 工程类型: spa含有前端路由的单页应用,mpa多入口单文件应用,配合ssr配置项可以开启单页或多页ssr渲染应用
// 不配置pages,默认为spa应用
// 配置pages,默认为mpa应用
type: {
type: 'string',
enum: ['spa', 'mpa', 'spa-ssr', 'mpa-ssr', 'component']
},
// mode development production
mode: {
type: 'string',
enum: ['development', 'production']
},
// 插件
plugins: {
anyOf: [
{
type: 'array',
items: {
instanceof: 'Function'
}
},
{
instanceof: 'Function'
}
]
},
// 页面配置
pages: {
anyOf: [
// key就是页面名称name
getPageSchema(false),
{
type: 'array',
items: [getPageSchema(true)]
},
{
type: 'string'
}
]
},
// 构建进度
progress: {
type: 'boolean'
},
// 监听模式
watch: {
type: 'boolean'
},
// build
build: {
type: 'object',
properties: {
// 页面复制
copy: {
type: 'array',
items: [
{
type: 'object',
additionalProperties: {
type: 'string'
}
},
{
type: 'string'
},
],
additionalItems: false
},
outDir: {
type: 'string'
},
analyze: boolOrObject,
clean: {
type: 'boolean'
},
esModule: {
type: 'boolean'
},
sourceMap: boolOrString,
publicPath: {
type: 'string'
},
assetDir: {
type: 'string'
},
hash: {
type: 'boolean'
},
largeAssetSize: {
type: 'number'
},
cache: boolOrObject,
parallel: {
type: 'boolean'
},
esbuild: {
type: 'boolean'
},
// 检查代码,比如检查出现额外的依赖、检查最后代码是不是es5
inspect: {
type: 'object',
properties: {
initialRes: {
type: 'object',
properties: {
count: getInspectSchema({ type: 'number' }),
totalSize: getInspectSchema({ type: 'number' }),
sizeDeviation: getInspectSchema({ type: 'number' }),
disallowImports: getInspectSchema({ type: 'array', items: { type: 'string' } })
},
additionalProperties: false
},
dupPkg: getOptionalSchema(),
esCheck: getOptionalSchema(true)
},
additionalProperties: false
},
// script
script: {
type: 'object',
properties: {
babel: boolOrObject,
polyfills: {
anyOf: [{ type: 'array' }, { type: 'string' }, { type: 'boolean' }]
},
finalize: { instanceof: 'Function' }
},
additionalProperties: false
},
// style
style: {
type: 'object',
properties: {
extract: {
type: 'boolean'
},
modules: boolOrObject
},
additionalProperties: false
},
optimization: {
type: 'object',
properties: {
splitChunks: { type: 'object' },
terser: { type: 'object' },
cssnano: { type: 'object' },
htmlMinify: { type: 'object' }
}
},
finalize: {
instanceof: 'Function'
}
},
additionalProperties: false
},
server: {
type: 'object',
properties: {
host: {
type: 'string'
},
port: {
type: 'number'
},
https: {
type: 'boolean'
},
proxy: {
type: 'object'
},
hot: {
type: 'boolean'
},
mock: {
anyOf: [
{
type: 'boolean'
},
{
type: 'object',
properties: {
mockRules: {
type: 'object'
},
mockDir: {
type: 'string'
},
proxy: {
type: 'object'
}
}
},
{
type: 'string'
},
{
instanceof: 'Function'
}
],
},
static: {
type: 'string'
},
headers: {
type: 'object'
},
finalize: {
instanceof: 'Function'
},
devMiddleware: {
type: 'object',
properties: {
serverSideRender: {
type: 'boolean'
},
writeToDisk: {
type: 'boolean'
},
mfs: {
type: 'boolean'
}
},
additionalProperties: false
},
},
additionalProperties: true
},
// ssr预留
ssr: {
type: 'object'
}
},
additionalProperties: false
};
/**
* 校验参数
* @param {Object} options 校验参数
*/
exports.default = (options) => {
// @ts-ignore
(0, schema_utils_1.validate)(schema, options, {
baseDataPath: 'sanp config',
});
};
//# sourceMappingURL=validate.js.map