@qse/edu-scripts 0.0.0-beta.0

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 (81) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +107 -0
  3. package/app.d.ts +73 -0
  4. package/babel.config.json +3 -0
  5. package/docs/.vitepress/config.ts +35 -0
  6. package/docs/changelog.md +1 -0
  7. package/docs/debug.md +17 -0
  8. package/docs/deploy.md +54 -0
  9. package/docs/faq.md +144 -0
  10. package/docs/feat.md +167 -0
  11. package/docs/grayscale.md +31 -0
  12. package/docs/index.md +15 -0
  13. package/docs/install.md +1 -0
  14. package/docs/mode.md +42 -0
  15. package/docs/override.md +193 -0
  16. package/docs/refactor-react-16.md +37 -0
  17. package/docs/refactor.md +67 -0
  18. package/docs/static.md +24 -0
  19. package/es/asset/dll/libcommon3-manifest.json +181 -0
  20. package/es/asset/template/edu-app-env.d.ts.tpl +20 -0
  21. package/es/asset/template/edu-scripts.override.js.tpl +7 -0
  22. package/es/asset/template/tailwind.config.js.tpl +11 -0
  23. package/es/asset/template/tsconfig.json.tpl +24 -0
  24. package/es/auto-refactor.js +153 -0
  25. package/es/build.js +58 -0
  26. package/es/cli.js +65 -0
  27. package/es/commit-dist.js +83 -0
  28. package/es/config/paths.js +39 -0
  29. package/es/config/plugins/mock-server/defineMock.d.ts +6 -0
  30. package/es/config/plugins/mock-server/defineMock.js +7 -0
  31. package/es/config/plugins/mock-server/index.js +122 -0
  32. package/es/config/plugins/postcss-safe-area.js +22 -0
  33. package/es/config/plugins/ws-utils-createSocketURL.js +98 -0
  34. package/es/config/webpackConfig.js +418 -0
  35. package/es/config/webpackDevServerConfig.js +73 -0
  36. package/es/deploy.js +148 -0
  37. package/es/generator.js +52 -0
  38. package/es/index.d.ts +2 -0
  39. package/es/index.js +7 -0
  40. package/es/start.js +36 -0
  41. package/es/utils/FileSizeReporter.js +107 -0
  42. package/es/utils/appConfig.js +35 -0
  43. package/es/utils/beforeStart.js +51 -0
  44. package/es/utils/changeDeployVersion.js +85 -0
  45. package/es/utils/defineConfig.d.ts +76 -0
  46. package/es/utils/defineConfig.js +7 -0
  47. package/es/utils/exec.js +10 -0
  48. package/es/utils/getConfig.js +23 -0
  49. package/es/utils/getOverride.js +28 -0
  50. package/eslint.config.mjs +3 -0
  51. package/jest.config.mjs +199 -0
  52. package/package.json +95 -0
  53. package/src/asset/dll/libcommon3-manifest.json +181 -0
  54. package/src/asset/template/edu-app-env.d.ts.tpl +20 -0
  55. package/src/asset/template/edu-scripts.override.js.tpl +7 -0
  56. package/src/asset/template/tailwind.config.js.tpl +11 -0
  57. package/src/asset/template/tsconfig.json.tpl +24 -0
  58. package/src/auto-refactor.js +170 -0
  59. package/src/build.js +64 -0
  60. package/src/cli.js +88 -0
  61. package/src/commit-dist.js +103 -0
  62. package/src/config/paths.js +38 -0
  63. package/src/config/plugins/mock-server/defineMock.ts +12 -0
  64. package/src/config/plugins/mock-server/index.js +150 -0
  65. package/src/config/plugins/postcss-safe-area.js +21 -0
  66. package/src/config/plugins/ws-utils-createSocketURL.js +140 -0
  67. package/src/config/webpackConfig.js +444 -0
  68. package/src/config/webpackDevServerConfig.js +83 -0
  69. package/src/deploy.js +182 -0
  70. package/src/generator.js +67 -0
  71. package/src/index.ts +2 -0
  72. package/src/start.js +37 -0
  73. package/src/utils/FileSizeReporter.js +148 -0
  74. package/src/utils/appConfig.js +36 -0
  75. package/src/utils/beforeStart.js +55 -0
  76. package/src/utils/changeDeployVersion.js +119 -0
  77. package/src/utils/defineConfig.ts +81 -0
  78. package/src/utils/exec.js +7 -0
  79. package/src/utils/getConfig.js +26 -0
  80. package/src/utils/getOverride.js +33 -0
  81. package/tsconfig.json +21 -0
