![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
cw-egg-boilerplate-simple
Advanced tools
git clone https://github.com/maidol/egg-sample.git
cd egg-sample
npm i # 安装依赖
npm run dev # 开发环境
npm run test # 单元测试
npm run cov # 代码覆盖率
npm run start # 生产环境
npm run hint # 检查语法
安装vscode
安装格式化插件 Beautify
安装语法检查插件 ESLint
tab配置制表符大小为2, 并将缩进转换为tab制表符
// 在用户设置默认
{ "editor.tabSize": 2 }
// 在用户设置默认
// 默认行尾字符。使用 \n 表示 LF,\r\n 表示 CRLF。
{ "files.eol": "\n"}
vscode开发环境下, 按f5
使用tab缩进, 设置制表符大小为2
块级作用域
使用let取代var
优先使用const常量定义, 所有函数都应该设置为常量
静态字符串一律使用单引号或反引号,不使用双引号。动态字符串使用反引号
解构赋值
数组
使用扩展运算符(...)拷贝数组
// bad
const len = items.length;
const itemsCopy = [];
let i;
for (i = 0; i < len; i++) {
itemsCopy[i] = items[i];
}
// good
const itemsCopy = [...items];
尽量用箭头函数代替, 简洁且绑定了this, 箭头函数取代Function.prototype.bind, 不再用 self/_this/that
const obj = {
t1: function(){
const d1 = function(){
console.log('d1', this);
}
const d2 = () => {
console.log('d2', this);
}
d1(); // this 不是 obj
d2(); // this = obj
}
}
obj.t1();
使用默认值语法设置函数参数的默认值
// bad
function handleThings(opts) {
opts = opts || {};
}
// good
function handleThings(opts = {}) {
// ...
}
总是用 Class,取代需要 prototype 的操作
import/export, 目前此框架未支持此语法特性
安装格式化工具 js-beautify for VS Code
一个语法规则和代码风格的检查工具,可以用来保证写出语法正确、风格统一的代码
npm install --save-dev eslint
npm install --save-dev eslint-config-airbnb-base eslint-plugin-import
新建一个.eslintrc文件,配置ESLint
{
"extends": "standard",
"rules": {
"semi": [ 2, "always"],
"semi-spacing": 1,
"space-before-function-paren": 0,
"no-trailing-spaces": 1,
"eol-last": 0,
"indent": [2, "tab"],
"comma-dangle": 0,
"curly": 0,
"spaced-comment": 1,
"no-tabs": 0,
"generator-star-spacing": 0
}
}
# 检查指定文件
eslint index.js
添加必要的提交描述(功能, bug相关)
先拉取, 检查语法, 再推送
# 检查语法
npm run hint
NODE_ENV
EGG_SERVER_ENV
日志组件cw-logger
数据库mysql
mysql组件添加了promise的封装层, 调用 const [result, fields] = await pool.query(...) 方法返回的数据类型为数组
数据校验joi
Application(Koa的全局应用对象)
{ logger, coreLogger, config, Controller, Service, middleware, middlewares }
配置Config
{ appMiddleware, coreMiddleware }
Context
{ app, logger, coreLogger, service, request, response, helper }
Controller
{ ctx, app, config, logger }
Service
{ ctx, app, config, logger, service }
Logger
连接池
const pool = app.dbpool('eggsample');
const [rows, fields] = await pool.query('select * from user');
数据库事务
const [insertResult, updateResult] = await app.beginTransaction('eggsample', async(tran) => {
let [insertResult] = await tran.query('insert into user set ?;', {
name: 'king1'
});
const sql = tran.format('update user set ? where ?;', [{
name: 'king3'
}, {
id: 13
}]);
let [updateResult] = await tran.query(sql);
return [insertResult, updateResult];
});
app.Joi, 参数校验统一放在路由层
app.get('/validate', app.middlewares.validate({
// auth: false,
req: {
headers: app.Joi.object().unknown(),
body: app.Joi.object().unknown(),
query: {
account: app.Joi.string().example('12345678901').description('邮箱/手机号码').required(),
password: app.Joi.string().min(3).max(24).example('1234').description('密码').required()
}
},
// res: {
// id: app.Joi.number().min(3).required()
// }
}, app), app.controller.home.index);
npm run test
TESTS=test/x.test.js npm test
TEST_REPORTER=dot npm test
TEST_TIMEOUT=5000 npm test
$ # npm 传递参数需额外加一个 `--`,参见 https://docs.npmjs.com/cli/run-script
$ npm test -- --help
$
$ # 等同于 `TESTS=test/**/test.js npm test`,受限于 bash,最好加上双引号
$ npm test "test/**/test.js"
$
$ # 等同于 `TEST_REPORTER=dot npm test`
$ npm test -- --reporter=dot
$
$ # 支持 mocha 的参数,如 grep / require 等
$ npm test -- -t 30000 --grep="should GET"
npm run cov
和 test 命令一样,cov 命令执行时,应用也是以 env: unittest 启动的,读取的配置也是 config.default.js 和 config.unittest.js 合并的结果
FAQs
boilerplate for cw egg quickstart
The npm package cw-egg-boilerplate-simple receives a total of 0 weekly downloads. As such, cw-egg-boilerplate-simple popularity was classified as not popular.
We found that cw-egg-boilerplate-simple demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.