@qse/edu-scripts 1.13.7 → 1.13.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/CHANGELOG.md +14 -1
  2. package/app.d.ts +10 -0
  3. package/docs/feat.md +84 -0
  4. package/docs/override.md +7 -0
  5. package/lib/auto-refactor.js +88 -95
  6. package/lib/build.js +33 -36
  7. package/lib/cli.js +64 -61
  8. package/lib/commit-dist.js +40 -54
  9. package/lib/config/babel.dependencies.js +41 -39
  10. package/lib/config/babel.js +82 -55
  11. package/lib/config/paths.js +28 -30
  12. package/lib/config/plugins/mock-server/defineMock.d.ts +6 -0
  13. package/lib/config/plugins/mock-server/defineMock.js +31 -0
  14. package/lib/config/plugins/mock-server/index.js +69 -0
  15. package/lib/config/plugins/postcss-safe-area.js +12 -12
  16. package/lib/config/webpackConfig.js +360 -325
  17. package/lib/config/webpackDevServerConfig.js +27 -29
  18. package/lib/deploy.js +58 -100
  19. package/lib/generator.js +49 -81
  20. package/lib/index.d.ts +1 -0
  21. package/lib/index.js +29 -9
  22. package/lib/start.js +16 -22
  23. package/lib/utils/FileSizeReporter.js +45 -56
  24. package/lib/utils/appConfig.js +12 -13
  25. package/lib/utils/beforeStart.js +35 -35
  26. package/lib/utils/changeDeployVersion.js +36 -65
  27. package/lib/utils/defineConfig.d.ts +7 -0
  28. package/lib/utils/defineConfig.js +27 -5
  29. package/lib/utils/exec.js +5 -9
  30. package/lib/utils/getConfig.js +6 -7
  31. package/lib/utils/getOverride.js +13 -17
  32. package/package.json +32 -29
  33. package/src/config/paths.js +1 -0
  34. package/src/config/plugins/mock-server/defineMock.ts +12 -0
  35. package/src/config/plugins/mock-server/index.js +92 -0
  36. package/src/config/webpackConfig.js +9 -2
  37. package/src/config/webpackDevServerConfig.js +8 -0
  38. package/src/index.ts +1 -0
  39. package/src/utils/defineConfig.ts +8 -1
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@qse/edu-scripts",
3
- "version": "1.13.7",
3
+ "version": "1.13.9",
4
4
  "author": "Kinoko",
5
5
  "license": "MIT",
6
6
  "description": "教育工程化基础框架",
7
7
  "engines": {
8
- "node": ">=12"
8
+ "node": ">=14"
9
9
  },
10
10
  "scripts": {
11
11
  "start": "dumi dev",
12
12
  "docs:build": "dumi build",
13
13
  "docs:deploy": "ssh-sftp",
14
- "build": "father-build",
14
+ "build": "father build",
15
15
  "deploy": "node scripts/deploy && rimraf docs-dist",
16
16
  "release": "npm run build && npm publish && rimraf lib",
17
17
  "prettier": "prettier -c -w \"src/**/*.{js,jsx,tsx,ts,less,md,json}\"",
@@ -34,58 +34,61 @@
34
34
  "extends": "qsb-react"
35
35
  },