@@ -0,0 +1,67 @@
1
+ import path from 'path'
2
+ import fs from 'fs-extra'
3
+ import paths from './config/paths.js'
4
+ import chalk from 'chalk'
5
+ import { fileURLToPath } from 'node:url'
6
+
7
+ const __dirname = path.dirname(fileURLToPath(import.meta.url))
8
+
9
+ /**
10
+ * @param {string[]} args
11
+ * @returns {string}
12
+ */
13
+ const getTmpPath = (...args) => path.resolve(__dirname, 'asset', 'template', ...args)
14
+
15
+ async function generatorOverride() {
16
+ if (fs.existsSync(paths.override)) {
17
+ console.log(chalk.red(`文件已存在 ${paths.override}`))
18
+ process.exit(0)
19
+ }
20
+
21
+ fs.copySync(getTmpPath('edu-scripts.override.js.tpl'), paths.override)
22
+
23
+ console.log(chalk.green(`成功生成 ${paths.override}`))
24
+ }
25
+
26
+ async function generatorTsconfig() {
27
+ if (fs.existsSync(paths.tsconfig)) {
28
+ console.log(chalk.red(`文件已存在 ${paths.tsconfig}`))
29
+ process.exit(0)
30
+ }
31
+
32
+ fs.copySync(getTmpPath('tsconfig.json.tpl'), paths.tsconfig)
33
+ fs.copySync(getTmpPath('edu-app-env.d.ts.tpl'), paths.eduAppEnv)
34
+
35
+ console.log(chalk.green(`成功生成 ${paths.tsconfig}`))
36
+ }
37
+
38
+ async function generatorTailwind() {
39
+ if (fs.existsSync(paths.tailwind)) {
40
+ console.log(chalk.red(`文件已存在 ${paths.tailwind}`))
41
+ process.exit(0)
42
+ }
43
+
44
+ fs.copySync(getTmpPath('tailwind.config.js.tpl'), paths.tailwind)
45
+
46
+ const code = ['@tailwind base;', '@tailwind components;', '@tailwind utilities;'].join('\n')
47
+
48
+ const globalLessFile = paths.resolveApp('src', 'index.less')
49
+ if (fs.existsSync(globalLessFile)) {
50
+ const content = fs.readFileSync(globalLessFile, 'utf-8')
51
+ if (!content.includes('@tailwind base')) {
52
+ fs.writeFileSync(globalLessFile, [code, content].join('\n'))
53
+ }
54
+
55
+ console.log(chalk.green(`成功生成 ${paths.tailwind}`))
56
+
57
+ return
58
+ }
59
+
60
+ console.log(
61
+ chalk.green(
62
+ [`成功生成 ${paths.tailwind}`, '', '添加以下代码到入口处的 index.less 中', code].join('\n')
63
+ )
64
+ )
65
+ }
66
+
67
+ export { generatorOverride as override, generatorTsconfig as ts, generatorTailwind as tailwind }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { defineConfig } from './utils/defineConfig.js'
2
+ export { defineMock } from './config/plugins/mock-server/defineMock.js'
package/src/start.js ADDED
@@ -0,0 +1,37 @@
1
+ import { rspack } from '@rspack/core'
2
+ import { RspackDevServer } from '@rspack/dev-server'
3
+ import getConfig from './utils/getConfig.js'
4
+ import chalk from 'chalk'
5
+
6
+ export default async function start(args) {
7
+ process.env.NODE_ENV = 'development'
8
+ process.env.BABEL_ENV = 'development'
9
+ process.env.BROWSERSLIST = 'chrome >= 70'
10
+ process.env.WEBPACK_DEV_SERVER_BASE_PORT = '3000'
11
+
12
+ const basePort = process.env.WEBPACK_DEV_SERVER_BASE_PORT
13
+ const port = await RspackDevServer.getFreePort(args.port || process.env.PORT)
14
+ if (!(args.port || process.env.PORT) && +port !== +basePort) {
15
+ console.log(chalk.bgYellow(`${basePort} 端口已被占用,现切换到 ${port} 端口运行`))
16
+ }
17
+ args.port = port
18
+ process.env.PORT = port
19
+
20
+ const compiler = rspack(getConfig(args))
21
+ const devServer = new RspackDevServer(compiler.options.devServer, compiler)
22
+ devServer.start()
23
+ ;[('SIGINT', 'SIGTERM')].forEach(function (sig) {
24
+ process.on(sig, function () {
25
+ devServer.stop()
26
+ process.exit()
27
+ })
28
+ })
29
+
30
+ if (process.env.CI !== 'true') {
31
+ // Gracefully exit when stdin ends
32
+ process.stdin.on('end', function () {
33
+ devServer.stop()
34
+ process.exit()
35
+ })
36
+ }
37
+ }
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import fs from 'fs'
9
+ import path from 'path'
10
+ import chalk from 'chalk'
11
+ import filesize from 'filesize'
12
+ import recursive from 'recursive-readdir'
13
+ import stripAnsi from 'strip-ansi'
14
+ import { gzipSizeSync } from 'gzip-size'
15
+
16
+ function canReadAsset(asset) {
17
+ return (
18
+ /\.(js|css)$/.test(asset) &&
19
+ !/service-worker\.js/.test(asset) &&
20
+ !/precache-manifest\.[0-9a-f]+\.js/.test(asset)
21
+ )
22
+ }
23
+
24
+ // Prints a detailed summary of build files.
25
+ function printFileSizesAfterBuild(
26
+ webpackStats,
27
+ previousSizeMap,
28
+ buildFolder,
29
+ maxBundleGzipSize,
30
+ maxChunkGzipSize
31
+ ) {
32
+ var root = previousSizeMap.root
33
+ var sizes = previousSizeMap.sizes
34
+ var assets = (webpackStats.stats || [webpackStats])
35
+ .map((stats) =>
36
+ stats
37
+ .toJson({ all: false, assets: true })
38
+ .assets.filter((asset) => canReadAsset(asset.name))
39
+ .map((asset) => {
40
+ var fileContents = fs.readFileSync(path.join(root, asset.name))
41
+ var size = gzipSizeSync(fileContents)
42
+ var previousSize = sizes[removeFileNameHash(root, asset.name)]
43
+ var difference = getDifferenceLabel(size, previousSize)
44
+ return {
45
+ folder: path.join(path.basename(buildFolder), path.dirname(asset.name)),
46
+ name: path.basename(asset.name),
47
+ size: size,
48
+ sizeLabel: filesize(size) + (difference ? ' (' + difference + ')' : ''),
49
+ }
50
+ })
51
+ )
52
+ .reduce((single, all) => all.concat(single), [])
53
+
54
+ if (assets.length === 0) return
55
+
56
+ console.log('\ngzip 后文件大小:\n')
57
+
58
+ assets.sort((a, b) => b.size - a.size)
59
+
60
+ // move main file to first
61
+ var mainAssetIdx = assets.findIndex((asset) => /_\d+\.\d+\.\d+\./.test(asset.name))
62
+ assets.unshift(assets.splice(mainAssetIdx, 1)[0])
63
+
64
+ var longestSizeLabelLength = Math.max.apply(
65
+ null,
66
+ assets.map((a) => stripAnsi(a.sizeLabel).length)
67
+ )
68
+ var suggestBundleSplitting = false
69
+ assets.forEach((asset) => {
70
+ var sizeLabel = asset.sizeLabel
71
+ var sizeLength = stripAnsi(sizeLabel).length
72
+ if (sizeLength < longestSizeLabelLength) {
73
+ var rightPadding = ' '.repeat(longestSizeLabelLength - sizeLength)
74
+ sizeLabel += rightPadding
75
+ }
76
+ var isMainBundle = /_\d+\.\d+\.\d+\./.test(asset.name)
77
+ var maxRecommendedSize = isMainBundle ? maxBundleGzipSize : maxChunkGzipSize
78
+ var isLarge = maxRecommendedSize && asset.size > maxRecommendedSize
79
+ if (isLarge && path.extname(asset.name) === '.js') {
80
+ suggestBundleSplitting = true
81
+ }
82
+ console.log(
83
+ ' ' +
84
+ (isLarge ? chalk.yellow(sizeLabel) : sizeLabel) +
85
+ ' ' +
86
+ chalk.dim(asset.folder + path.sep) +
87
+ chalk.cyan(asset.name)
88
+ )
89
+ if (isMainBundle) {
90
+ console.log('')
91
+ }
92
+ })
93
+ if (suggestBundleSplitting) {
94
+ console.log()
95
+ console.log(chalk.yellow('产物大小明显大于推荐的大小 (主文件 30k, chunk 1M, 黄色标注为偏大)'))
96
+ console.log(chalk.yellow('考虑下使用代码分割解决'))
97
+ console.log(chalk.yellow('也可以使用 npm run analyze 命令分析产物'))
98
+ }
99
+
100
+ console.log()
101
+ }
102
+
103
+ function removeFileNameHash(buildFolder, fileName) {
104
+ return fileName
105
+ .replace(buildFolder, '')
106
+ .replace(/\\/g, '/')
107
+ .replace(/\/\d+\.\d+\.\d+\//, '/')
108
+ .replace(/\/?(.*)(\.[0-9a-f]+)(\.chunk)?(\.js|\.css)/, (match, p1, p2, p3, p4) => p1 + p4)
109
+ }
110
+
111
+ // Input: 1024, 2048
112
+ // Output: "(+1 KB)"
113
+ function getDifferenceLabel(currentSize, previousSize) {
114
+ var FIFTY_KILOBYTES = 1024 * 50
115
+ var difference = currentSize - previousSize
116
+ var fileSize = !Number.isNaN(difference) ? filesize(difference) : 0
117
+ if (difference >= FIFTY_KILOBYTES) {
118
+ return chalk.red('+' + fileSize)
119
+ } else if (difference < FIFTY_KILOBYTES && difference > 0) {
120
+ return chalk.yellow('+' + fileSize)
121
+ } else if (difference < 0) {
122
+ return chalk.green(fileSize)
123
+ } else {
124
+ return ''
125
+ }
126
+ }
127
+
128
+ function measureFileSizesBeforeBuild(buildFolder) {
129
+ return new Promise((resolve) => {
130
+ recursive(buildFolder, (err, fileNames) => {
131
+ var sizes
132
+ if (!err && fileNames) {
133
+ sizes = fileNames.filter(canReadAsset).reduce((memo, fileName) => {
134
+ var contents = fs.readFileSync(fileName)
135
+ var key = removeFileNameHash(buildFolder, fileName)
136
+ memo[key] = gzipSizeSync(contents)
137
+ return memo
138
+ }, {})
139
+ }
140
+ resolve({
141
+ root: buildFolder,
142
+ sizes: sizes || {},
143
+ })
144
+ })
145
+ })
146
+ }
147
+
148
+ export { measureFileSizesBeforeBuild, printFileSizesAfterBuild }
@@ -0,0 +1,36 @@
1
+ import paths from '../config/paths.js'
2
+ import fs from 'fs-extra'
3
+
4
+ const appPkg = fs.readJsonSync(paths.package)
5
+
6
+ const edu = appPkg.edu || {}
7
+
8
+ if (edu.single || edu.mainProject) {
9
+ if (edu.single) {
10
+ edu.mode = 'single'
11
+ delete edu.single
12
+ }
13
+ if (edu.mainProject) {
14
+ edu.mode = 'main'
15
+ delete edu.mainProject
16
+ }
17
+ fs.writeFileSync(paths.package, JSON.stringify(appPkg, null, 2), 'utf-8')
18
+ }
19
+
20
+ const appConfig = {
21
+ /** @type {'main'|'single'} */
22
+ get mode() {
23
+ return edu.mode
24
+ },
25
+ get mainProject() {
26
+ return this.mode === 'main'
27
+ },
28
+ get single() {
29
+ return this.mode === 'single'
30
+ },
31
+ get grayscale() {
32
+ return !!edu.grayscale
33
+ },
34
+ }
35
+
36
+ export default appConfig
@@ -0,0 +1,55 @@
1
+ import fs from 'fs-extra'
2
+ import paths from '../config/paths.js'
3
+ import chalk from 'chalk'
4
+ import semver from 'semver'
5
+ import appConfig from './appConfig.js'
6
+ import { fileURLToPath } from 'node:url'
7
+
8
+ const pkg = fs.readJsonSync(fileURLToPath(import.meta.resolve('../../package.json')))
9
+
10
+ const appPkg = fs.readJsonSync(paths.package)
11
+
12
+ if (semver.valid(appPkg.version) === null) {
13
+ console.log(
14
+ chalk.red(`package.version 不符合 semver 规范 https://docs.npmjs.com/about-semantic-versioning`)
15
+ )
16
+ process.exit(1)
17
+ }
18
+
19
+ switch (appConfig.mode) {
20
+ case 'main':
21
+ console.log(chalk.bgMagenta('正在使用教育主工程模式'))
22
+ break
23
+ case 'single':
24
+ console.log(chalk.bgMagenta('正在使用独立项目模式'))
25
+ break
26
+ default:
27
+ console.log(chalk.bgMagenta('正在使用教育集成模式'))
28
+ break
29
+ }
30
+
31
+ if (appConfig.grayscale) {
32
+ console.log(chalk.bgYellow('正在使用灰度测试模式'))
33
+ }
34
+
35
+ if (!(appConfig.single || appConfig.mainProject)) {
36
+ if (fs.existsSync(paths.static)) {
37
+ console.log(chalk.bgYellow('教育集成工程不能含有 public/static, 现已自动删除'))
38
+ try {
39
+ fs.rmSync(paths.static, { recursive: true })
40
+ } catch (e) {}
41
+ }
42
+ }
43
+
44
+ if (!fs.existsSync(paths.public) && !process.argv.includes('auto-refactor')) {
45
+ console.log(chalk.red(`public 文件夹不存在,请先按文档改造项目`))
46
+ console.log(`文档: ${chalk.underline(pkg.homepage)}`)
47
+ console.log(`\n使用 ${chalk.green('npx edu-scripts auto-refactor')} 自动改造\n`)
48
+ process.exit(0)
49
+ }
50
+
51
+ if (appPkg.browserslist) {
52
+ console.log(chalk.yellow('已删除 package.json 中 browserslist,该值由内部自动控制\n'))
53
+ delete appPkg.browserslist
54
+ fs.writeFileSync(paths.package, JSON.stringify(appPkg, null, 2), 'utf-8')
55
+ }
@@ -0,0 +1,119 @@
1
+ import { parse, traverse, types as t, transformFromAstSync } from '@babel/core'
2
+
3
+ // ver.js 中定义模块的数组
4
+ const TARGET_IDENTIFIER_NAME = 'project_apiArr'
5
+ const MODULE_IDENTIFIER_NAME = 'module'
6
+
7
+ function changeDeployVersion(code, pkg) {
8
+ const { name, version, grayscale } = pkg
9
+ let ast
10
+ try {
11
+ ast = parse(code)
12
+ } catch (error) {
13
+ throw new Error(`代码解析错误: ${error.message}`)
14
+ }
15
+ const keyName = grayscale ? 'grayscale' : 'main'
16
+
17
+ /**
18
+ * @return {babel.NodePath<t.VariableDeclarator> | undefined}
19
+ */
20
+ function findTargetDeclarator(ast) {
21
+ let res
22
+
23
+ traverse(ast, {
24
+ VariableDeclarator(path) {
25
+ if (
26
+ t.isIdentifier(path.node.id, { name: TARGET_IDENTIFIER_NAME }) &&
27
+ t.isArrayExpression(path.node.init)
28
+ ) {
29
+ res = path
30
+ }
31
+ },
32
+ })
33
+
34
+ return res
35
+ }
36
+
37
+ /**
38
+ * @param {babel.NodePath<t.VariableDeclarator>} path
39
+ * @return {babel.NodePath<t.ObjectExpression> | undefined}
40
+ */
41
+ function findModuleObject(path) {
42
+ let res
43
+
44
+ path.traverse({
45
+ ObjectExpression(path) {
46
+ if (
47
+ path.node.properties.some(
48
+ (node) =>
49
+ t.isIdentifier(node.key, { name: MODULE_IDENTIFIER_NAME }) &&
50
+ t.isLiteral(node.value, { value: name })
51
+ )
52
+ ) {
53
+ res = path
54
+ }
55
+ },
56
+ })
57
+
58
+ return res
59
+ }
60
+
61
+ /**
62
+ * @param {babel.NodePath<t.ObjectExpression>} path
63
+ */
64
+ function modifyModuleVersion(path) {
65
+ let hasModify = false
66
+ path.traverse({
67
+ Property(path) {
68
+ if (path.node.key.name === keyName) {
69
+ path.node.value.value = version
70
+ hasModify = true
71
+ }
72
+ },
73
+ })
74
+
75
+ if (!hasModify) {
76
+ path.node.properties.push(t.objectProperty(t.identifier(keyName), t.stringLiteral(version)))
77
+ }
78
+ }
79
+ /**
80
+ * @param {babel.NodePath<t.ObjectExpression>} path
81
+ */
82
+ function deleteModuleComments(path) {
83
+ path.traverse({
84
+ ObjectExpression(path) {
85
+ delete path.node.leadingComments
86
+ },
87
+ })
88
+ }
89
+
90
+ /**
91
+ * @param {babel.NodePath<t.VariableDeclarator>} path
92
+ */
93
+ function addModuleToTarget(path) {
94
+ const elements = path.node.init.elements
95
+ elements.splice(
96
+ elements.length - 2,
97
+ 0,
98
+ t.objectExpression([
99
+ t.objectProperty(t.identifier(MODULE_IDENTIFIER_NAME), t.stringLiteral(name)),
100
+ t.objectProperty(t.identifier(keyName), t.stringLiteral(version)),
101
+ ])
102
+ )
103
+ }
104
+
105
+ const targetPath = findTargetDeclarator(ast)
106
+ if (!targetPath) throw new Error(`ver.js 不合规范,未找到参数 ${TARGET_IDENTIFIER_NAME}`)
107
+
108
+ const moduleObjPath = findModuleObject(targetPath)
109
+ if (moduleObjPath) {
110
+ modifyModuleVersion(moduleObjPath)
111
+ } else {
112
+ addModuleToTarget(targetPath)
113
+ }
114
+ deleteModuleComments(targetPath)
115
+
116
+ return transformFromAstSync(ast, undefined, { minified: true }).code
117
+ }
118
+
119
+ export default changeDeployVersion
@@ -0,0 +1,81 @@
1
+ import type { Chalk } from 'chalk'
2
+ import type { Compiler, Configuration } from '@rspack/core'
3
+ import type { Configuration as DevServerConfiguration } from '@rspack/dev-server'
4
+
5
+ type ProxyConfigArray = NonNullable<DevServerConfiguration['proxy']>
6
+
7
+ export type BabelImportPlugin = {
8
+ libraryName: string
9
+ libraryDirectory: string
10
+ camel2DashComponentName?: boolean
11
+ style?: boolean | 'css' | ((name: string) => string)
12
+ }
13
+
14
+ export type Config = {
15
+ webpack?: (config: Configuration) => Configuration | undefined
16
+ devServer?: (config: DevServerConfiguration) => DevServerConfiguration | undefined
17
+ /**
18
+ * webpack alias 配置,会与内置 alias 合并
19
+ *
20
+ * @default
21
+ * { '@': './src' }
22
+ */
23
+ alias?: Record<string, string>
24
+ /**
25
+ * webpack externals 配置,会与内置 externals 合并
26
+ */
27
+ externals?: Record<string, string>
28
+ /**
29
+ * terser pure_funcs,esbuild pure 配置,传 [] 可以不清空 console.log
30
+ * @default ['console.log']
31
+ */
32
+ pure_funcs?: string[]
33
+ /**
34
+ * 是否压缩代码
35
+ * @default true
36
+ */
37
+ minify?: boolean
38
+ /** 自定义全局参数 */
39
+ define?: Record<string, any>
40
+ /**
41
+ * context 填写的顺序很重要,前面的会优先匹配。可以重复定义 /api 来覆盖默认的配置
42
+ *
43
+ * proxy 会自动删除请求头 referer,避免某些后端校验失败的问题
44
+ * @see https://webpack.js.org/configuration/dev-server/#devserverproxy
45
+ * @default /api -> /qsxxwapdev/api
46
+ * @example { '/api': 'http://localhost:3000' }
47
+ * @example [{ context: ['/api'], target: 'http://localhost:3000' }]
48
+ */
49
+ proxy?: ProxyConfigArray | Record<string, string>
50
+ extraPostCSSPlugins?: any[]
51
+ /**
52
+ * 开启 mock 功能,会自动加载 mock 文件夹下的文件
53
+ *
54
+ * 默认情况下自动判断根目录是否存在 mock 文件夹,如果存在则开启 mock 功能。
55
+ * 如果设置 false,会关闭 mock 功能
56
+ */
57
+ mock?: boolean
58
+ /**
59
+ * 开发模式启动后只执行一次的函数,用来展示启动后的提示信息,或者自定义的逻辑
60
+ */
61
+ startup?: (params: {
62
+ /** 控制台输出内容 */
63
+ logger: ReturnType<Compiler['getInfrastructureLogger']>
64
+ /** 字体颜色相关工具 */
65
+ chalk: Chalk
66
+ /** webpack compiler 自定义的时候会用到 */
67
+ compiler: Compiler
68
+ }) => void
69
+
70
+ /**
71
+ * 是否开启装饰器语法支持 只支持 legacy 版本
72
+ * @see https://babeljs.io/docs/en/babel-plugin-proposal-decorators
73
+ * @default false
74
+ */
75
+ decorators?: boolean
76
+ /** babel-plugin-import */
77
+ import?: BabelImportPlugin[]
78
+ }
79
+ export function defineConfig(config: Config) {
80
+ return config
81
+ }
@@ -0,0 +1,7 @@
1
+ import cp from 'child_process'
2
+ const exec = (cmd) => {
3
+ console.log(`> ${cmd}`)
4
+ return cp.execSync(cmd, { stdio: 'inherit', encoding: 'utf-8' })
5
+ }
6
+
7
+ export default exec
@@ -0,0 +1,26 @@
1
+ import getWebpackConfig from '../config/webpackConfig.js'
2
+ import getWebpackDevServerConfig from '../config/webpackDevServerConfig.js'
3
+ import getOverride from './getOverride.js'
4
+
5
+ function getConfig(args) {
6
+ const override = getOverride()
7
+
8
+ let webpackConfig = getWebpackConfig(args, override)
9
+
10
+ if (override.webpack) {
11
+ webpackConfig = override.webpack(webpackConfig) || webpackConfig
12
+ }
13
+
14
+ if (process.env.NODE_ENV === 'development') {
15
+ let devServerConfig = getWebpackDevServerConfig(args, override)
16
+
17
+ if (override.devServer) {
18
+ devServerConfig = override.devServer(devServerConfig) || devServerConfig
19
+ }
20
+ webpackConfig.devServer = devServerConfig
21
+ }
22
+
23
+ return webpackConfig
24
+ }
25
+
26
+ export default getConfig
@@ -0,0 +1,33 @@
1
+ import fs from 'fs-extra'
2
+ import paths from '../config/paths.js'
3
+ import { createRequire } from 'node:module'
4
+ const require = createRequire(import.meta.url)
5
+
6
+ /** @type {import('./defineConfig').Config} */
7
+ const defaultOverride = {
8
+ minify: true,
9
+ proxy: [],
10
+ extraPostCSSPlugins: [],
11
+ import: [],
12
+ pure_funcs: ['console.log'],
13
+ }
14
+
15
+ let override = null
16
+
17
+ /**
18
+ * @return {import('./defineConfig').Config}
19
+ */
20
+ export default function getOverride() {
21
+ if (override) return override
22
+
23
+ override = Object.assign({}, defaultOverride)
24
+
25
+ if (fs.existsSync(paths.override)) {
26
+ const userOverride = require(paths.override)
27
+ if (typeof userOverride !== 'object')
28
+ throw new Error('格式错误,请使用 npx edu g override 生成文件')
29
+ Object.assign(override, userOverride)
30
+ }
31
+
32
+ return override
33
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "esnext",
4
+ "module": "node16",
5
+ "moduleResolution": "node16",
6
+ "importHelpers": true,
7
+ "jsx": "react",
8
+ "esModuleInterop": true,
9
+ "sourceMap": true,
10
+ "baseUrl": "./",
11
+ "strict": true,
12
+ "paths": {
13
+ "@/*": ["src/*"],
14
+ "@@/*": ["src/.umi/*"]
15
+ },
16
+ "allowSyntheticDefaultImports": true,
17
+ "skipLibCheck": true,
18
+ "declaration": true
19
+ },
20
+ "include": ["src", "scripts"]
21
+ }