36
36
  "dependencies": {
37
- "@babel/core": "~7.18.6",
38
- "@babel/plugin-proposal-decorators": "~7.18.6",
39
- "@babel/plugin-transform-runtime": "~7.18.6",
40
- "@babel/preset-env": "~7.18.6",
37
+ "@babel/core": "~7.18.13",
38
+ "@babel/plugin-proposal-decorators": "~7.18.10",
39
+ "@babel/plugin-transform-runtime": "~7.18.10",
40
+ "@babel/preset-env": "~7.18.10",
41
41
  "@babel/preset-react": "~7.18.6",
42
42
  "@babel/preset-typescript": "~7.18.6",
43
- "@babel/runtime": "~7.18.6",
43
+ "@babel/register": "~7.18.0",
44
+ "@babel/runtime": "~7.18.9",
44
45
  "@pmmmwh/react-refresh-webpack-plugin": "^0.5.10",
45
46
  "@qse/ssh-sftp": "^1.0.0",
46
- "@svgr/webpack": "^7.0.0",
47
+ "@svgr/webpack": "^8.0.1",
47
48
  "babel-loader": "^8.3.0",
48
49
  "babel-plugin-import": "^1.13.6",
49
50
  "case-sensitive-paths-webpack-plugin": "^2.4.0",
50
51
  "chalk": "^4.1.2",
51
- "css-loader": "^6.7.3",
52
- "cssnano": "^5.1.15",
52
+ "chokidar": "^3.5.3",
53
+ "cookie-parser": "^1.4.6",
54
+ "css-loader": "^6.8.1",
55
+ "cssnano": "^6.0.1",
53
56
  "esbuild-loader": "^2.21.0",
54
- "file-loader": "^6.2.0",
57
+ "express": "^4.18.2",
55
58
  "filesize": "^8.0.7",
56
59
  "fs-extra": "^10.1.0",
57
60
  "globby": "^11.1.0",
58
61
  "gzip-size": "^6.0.0",
59
- "html-webpack-plugin": "^5.5.0",
62
+ "html-webpack-plugin": "^5.5.3",
60
63
  "inquirer": "^8.2.5",
61
64
  "less": "^3.13.1",
62
65
  "less-loader": "^10.2.0",
63
66
  "lodash": "^4.17.21",
64
67
  "open": "^8.4.2",
65
- "postcss": "^8.4.21",
68
+ "postcss": "^8.4.27",
66
69
  "postcss-flexbugs-fixes": "^5.0.2",
67
- "postcss-loader": "^6.2.1",
70
+ "postcss-loader": "^7.3.3",
68
71
  "postcss-momentum-scrolling": "^3.14.22",
69
72
  "postcss-normalize": "^8.0.1",
70
- "postcss-preset-env": "^7.8.3",
71
- "prettier": "^2.8.7",
72
- "prettier-plugin-tailwindcss": "^0.1.13",
73
- "react-refresh": "^0.13.0",
73
+ "postcss-preset-env": "^9.1.0",
74
+ "prettier": "^3.0.0",
75
+ "prettier-plugin-tailwindcss": "^0.4.1",
76
+ "react-refresh": "^0.14.0",
74
77
  "recursive-readdir": "^2.2.3",
75
78
  "rimraf": "^3.0.2",
76
- "semver": "^7.3.8",
79
+ "semver": "^7.5.4",
77
80
  "strip-ansi": "^6.0.1",
78
- "style-loader": "^3.3.2",
79
- "tailwindcss": "^3.3.1",
80
- "terser-webpack-plugin": "~5.3.7",
81
+ "style-loader": "^3.3.3",
82
+ "tailwindcss": "^3.3.3",
83
+ "terser-webpack-plugin": "~5.3.9",
81
84
  "tmp": "^0.2.1",
82
- "typescript": "^4.9.5",
85
+ "typescript": "^5.1.6",
83
86
  "update-notifier": "^5.1.0",
84
87
  "url-loader": "^4.1.1",
85
- "webpack": "^5.78.0",
86
- "webpack-bundle-analyzer": "^4.8.0",
87
- "webpack-dev-server": "^4.13.2",
88
- "yargs": "^17.7.1"
88
+ "webpack": "^5.88.2",
89
+ "webpack-bundle-analyzer": "^4.9.0",
90
+ "webpack-dev-server": "^4.15.1",
91
+ "yargs": "^17.7.2"
89
92
  },
90
93
  "devDependencies": {
91
94
  "@types/fs-extra": "^9.0.13",
@@ -93,7 +96,7 @@
93
96
  "@types/yargs": "^17.0.24",
94
97
  "dumi": "^1.1.50",
95
98
  "eslint-config-qsb-react": "^1.1.1",
96
- "father-build": "^1.22.5",
99
+ "father": "^4.3.0",
97
100
  "jest": "^28.1.3"
98
101
  }
99
102
  }
@@ -33,5 +33,6 @@ const paths = {
33
33
  public: resolveApp('public'),
34
34
  static: resolveApp('public', 'static'),
35
35
  theme: getExistPath(resolveApp('theme.json'), resolveApp('theme.js')),
36
+ mock: resolveApp('mock'),
36
37
  }
37
38
  module.exports = paths
@@ -0,0 +1,12 @@
1
+ import type { RequestHandler } from 'express'
2
+ type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE'
3
+ type API = string
4
+
5
+ export type MockConfig = Record<
6
+ `${Method} ${API}`,
7
+ string | number | null | undefined | boolean | Record<string, any> | RequestHandler
8
+ >
9
+
10
+ export function defineMock(config: MockConfig) {
11
+ return config
12
+ }
@@ -0,0 +1,92 @@
1
+ const paths = require('../../paths')
2
+ const globby = require('globby')
3
+ const chokidar = require('chokidar')
4
+ const debounce = require('lodash/debounce')
5
+ const chalk = require('chalk')
6
+ const fs = require('fs-extra')
7
+ const express = require('express')
8
+ const cookieParser = require('cookie-parser')
9
+ let mockCache = {}
10
+ let isSetup = false
11
+
12
+ /**
13
+ * @param {import('webpack-dev-server').Middleware[]} middelwaves
14
+ */
15
+ const setupMock = debounce(function setupMock() {
16
+ // clean
17
+ mockCache = {}
18
+
19
+ const files = globby.sync(paths.mock)
20
+
21
+ if (isSetup) {
22
+ console.log(chalk.green('Mock files changed, reloaded'))
23
+ } else {
24
+ isSetup = true
25
+ }
26
+
27
+ // setup
28
+ for (const file of files) {
29
+ delete require.cache[require.resolve(file)]
30
+ try {
31
+ let mock = require(file)
32
+ mock = mock.default || mock
33
+ mockCache = { ...mockCache, ...mock }
34
+ } catch (e) {
35
+ console.error(chalk.red(`Mock file ${file} error: ${e.message}`))
36
+ }
37
+ }
38
+ console.log('🚀 ~ file: index.js:47 ~ setupMock ~ mockCache:', mockCache)
39
+ }, 100)
40
+
41
+ /**
42
+ * @type {import('express').RequestHandler}
43
+ */
44
+ function mockMiddlewave(req, res, next) {
45
+ const { method, path } = req
46
+ const key = `${method.toUpperCase()} ${path}`
47
+ const mock = mockCache[key]
48
+ console.log(chalk.green(`Mock: ${key}`))
49
+ if (mock) {
50
+ if (typeof mock === 'function') {
51
+ mock(req, res, next)
52
+ } else {
53
+ res.json(mock)
54
+ }
55
+ } else {
56
+ next()
57
+ }
58
+ }
59
+
60
+ /**
61
+ * @param {import('webpack-dev-server').Middleware[]} middelwaves
62
+ * @param {import('webpack-dev-server')} devServer
63
+ */
64
+ function setupMockServer(middelwaves, devServer) {
65
+ if (!fs.existsSync(paths.mock)) return
66
+
67
+ require('@babel/register')({
68
+ presets: [['@babel/preset-env', { modules: 'cjs' }], '@babel/preset-typescript'],
69
+ babelrc: false,
70
+ only: [/\/mock\//, /\/src\//],
71
+ extensions: ['.js', '.ts'],
72
+ })
73
+
74
+ devServer.app.use(express.urlencoded({ extended: false }))
75
+ devServer.app.use(express.json())
76
+ devServer.app.use(cookieParser())
77
+
78
+ middelwaves.unshift({
79
+ name: 'edu-scripts-mock-middelwave',
80
+ middleware: mockMiddlewave,
81
+ })
82
+
83
+ console.log(
84
+ `<i> ${chalk.green.bold('[edu-scripts] Mock server created:')} ${chalk.cyan.bold(paths.mock)}`
85
+ )
86
+
87
+ chokidar.watch(paths.mock).on('all', setupMock)
88
+
89
+ return middelwaves
90
+ }
91
+
92
+ module.exports = setupMockServer
@@ -200,11 +200,14 @@ module.exports = function getWebpackConfig(args, override) {
200
200
  rules: [
201
201
  {
202
202
  oneOf: [
203
- // 两个 babel-loader 是为了处理 pdfjs-dist 与 ofd.js
203
+ {
204
+ resourceQuery: /raw/,
205
+ type: 'asset/source',
206
+ },
204
207
  {
205
208
  test: /\.(j|t)sx?$/,
206
209
  loader: 'babel-loader',
207
- include: paths.src,
210
+ exclude: /node_modules/,
208
211
  options: require('./babel')(),
209
212
  },
210
213
  (override.transformNodeModules || isProd) && {
@@ -314,6 +317,10 @@ module.exports = function getWebpackConfig(args, override) {
314
317
  and: [/\.(ts|tsx|js|jsx|md|mdx)$/],
315
318
  },
316
319
  },
320
+ {
321
+ test: /\.md$/,
322
+ type: 'asset/source',
323
+ },
317
324
  {
318
325
  // Exclude `js` files to keep "css" loader working as it injects
319
326
  // its runtime that would otherwise be processed through "file" loader.
@@ -1,4 +1,5 @@
1
1
  const WebpackDevServer = require('webpack-dev-server')
2
+ const setupMockServer = require('./plugins/mock-server')
2
3
 
3
4
  /**
4
5
  * @param {*} args
@@ -30,6 +31,13 @@ module.exports = function getWebpackDevServerConfig(args, override) {
30
31
  'Access-Control-Allow-Methods': '*',
31
32
  'Access-Control-Allow-Headers': '*',
32
33
  },
34
+ setupMiddlewares: (middlewares, devServer) => {
35
+ if (override.mock !== false) {
36
+ setupMockServer(middlewares, devServer)
37
+ }
38
+
39
+ return middlewares
40
+ },
33
41
  proxy: [
34
42
  ...override.proxy,
35
43
  {
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export { defineConfig } from './utils/defineConfig'
2
+ export { defineMock } from './config/plugins/mock-server/defineMock'
@@ -9,7 +9,7 @@ export type BabelImportPlugin = [
9
9
  camel2DashComponentName?: boolean
10
10
  style?: boolean | 'css' | ((name: string) => string)
11
11
  },
12
- string
12
+ string,
13
13
  ]
14
14
  export type BabelConfig = {
15
15
  presets: any[]
@@ -57,6 +57,13 @@ export type Config = {
57
57
  */
58
58
  proxy?: ProxyConfigArray
59
59
  extraPostCSSPlugins?: any[]
60
+ /**
61
+ * 开启 mock 功能,会自动加载 mock 文件夹下的文件
62
+ *
63
+ * 默认情况下自动判断根目录是否存在 mock 文件夹,如果存在则开启 mock 功能。
64
+ * 如果设置 false,会关闭 mock 功能
65
+ */
66
+ mock?: boolean
60
67
  }
61
68
  export function defineConfig(config: Config) {
62
69
  return config